JSP使用JSTL从SpringMVC Model对象里取值显示
汇总 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<!-- JSP相关 scop:provided 只在编译和开发使用,发布不用 -->
<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>
<!-- JSP end -->Controller 类
1
2
3
4
5
6
7
8
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/**
* 返回基本类型
* @param model
* @return
*/
public String baseType(Model model) {
//model是用LinkHashMap存储数据,如果没有设置key,则key默认是该值的包装类名称的小写
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/**
* 返回对象类型
* @param model
* @return
*/
public String userInfoPg(Model model) {
User user = new User();
user.setRealName("王小二");
user.setAccount("root");
user.setPassword("root123");
user.setAge(22);
//没有设置key,默认是对象类型名称的小写
// model.addAttribute(user);
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/**
* 返回基本类型List集合
* @param model
* @return
*/
public String baseList(Model model) {
ArrayList<Object> arrayList = new ArrayList<>();
arrayList.add("张三");
arrayList.add("李四");
arrayList.add(30);
arrayList.add(15.67);
arrayList.add(true);
//没有设置key,认是stringList
// model.addAttribute(list);
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/**
* 返回对象List集合
* @param model
* @return
*/
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);
//默认key 是userList
// model.addAttribute(userList);
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/**
* 返回Map集合
* @param model
* @return
*/
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));
//没有设置key,默认key是hashMap
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/**
* 返回数组
* @param model
* @return
*/
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>
JSP使用JSTL从SpringMVC Model对象里取值显示
http://blog.gxitsky.com/2018/01/11/JavaWeb-jsp-getValue-from-springmvc-model/