汇总 SpringMVC 传参给到 JSP ,JSP 使用 JSTL
表达式取值来显示的各种方式。
准备
添加Jar
包,编写Controller
类,编写JSP
代码。
Maven配置文件pom.xml
添加jsp
相关jar
包。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
| <dependency> <groupId>javax.servlet</groupId> <artifactId>jstl</artifactId> <version>1.2</version> </dependency> <dependency> <groupId>javax.servlet.jsp</groupId> <artifactId>jsp-api</artifactId> <version>2.1</version> <scope>provided</scope> </dependency> <dependency> <groupId>javax.servlet</groupId> <artifactId>javax.servlet-api</artifactId> <version>3.0.1</version> <scope>provided</scope> </dependency>
|
Controller 类
1 2 3 4 5 6 7 8
| @Controller @RequestMapping("/user") public class UserController { private final Logger logger = Logger.getLogger(this.getClass());
...执行方法... }
|
JSP 页面引入核心标签库和格式化标签库
1 2
| <%@ taglib prefix="c" uri="http://java.sun.com/jstl/core_rt" %> <%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %>
|
基本类型
Model
模型是以key:value
形式来存储数据的,Model的实现类ExtendedModelMap
继承了ModelMap
,ModelMap继承自LinkedHashMap
。
- java代码
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
|
@RequestMapping("/baseType") public String baseType(Model model) { model.addAttribute("张三"); model.addAttribute(56); model.addAttribute("name", "李四"); model.addAttribute("age",22); model.addAttribute("price", 35.89); model.addAttribute("orTrue", true); return "userInfo"; }
|
- jsp代码
1 2 3 4 5 6 7 8 9 10 11
| <body> <h2> <font color="red"><strong>基本类型</strong></font> </h2> <h3>String: ${name}</h3> <h3>int:${age}</h3> <h3>double:${price}</h3> <h3>boolean:${orTrue}</h3> <h3>default:${string}</h3> <h3>integer:${integer}</h3> </body>
|
对象类型
- java代码
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
|
@RequestMapping("/userInfo") public String userInfoPg(Model model) { User user = new User(); user.setRealName("王小二"); user.setAccount("root"); user.setPassword("root123"); user.setAge(22);
model.addAttribute("user",user); return "userInfo"; }
|
- jsp代码
1 2 3 4 5 6
| <body> <h2><font color="red"><strong>对象类型</strong></font></h2> <h3>真实姓名:${user.realName}</h3> <h3>登录名:${user.account}</h3> <h3>密 码:${user.password}</h3> </body>
|
List集合
基本类型集合
- java代码
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40
|
@RequestMapping("/baseList") public String baseList(Model model) { ArrayList<Object> arrayList = new ArrayList<>(); arrayList.add("张三"); arrayList.add("李四"); arrayList.add(30); arrayList.add(15.67); arrayList.add(true);
model.addAttribute("list", arrayList); return "userInfo"; }
2. jsp代码 ``` jsp <body> <h2> <font color="red"><strong>遍历基本类型List</strong></font> </h2> <h3> <!-- 偏历list --> <c:forEach var="item" items="${list }"> <tr> <c:out value="${item }"></c:out> </tr> </c:forEach> </h3> <!-- 从list集合中根据索引位取值 --> <h3>${list[0] }</h3> <h3>${list[1] }</h3> <h3>${list[2] }</h3> <h3>${list[3] }</h3> <br /> </body>
|
对象类型集合
- java代码
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
|
@RequestMapping("/userList") public String userList(Model model) { ArrayList<User> userList = new ArrayList<>(); User user1 = new User(); User user2 = new User(); user1.setAccount("root"); user1.setPassword("root123"); user1.setAge(22); user2.setAccount("admin"); user2.setPassword("admin123"); user2.setAge(28); userList.add(user1); userList.add(user2);
model.addAttribute("uList", userList); return "userInfo"; }
|
- jsp代码
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
| <h2> <font color="red"><strong>遍历对象类型List</strong></font> </h2> <h3>${uList[0].age }</h3> <h3>${uList[1].account }</h3> <table border="1"> <tr> <th>真实姓名</th> <th>账号</th> <th>密码</th> <th>年龄</th> </tr> <!-- 遍历对象list --> <c:forEach var="item" items="${uList}"> <tr> <td><c:out value="${item.realName}"></c:out></td> <td><c:out value="${item.account}"></c:out></td> <td><c:out value="${item.password}"></c:out></td> <td><c:out value="${item.age}"></c:out></td> </tr> </c:forEach> </table>
|
Map集合
- java代码
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
|
@RequestMapping("/userMap") public String userMap(Model model) { HashMap<String, User> userMap = new HashMap<>(); userMap.put("user1", new User("admin1", "admin1111", 21)); userMap.put("user2", new User("admin2", "admin2222", 22)); userMap.put("user3", new User("admin3", "admin3333", 23)); model.addAttribute(userMap); model.addAttribute("map", userMap); return "userInfo"; }
|
- jsp代码
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
| <body> <h2> <font color="red"><strong>遍历Map集合</strong></font> </h2> <table border="1"> <tr> <th>key</th> <th>value</th> <th>账号</th> <th>密码</th> <th>年龄</th> </tr> <!-- 遍历对象list --> <c:forEach var="item" items="${map}"> <tr> <td><c:out value="${item.key}"></c:out></td> <td><c:out value="${item.value}"></c:out></td> <td><c:out value="${item.value.account}"></c:out></td> <td><c:out value="${item.value.password}"></c:out></td> <td><c:out value="${item.value.age}"></c:out></td> </tr> </c:forEach> </table> </body>
|
Array数组
- java代码
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
|
@RequestMapping("/arrayType") public String arrayType(Model model) { String[] strArr = new String[5]; for (int i = 0; i < strArr.length; i++) { strArr[i] = String.valueOf(i * 2); } model.addAttribute(strArr); model.addAttribute("arr",strArr); return "userInfo"; }
|
- jsp代码
1 2 3 4 5 6 7 8
| <body> <!-- 遍历数组 --> <c:forEach var="item" items="${arr}"> <tr> <td><c:out value="${item}"></c:out></td> </tr> </c:forEach> </body>
|