首先,SpringMVC中Controller向jsp传递数据大概有五种方法:
1.使用Model:
Model model;
model.addAttribute("xxx", xxx);
返回String,即视图名称。
2.使用HashMap:
HashMap<String,Object> hashMap;
hashMap.put("xxx", xxx);
返回String,即视图名称。
3.使用Session:
HttpSession session;
session.setAttribute("xxx", xxx);
返回String,即视图名称。
4.使用Request:
HttpServletRequest request;
request.setAttribute("xxx", xxx);
返回String,即视图名称。
5.使用ModelAndView:
ModelAndView modelAndView;
view.addObject("xxx", xxx);
view.setViewName("xxx"); //此处为视图名称
返回值类型为ModelAndView。
但是,无论我用哪种方法,都取不了值,代码和运行截图如下:
Controller:
@RequestMapping(value = "/selectCityByLevel")
public String SelectCityByLevel(Model model){
List<City> cities = new ArrayList<City>();
cities = cityService.FindCityByLevel(1);
model.addAttribute("cities",cities);
return "test";
}
JSP(test.jsp):
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<html>
<head>
<title>Title</title>
</head>
<body>
<c:forEach items="${cities}" var="list">
${list.name}
</c:forEach>
</body>
</html>
运行截图:
上网百度,排查了一天都没找到问题所在,很崩溃!
最终皇天不负有心人!!!终于被我揪出来了!!!!
这是改过以后的代码,看出来了没?!!
<%@ page contentType="text/html;charset=UTF-8" language="java" isELIgnored="false" %>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<html>
<head>
<title>Title</title>
</head>
<body>
<c:forEach items="${cities}" var="list">
${list.name}
</c:forEach>
</body>
</html>
就是多加了这个东东!!!
运行截图:
于是我也上网百度了下原因,借用他们大神的话,就是:
“在 page directive 中的 isELIgnored 属性用来指定是否忽略。格式为: <%@ page isELIgnored="true|false"%>。
如果设定为真,那么JSP中的表达式被当成字符串处理!!!”