借助 org.springframework.ui.Model 对象或 Map 对象将信息传到 springmvc 的页面中
需要:jstl 支持 和 EL 表达式
1.maven 添加依赖:
1
2
3
4
5
6
|
<!-- jstl(jsp 标准标签库) --> ;dependency> < groupId >jstl</ groupId > < artifactId >jstl</ artifactId > < version >1.2</ version > ;/dependency> |
2.controller 中的方法
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
|
/** * */ @Controller @RequestMapping("/hello") public class HelloController { /** * 请求地址:项目名/hello/userinfo * @return */ @RequestMapping("userinfo") public String userinfo(org.springframework.ui.Model model,Map< String , Object> map){ //传递单个对象 UserInfoModel info = new UserInfoModel("张三",99,"男"); model.addAttribute("info",info); List< UserInfoModel > list = new ArrayList< UserInfoModel >(); list.add(new UserInfoModel("李四",20,"男")); list.add(new UserInfoModel("王五",21,"女")); list.add(new UserInfoModel("赵六",22,"男")); //传递多个对象 map.put("user_list", list); return "hello/userinfo"; //servlet.xml 中定义的 前缀(prefix)+返回值+后缀(suffix)拼接的物理地址 } } |
3.测试实体对象
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
41
42
43
|
package demo.models.hello; import java.util.Date; /** * */ public class UserInfoModel { public UserInfoModel(String name,int age,String sex){ this.name=name; this.age=age; this.sex=sex; this.date = new Date(); } String name; int age; String sex; Date date; public Date getDate() { return date; } public void setDate(Date date) { this.date = date; } public String getName() { return name; } public void setName(String name) { this.name = name; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } public String getSex() { return sex; } public void setSex(String sex) { this.sex = sex; } } |
4.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
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
|
<%@ page language="java" contentType="text/html; charset=utf-8" pageEncoding="utf-8"%> <!-- 引入jstl core库 ,使用 循环标签--> <%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%> <!-- 引入jstl fmt库,格式化时间标签 --> <%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %> <!-- 开启 EL 表达式支持 --> <%@ page isELIgnored="false"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> < html > < head > < meta http-equiv = "Content-Type" content = "text/html; charset=utf-8" > < title >Insert title here</ title > < script scr = "/content/index.js" ></ script > < link href = "/content/index.css" rel = "external nofollow" /> </ head > < body > < h3 >页面信息</ h3 > < div >url:${pageContext.request.contextPath}</ div > < h3 >用户信息</ h3 > < div >name:${info.name}</ div > < div >age:${info.age}</ div > < div >sex:${info.sex}</ div > < div >date:< fmt:formatDate pattern = "yyyy-MM-dd HH:mm:ss" value = "${info.date}" /></ div > < h3 >用户列表</ h3 > < table border = "1" > < tr > < td >name</ td > < td >age</ td > < td >sex</ td > < td >date</ td > </ tr > < c:forEach items = "${user_list}" var = "p" > < tr > < td >${p.name}</ td > < td >${p.age}</ td > < td >${p.sex}</ td > < td >< fmt:formatDate pattern = "yyyy-MM-dd HH:mm:ss" value = "${p.date}" /></ td > </ tr > </ c:forEach > < tr > < td colspan = "4" style = "text-align: center;" >jstl forEach</ td > </ tr > < tr > < td >行号</ td > < td >col1</ td > < td >col2</ td > < td >col3</ td > </ tr > < c:forEach begin = "1" end = "5" var = "i" > < tr > < td >${i}</ td > < td >${i+2}</ td > < td >${i+3}</ td > < td >${i+4}</ td > </ tr > </ c:forEach > </ table > </ body > </ html > |
以上这篇java-jsp springmvc-controller 传值到页面的方法就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持服务器之家。
原文链接:https://blog.csdn.net/xxj_jing/article/details/68488174