昨天和今天都在解決一個問題,即:
@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">