Search code examples
javajspspring-mvcsessionjstl

Why does calling a session on jsp returns an error?


I am having problem with calling a session on my jsp file. I am trying to create a Spring and Hibernate project that involves a session using Java Based Configuration(No xml configuration). Below is the snapshot of what I am doing.

This is one of my controller:

@Controller
@RequestMapping(value = "/sp")
public class ELibraryController {

    @RequestMapping(value = "/form")
    public String index(ModelMap m) {
        m.put("basket", new Basket());
        return "form";
    }

    @SuppressWarnings("unchecked")
    @RequestMapping(value = "/addbasket", method = RequestMethod.POST)
    public String addbasket(@ModelAttribute("basket") Basket b, HttpSession session) {
        List<Basket> lst = (List<Basket>) session.getAttribute("basket");
        if(lst == null) {
            lst = new ArrayList<>();
            lst.add(b);
        }else {
            boolean flag = false;
            for(Basket basket : lst) {
                if(basket.getId()==b.getId()) {
                    basket.setQuantity(basket.getQuantity()+1);
                    flag = true;
                    break;
                }
            }
            if(flag==false)
                lst.add(b);
        }
        session.setAttribute("basket", lst);
        session.setAttribute("total", lst);
        return "basket";
    }

    public int getNoBooksBorrowed(List<Basket> lst) {
        int total = 0;
        for(Basket basket : lst) {
            total += (basket.getQuantity());
        }
        return total;
    }

}

This is my form.jsp

...
<body>
    <f:form modelAttribute="basket" action="addbasket" method="POST">
        ID: <f:input path="id"/><br/>
        Book Name: <f:input path="name"/><br/>
        Quantity: <f:input path="quantity"/><br/>
        <input type="submit" value="Add to Basket"/>
    </f:form>
</body>
...

where f is a spring forms.

This is my basket.jsp

...
<table border="1" cellpadding="10px" width="100%" style="border-collapse: collapse;">
        <c:forEach items="<%= request.getSession().getAttribute("basket") %>" var="b">
            <tr>
                <td>${b.id}</td>
                <td>${b.name}</td>
                <td>${b.quantity}</td>
                <td>
                    Remove
                </td>

            </tr>
        </c:forEach>
            <tr>
                <td colspan="5">
                    Total Book/s Borrowd:  <%= request.getSession().getAttribute("total") %>
                </td>
            </tr>
</table>
...

where c is a jstl core.

Since I am using Java Based Configuration. Below is the initializer class:

public class AppInitializer extends AbstractAnnotationConfigDispatcherServletInitializer {

    @Override
    protected Class<?>[] getRootConfigClasses() {
        return new Class[] { AppConfig.class };
    }

    @Override
    protected Class<?>[] getServletConfigClasses() {
        return null;
    }

    @Override
    protected String[] getServletMappings() {
        return new String[] { "/" };
    }
}

This is my class AppConfig.java

@Configuration
@EnableWebMvc
@ComponentScan(basePackages = "com.org.elibrary")
public class AppConfig extends WebMvcConfigurerAdapter{

    @Override
    public void configureViewResolvers(ViewResolverRegistry registry) {
        InternalResourceViewResolver viewResolver = new InternalResourceViewResolver();
        viewResolver.setViewClass(JstlView.class);
        viewResolver.setPrefix("/WEB-INF/views/");
        viewResolver.setSuffix(".jsp");
        registry.viewResolver(viewResolver);
    }
}

Upon running, if i browse to http://localhost:8080/Elibrary/sp/form, the page displays correctly but when i click Add to Basket button, the browser point to http://localhost:8080/Elibrary/sp/addbasket but returns this following error:

SEVERE: Servlet.service() for servlet [dispatcher] in context with path [/Elibrary] threw exception [/WEB-INF/views/basket.jsp (line: 13, column: 24) Attribute value  request.getSession().getAttribute("basket")  is quoted with " which must be escaped when used within the value] with root cause
org.apache.jasper.JasperException: /WEB-INF/views/basket.jsp (line: 13, column: 24) Attribute value  request.getSession().getAttribute("basket")  is quoted with " which must be escaped when used within the value
    at org.apache.jasper.compiler.DefaultErrorHandler.jspError(DefaultErrorHandler.java:41)
...

Thank you very much for any help!


Solution

  • Use single quotes

    "<%=request.getSession().getAttribute('basket')%>"
    

    Or use

    '<%=request.getSession().getAttribute("basket")%>'