当Controller组件处理后,需要向jsp传值时,用下面方法:
1.直接使用HttpServletRequest和Session
2.使用ModeAndView对象
3.使用ModelMap(建模)参数对象
4.使用@ModelAttribute注解
直接使用HttpServletRequest和Session Session存储
@RequestMappings("/login-action5.from")
publica String CheckLogin5(String name,String password,
ModelMap model,HttpServletRequest request){
User user=userService.login(name,password);
request.getSession().setAttribute("loginUser",user);
model.addAttribute("user",user);
return "success";
@RequestMapping("/test7.do")
public ModelAndView test7(HttpServletRequest request, User user) {
HttpSession session = request.getSession();
session.setAttribute("langs", "bigdata");
return new ModelAndView("jsp/hello");
}
1.使用ModelAndView传出数据
// 1.使用ModelAndView传出数据
@RequestMapping("/test4.do")
public ModelAndView test4() {
Map<String, Object> data = new HashMap<String, Object>();
data.put("success", "true");
data.put("message", "操作成功");
return new ModelAndView("jsp/hello", data);
}
2.使用ModelMap传出数据
// 2.使用ModelMap传出数据
@RequestMapping("/test5.do")
public ModelAndView test5(ModelMap model) {
model.addAttribute("success", false);
model.addAttribute("message", "操作失败");
return new ModelAndView("jsp/hello");
}
3.使用@ModelAttribute传出bean属性
// 3.使用@ModelAttribute传出bean属性
@ModelAttribute("age")
public int getAge() {
return 18;
}
4.使用@ModelAttribute传出参数值
// 4.使用@ModelAttribute传出参数值
@RequestMapping("/test6.do")
public ModelAndView test6(@ModelAttribute("userName") String username, String password) {
return new ModelAndView("jsp/hello");
}
5.使用Session
// 5.使用Session
@RequestMapping("/test7.do")
public ModelAndView test7(HttpServletRequest request, User user) {
HttpSession session = request.getSession();
session.setAttribute("langs", "bigdata");
return new ModelAndView("jsp/hello");
}
6. 返回String:视图信息
// 6.返回String:视图信息
@RequestMapping("/test8.do")
public String test8(User user, Model model) {
model.addAttribute("user", user);
return "jsp/hello";
}
7.系统错误页面
//7.系统错误页面
@RequestMapping("/test9.do")
public String test9() {
return "jsp/error";
}