---------------------siwuxie095
ViewResolver 和 JSTL
1、SpringMVC 的视图解析器 InternalResourceViewResolver
默认使用的是
JSP 标准标签库 JSTL
2、导入 JSTL 的 jar 包,下载链接:
(1)http://tomcat.apache.org/taglibs/standard/
(2)http://archive.apache.org/dist/jakarta/taglibs/standard/binaries/
注意:
JSTL 1.1 版本及之前,需要两个 jar 包:jstl 和 standard
JSTL 1.2
版本及之后,只需要一个
jar 包:jstl
3、具体案例
(1)编写一个实体类
User.java:
package com.siwuxie095.entity; public class User { private String userId; private String userName; private String userSex; private String userAge;
public String getUserId() { return userId; } public this.userId = userId; }
public String getUserName() { return userName; } public this.userName = userName; }
public String getUserSex() { return userSex; } public this.userSex = userSex; }
public String getUserAge() { return userAge; } public this.userAge = userAge; }
@Override public String toString() { return + ", userSex=" + userSex + ", userAge=" + userAge + "]"; }
} |
(2)编写一个
JSP 页面
show.jsp:
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <!-- 引用 JSTL 标签库 --> <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" <html> <head> <meta <title>show</title> </head> <body> <table>
<c:forEach
<tr> <td>${user.userId}</td> <td>${user.userName}</td> <td>${user.userSex}</td> <td>${user.userAge}</td> </tr>
</c:forEach>
</table>
</body> </html> |
(3)编写一个
Controller 类
UserController.java:
package com.siwuxie095.controller; import java.util.ArrayList; import java.util.List; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.servlet.ModelAndView; import com.siwuxie095.entity.User; @Controller public class UserController { @RequestMapping("/show") public ModelAndView show() { // 创建 ModelAndView 对象,并设置视图名称 ModelAndView mv = new ModelAndView("show");
List<User> userList = new ArrayList<User>(); for (int i = 0; i < 10; i++) { User user = new User(); user.setUserId("100" + i); user.setUserName("小明-"+i); user.setUserSex("男"); user.setUserAge("1" + i); userList.add(user); }
// 添加模型数据 mv.addObject("userList", userList); return mv; }
} |
(4)访问路径:
http://localhost:8080/工程名/show.do
【made by siwuxie095】