[刘阳Java]_Spring MVC中ModelAndView的用法_第3讲

通过前面两篇博客我们已经学习到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");
    }
    
}

 源码下载:https://pan.baidu.com/s/1eSDZwFg

posted @ 2017-04-23 12:09  子墨老师  阅读(28706)  评论(2编辑  收藏  举报