0
我在調用jsp文件上的會話時遇到了問題。我正在嘗試創建一個涉及使用基於Java的配置(無xml配置)的會話的Spring和Hibernate項目。下面是我正在做的事情的快照。爲什麼在jsp上調用會話會返回錯誤?
這是我的控制器的一個:
@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;
}
}
這是我的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>
...
其中˚F是彈簧形式。
這是我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>
...
其中Ç是JSTL核心。
因爲我使用基於Java的配置。下面是初始化類:
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[] { "/" };
}
}
這是我的課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);
}
}
在運行時,如果我瀏覽到,頁面顯示正常,但是當我點擊添加到購物籃按鈕,瀏覽器指向http://localhost:8080/Elibrary/sp/addbasket但是返回以下錯誤:
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)
...
非常感謝您的幫助!
非常感謝你。它現在使用第二個。 –