Spring 梳理-传递模型数据到视图
Posted 手握太阳
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Spring 梳理-传递模型数据到视图相关的知识,希望对你有一定的参考价值。
- 传递model,返回string,不指定返回key
-
package com.jt; import com.myOrg.DeptUserDomain; @Controller @RequestMapping(value="/FirstControl") public class HelloControl { @RequestMapping("/viewdud") public String viewDeptUser(Model model){ DeptUserDomain dud=new DeptUserDomain(); dud.setId("1"); dud.setName("zs"); dud.setSalary(10000); dud.setHireDate(new Date(100)); model.addAttribute(dud); return "deptUser"; } }
-
<body> viewDeptUser<br> ${deptUserDomain.id}<br> </body>
-
- 传递model,返回string,指定返回key
-
@RequestMapping("/viewdud") public String viewDeptUser(Model model){ DeptUserDomain dud=new DeptUserDomain(); dud.setId("1"); dud.setName("zs"); dud.setSalary(10000); dud.setHireDate(new Date(100)); model.addAttribute("data",dud); return "deptUser"; }
-
<body> viewDeptUser<br> ${data.id}<br> </body>
-
- 传递map,返回string,必须指定返回key。(可避免被Spring类型的Model污染代码)
-
@RequestMapping("viewdudByMap") public String viewDeptUserByMap(Map map){ DeptUserDomain dud=new DeptUserDomain(); dud.setId("1"); dud.setName("zs"); dud.setSalary(10000); map.put("dataByMap",dud); return "deptUser"; }
<body> viewDeptUser<br> ${dataByMap.id}<br> </body>
-
- 传递空,返回data
-
@Controller
@RequestMapping(value="/FirstControl")
public class HelloControl {
@RequestMapping("/viewdudByReturn")
public DeptUserDomain viewDeptUserByReturn(){
DeptUserDomain dud=new DeptUserDomain();
dud.setId("1");
dud.setName("zs");
dud.setSalary(10000);
return dud;
}
}系统自动根据请求路径进行推断设置,视图名称为viewdudByReturn,jsp路径为 /WEB-INF/FirstControl/viewdudByReturn.jsp ,
相当于返回的String为“/FirstControl/viewdudByReturn”
<body> viewDeptUser<br> ${deptUserDomain.id}<br> </body>
-
以上是关于Spring 梳理-传递模型数据到视图的主要内容,如果未能解决你的问题,请参考以下文章