SpringMVC框架中的ModelAndView
来源:互联网 发布:淘宝主图防盗 编辑:程序博客网 时间:2024/06/06 19:50
通过前面两篇博客我们已经学习到SpringMVC框架快速搭建和@RequestMapping注解的用法,今天我们来介绍一下SpringMVC框架中的ModelAndView。我个人理解对于快速入门SpringMVC可以按照如下思路学习
- SpringMVC框架环境快速搭建
- @RequestMapping的用法
- ModelAndView的用法
- 整合Spring+SpringMVC+MyBatis
- 然后在学习SpringMVC框架高级部分
1. ModelAndView是什么以及它的作用是什么
- 简单理解它是将后台返回的数据传递给View层,同时包含一个要访问的View层的URL地址
- 当控制器处理完请求后,通常控制器会将包含视图名称以及一些模型属性的ModelAndView对象返回给DispatcherServlet。因此,在控制器中会构造一个ModelAndView对象
- ModelAndView作用
- 设置转向地址
- 将底层获取的数据进行存储(或者封装)
- 最后将数据传递给View
2. ModelAndView的第一种用法,先创建ModelAndView对象,再通过它的方法去设置数据与转发的视图名
package com.gxa.spmvc.controller;import java.io.IOException;import java.io.PrintWriter;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;import org.springframework.stereotype.Controller;import org.springframework.web.bind.annotation.PathVariable;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.bind.annotation.RequestParam;import org.springframework.web.bind.annotation.ResponseBody;import org.springframework.web.servlet.ModelAndView;import com.gxa.spmvc.entity.Student;/** * SpringMVC的控制器(业务控制器) * 定义的方法就是一个请求处理的方法 * @author caleb * */@Controller@RequestMapping("/user")public class TestController { /** * 利用ModelAndView来转发数据,给前端视图 * @return */ @RequestMapping("/m06") public ModelAndView m06() { ModelAndView modelAndView = new ModelAndView(); modelAndView.setViewName("m06"); modelAndView.addObject("message", "Hello World, Hello Kitty"); return modelAndView; } }
- setViewName(String viewName):Set a view name for this ModelAndView, to be resolved by the DispatcherServlet via a ViewResolver
- addObject(String attributeName, Object attributeValue):通过key/value的方式绑定数据
3. ModelAndView的第二种方法,可以直接通过带有参数的构造方法 ModelAndView(String viewName, String attributeName, Object attributeValue) 来返回数据与转发的视图名
package com.gxa.spmvc.controller;import java.io.IOException;import java.io.PrintWriter;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;import org.springframework.stereotype.Controller;import org.springframework.web.bind.annotation.PathVariable;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.bind.annotation.RequestParam;import org.springframework.web.bind.annotation.ResponseBody;import org.springframework.web.servlet.ModelAndView;import com.gxa.spmvc.entity.Student;/** * SpringMVC的控制器(业务控制器) * 定义的方法就是一个请求处理的方法 * @author caleb * */@Controller@RequestMapping("/user")public class TestController { /** * 利用ModelAndView来转发数据,给前端视图 * @return */ @RequestMapping("/m07") public ModelAndView m07() { return new ModelAndView("m07", "message", "Hello World"); } }
4. ModelAndView的第三种用法,设置重定向
package com.gxa.spmvc.controller;import java.io.IOException;import java.io.PrintWriter;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;import org.springframework.stereotype.Controller;import org.springframework.web.bind.annotation.PathVariable;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.bind.annotation.RequestParam;import org.springframework.web.bind.annotation.ResponseBody;import org.springframework.web.servlet.ModelAndView;import com.gxa.spmvc.entity.Student;/** * SpringMVC的控制器(业务控制器) * 定义的方法就是一个请求处理的方法 * @author caleb * */@Controller@RequestMapping("/user")public class TestController { /** * ModelAndView默认转发 * ModelAndView还是可以设置重定向 * 1. 重定向另一个控制器 * 2. 重定向具体的jsp页面 * @param name * @return */ @RequestMapping("/{name}/m07") public ModelAndView m07(@PathVariable String name) { if (!"admin".equals(name)) { return new ModelAndView("redirect:/m07.jsp"); } return new ModelAndView("m07"); } }
阅读全文
0 0
- SpringMVC框架中的ModelAndView
- springMVC+Mybatis中的ModelMap与ModelAndView
- springMVC中的Model和ModelAndView的区别
- springMVC ModelAndView
- 【SpringMVC】ModelAndView
- springMvc ModelAndView
- springmvc modelandview
- SpringMVC框架中ModelAndView、Model、ModelMap的区别与使用
- springMVC框架下jsp取不到ModelAndView值的情况
- Springmvc中的return modelAndView和return "xxxx"的区别
- Springmvc中的return modelAndView和return "xxxx"的区别
- SpringMVC中JSP取不到ModelAndView中的值
- SpringMVC ModelAndView跳转失效
- springMVC 的modelandView
- SpringMVC返回ModelAndView
- ModelAndView + springMVC form:form
- springmvc ModelAndView 跳转失灵
- springMVC 的 ModelAndView 用法
- 接口
- pip install pyamf failed with exit status 1120
- tomcat启动报Initializing c3p0 pool... com.mchange.v2.c3p0.ComboPooledDataSource异常
- mac git和github
- sqlite3
- SpringMVC框架中的ModelAndView
- 蓝桥--芯片测试
- ios本地持久化存储
- iOS开发UI篇—Quartz2D(五) 图片裁剪圆形,UIKit的绘图方法,Path路径的使用
- 宏定义小程序(写一个宏可以将一个数字的奇数位和偶数位交换)
- 数组越界
- 【每日一题(16)】毕业设计选题题 2017年浙江工业大学迎新赛热身赛-A
- Jpush集成
- Android module library中添加aar包的那些坑