spring mvc model.addAttribute页面c:forEach取不到
2014-09-05 10:18
471 查看
昨天和今天都在解决一个问题,即:
@RequestMapping(value = "/listAccounts", method = RequestMethod.GET)
public String searchAccount(Model model,HttpServletRequest request, HttpServletResponse response) {
System.out.println("111111111111");
List<Account> temps = new ArrayList<Account>();
Account temp1 = new Account();
temp1.setBalance(20.0);
temp1.setId(1);
temp1.setHolder("rod");
temps.add(temp1);
Account temp2 = new Account();
temp2.setBalance(30.0);
temp2.setId(2);
temp2.setHolder("dianne");
temps.add(temp2);
model.addAttribute("accounts",temps);
return "listAccounts";
}
页面:
<c:forEach var="account" items="${accounts}">
<tr>
<td>${account.id}</td>
<td>${account.holder}</td>
<td>${account.balance}</td>
<td>${account.overdraft}</td>
<td>
<a href="post.html?id=${account.id}&amount=-20.00">-$20</a>
<a href="post.html?id=${account.id}&amount=-5.00">-$5</a>
<a href="post.html?id=${account.id}&amount=5.00">+$5</a>
<a href="post.html?id=${account.id}&amount=20.00">+$20</a>
</td>
</tr>
</c:forEach>
页面总是取不到值,总显示类似于${account.id}的值。
经过查资料终于找到解决问题的办法:
原来是页面识别不了el表达式,${}是el表达式,jsp默认支持,为什么识别不了呢?
解决办法:
方法一:
eclipse版本问题,isELIgnored默认是true,改成<%@ page isELIgnored="false" %>,即每个jsp都需要这样改,太麻烦了。
方法二:
web.xml中加上,简单,但是还不是好的解决办法。
<jsp-config>
<jsp-property-group>
<url-pattern>*.jsp</url-pattern>
<el-ignored>false</el-ignored>
</jsp-property-group>
</jsp-config>
方法三:web.xml中
如果有以下内容,表示是Servlet 2.3 / JSP 1.2。
<!--CTYPE
web-app PUBLIC </sp-->
"-//Sun
Microsystems, Inc.//DTD Web Application 2.3//EN"
"http://java.sun.com/dtd/web-app_2_3.dtd">
在默认情况下,Servlet
2.3 / JSP 1.2是不支持EL表达式的,而Servlet 2.4 / JSP 2.0支持。
将web.xml改成如下:
<web-app
version="2.5"
xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
如果web.xml以上设置也不支持EL表达式:
解决方法:
1.修改web.xml文件为(Servlet
2.4 / JSP 2.0):
<web-app
version="2.4"
xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_4.xsd">
@RequestMapping(value = "/listAccounts", method = RequestMethod.GET)
public String searchAccount(Model model,HttpServletRequest request, HttpServletResponse response) {
System.out.println("111111111111");
List<Account> temps = new ArrayList<Account>();
Account temp1 = new Account();
temp1.setBalance(20.0);
temp1.setId(1);
temp1.setHolder("rod");
temps.add(temp1);
Account temp2 = new Account();
temp2.setBalance(30.0);
temp2.setId(2);
temp2.setHolder("dianne");
temps.add(temp2);
model.addAttribute("accounts",temps);
return "listAccounts";
}
页面:
<c:forEach var="account" items="${accounts}">
<tr>
<td>${account.id}</td>
<td>${account.holder}</td>
<td>${account.balance}</td>
<td>${account.overdraft}</td>
<td>
<a href="post.html?id=${account.id}&amount=-20.00">-$20</a>
<a href="post.html?id=${account.id}&amount=-5.00">-$5</a>
<a href="post.html?id=${account.id}&amount=5.00">+$5</a>
<a href="post.html?id=${account.id}&amount=20.00">+$20</a>
</td>
</tr>
</c:forEach>
页面总是取不到值,总显示类似于${account.id}的值。
经过查资料终于找到解决问题的办法:
原来是页面识别不了el表达式,${}是el表达式,jsp默认支持,为什么识别不了呢?
解决办法:
方法一:
eclipse版本问题,isELIgnored默认是true,改成<%@ page isELIgnored="false" %>,即每个jsp都需要这样改,太麻烦了。
方法二:
web.xml中加上,简单,但是还不是好的解决办法。
<jsp-config>
<jsp-property-group>
<url-pattern>*.jsp</url-pattern>
<el-ignored>false</el-ignored>
</jsp-property-group>
</jsp-config>
方法三:web.xml中
如果有以下内容,表示是Servlet 2.3 / JSP 1.2。
<!--CTYPE
web-app PUBLIC </sp-->
"-//Sun
Microsystems, Inc.//DTD Web Application 2.3//EN"
"http://java.sun.com/dtd/web-app_2_3.dtd">
在默认情况下,Servlet
2.3 / JSP 1.2是不支持EL表达式的,而Servlet 2.4 / JSP 2.0支持。
将web.xml改成如下:
<web-app
version="2.5"
xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
如果web.xml以上设置也不支持EL表达式:
解决方法:
1.修改web.xml文件为(Servlet
2.4 / JSP 2.0):
<web-app
version="2.4"
xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_4.xsd">
相关文章推荐
- 关于springmvc.....model.addAttribute()之后jsp页面c:foreach取不到的问题
- SPRING IN ACTION 第4版笔记-第七章Advanced Spring MVC-006- 如何保持重定向的request数据(用model、占位符、RedirectAttributes、model.addFlashAttribute("spitter", spitter);)
- 关于modelmap.addAttribute("",)转到jsp页面获取不到值的问题
- Spring MVC @ModelAttribute 获取不到值
- spring-mvc @ModelAttribute 绑定表单数据到VO
- springmvc @SessionAttributes和@ModelAttribute的正确使用
- SpringMVC @ModelAttribute 详解
- Spring MVC @ModelAttribute 详解
- bug Spring Mvc中Jsp页面获取不到Controller中的变量
- Spring MVC @ModelAttribute详解
- springmvc+mybatis+maven项目集成的时候遇到一个问题,项目搭起来以后,http请求怎么都进不到controller中,页面直接返回404错误,控制台没有报任何错误,请大神帮我看看
- SpringMVC @ModelAttribute
- SpringMVC——@ModelAttribute
- SpringMVC @ModelAttribute 的使用
- SpringMvc @ModelAttribute
- Spring MVC @ModelAttribute 详解
- springMVC @ModelAttribute学习
- Spring MVC @ModelAttribute
- springMVC @ModelAttribute学习
- SpringMVC @ModelAttribute 的使用【转】