转:spring mvc model.addAttribute页面c:forEach取不到

spring mvc model.addAttribute页面c:forEach取不到

昨天和今天都在解决一个问题,即:

@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?>-$20</a>

<a href="post.html?>-$5</a>

<a href="post.html?>+$5</a>

<a href="post.html?>+$20</a>

</td>

</tr>

</c:forEach>

页面总是取不到值,总显示类似于${account.id}的值。

经过查资料终于找到解决问题的办法:

原来是页面识别不了el表达式,${}是el表达式,jsp默认支持,为什么识别不了呢?

解决办法:

方法一:

eclipse版本问题,isELIgnored默认是true,改成<%@ page isELIgnored="false" %>,即每个jsp都需要这样改,太麻烦了。

方法二:

web.xml中加上,简单,但是还不是好的解决办法。

  1. <jsp-config>
  2. <jsp-property-group>
  3. <url-pattern>*.jsp</url-pattern>
  4. <el-ignored>false</el-ignored>
  5. </jsp-property-group>
  6. </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?>-$20</a>

<a href="post.html?>-$5</a>

<a href="post.html?>+$5</a>

<a href="post.html?>+$20</a>

</td>

</tr>

</c:forEach>

页面总是取不到值,总显示类似于${account.id}的值。

经过查资料终于找到解决问题的办法:

原来是页面识别不了el表达式,${}是el表达式,jsp默认支持,为什么识别不了呢?

解决办法:

方法一:

eclipse版本问题,isELIgnored默认是true,改成<%@ page isELIgnored="false" %>,即每个jsp都需要这样改,太麻烦了。

方法二:

web.xml中加上,简单,但是还不是好的解决办法。

  1. <jsp-config>
  2. <jsp-property-group>
  3. <url-pattern>*.jsp</url-pattern>
  4. <el-ignored>false</el-ignored>
  5. </jsp-property-group>
  6. </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">