Spring 向页面传值的方式以及返回类型
来源:互联网 发布:尤克里里谱子软件 编辑:程序博客网 时间:2024/05/21 19:34
当Controller组件处理后,向jsp页面传值,
1:使用HttpServletRequest 和 Session 然后setAttribute(),就和Servlet中一样
2:使用ModelAndView对象
3:使用ModelMap对象
4:使用@ModelAttribute注解
ModelAndView:
ModelAndView有两个作用,通过ModelAndView构造方法可以指定返回的页面名称,也可以通过setViewName()方法跳转到指定的页面。用于传递控制方法处理结果数据到结果页面,也就是说我们把需要在结果页面上需要的数据放到ModelAndView对象中即可,他的作用类似于request对象的setAttribute方法的作用,用来在一个请求过程中传递处理的数据。通过以下方法向页面传递参数: addObject(String key,Object value);
@RequestMapping("/test") public ModelAndView show1(HttpServletRequest request, HttpServletResponse response) throwsException { ModelAndView modelAndView = new ModelAndView("/test"); modelAndView.addObject("type", "type"); return modelAndView;}
ModelMap :
ModelMap对象主要用于传递控制方法处理数据到结果页面,也就是说我们把结果页面上需要的数据放到ModelMap对象中即可,他的作用类似于request对象的setAttribute方法的作用,用来在一个请求过程中传递处理的数据。ModelMap数据会利用HttpServletRequest的Attribute传值到页面中。
@RequestMapping("/test")public String test(ModelMap modelMap){ modelMap.addAttribute("name3", "name3"); modelMap.put("name4", "name4"); return "test";}
ModelMap API:http://www.apihome.cn/api/spring/ModelMap.html
Model :
Model 是一个接口, 其实现类为ExtendedModelMap,继承了ModelMap类。Model数据会利用HttpServletRequest的Attribute传值到页面中。
@RequestMapping("/test")public String test(Model model){ model.addAttribute("name1", "name1"); return "test"";}
@ModelAttribute:
在Controller方法的参数部分或Bean属性方法上使用@ModelAttribute数据会利用HttpServletRequest的Attribute传值到页面中。被@ModelAttribute注释的方法会在此controller每个方法执行前被执行,因此对于一个controller映射多个URL的用法来说,要谨慎使用。
@RequestMapping("/test") public String login(@ModelAttribute("user") User user){ return "test"; } @ModelAttribute("name") public String getName(){ return name; }Session:
利用HttpServletReequest的getSession()方法
@RequestMapping("/test") public String login(HttpServletRequest request){ HttpSession session = request.getSession(); session.setAttribute("user",user); return "test"; }自定义Map:
@ResponseBody@RequestMapping(value = "/testMap", method = RequestMethod.POST)public Map<String, Object> testMap(HttpServletRequest request) { Map<String, Object> result = new HashMap<String, Object>(); boolean flag = false; .... result.put("status", flag); return result;}
Spring MVC 默认采用的是转发来定位视图,如果要使用重定向,可以如下操作
1:使用RedirectView
public ModelAndView test(){ RedirectView view = new RedirectView("test"); return new ModelAndView(view); }2:使用redirect:前缀
public String test(){ //TODO return "redirect:test"; }
spring mvc处理方法支持如下的返回方式:ModelAndView, Model, ModelMap, Map,View, String, void
- Spring 向页面传值的方式以及返回类型
- Spring 向页面传值以及接受页面传过来的参数的方式
- 向页面传值的方式
- 向页面传值的方式
- springMvc向页面传值和读取的方式
- MVC 向页面传值方式
- spring mvc controller返回值的类型
- Spring mvc转向以及设置返回类型
- Spring Security4.1.3实现拦截登录后向登录页面跳转方式(redirect或forward)返回被拦截界面
- Spring Security4.1.3实现拦截登录后向登录页面跳转方式(redirect或forward)返回被拦截界面
- jQuery Ajax向某个页面传值并取得返回的数组
- 返回页面的处理方式
- springmvc中下载的两种方式,以及向页面传递流数据
- spring mvc返回值类型
- spring mvc返回值类型
- Controller向前端传值以及跳转页面
- JSP页面向action传值方式总结
- hibernate的查询方法以及返回的值类型
- 日常汇总
- CentOS下添加用户并且让用户获得root权限
- Tomcat的生命周期管理简要分析
- windows service 安装
- 电影介绍
- Spring 向页面传值的方式以及返回类型
- UIView的Text属性调用不出来
- CSS3 中的伪类选择器
- Java中的四种权限
- android反射机制
- U9 UI插件中获取Btn按钮事件
- hibernate 查询的用法
- windows C++ 控制台程序不显示黑框的办法
- 3.2 js六大数据类型