springMVC 使用jstl
原创
©著作权归作者所有:来自51CTO博客作者pyzheng的原创作品,请联系作者获取转载授权,否则将追究法律责任
1. pom.xml
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jstl</artifactId>
<version>1.2</version>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>taglibs</groupId>
<artifactId>standard</artifactId>
<version>1.1.2</version>
</dependency>
2. web.xml
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
version="3.0">
3.xxx-servlet.xml
<bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="requestContextAttribute" value="rc"/>
<property name="viewClass" value="org.springframework.web.servlet.view.JstlView"/>
<property name="prefix" value="/"/>
<property name="suffix" value=".jsp"/>
</bean>
4. controller
@RequestMapping("/list.do")
public ModelAndView list() {
List<Map<String, String>> list = userService.listAll();
ModelAndView view = new ModelAndView("list.jsp");
view.addObject("processList", list);
view.addObject("TestStr", "This is a test message.");
return view;
}
5. jsp
[color=darkblue]<%@taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>[/color]
[i]<body>
${TestStr}
<c:forEach var="process" items="${processList}">
${process.name }
</c:forEach>
</body>[/i]