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>
-