Spring MVC中ModelAndView的用法

通過前面兩篇文章(Spring MVC入門、Spring MVC中@RequestMapping用法)我們已經學習到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對象,再通過它的方法去設置數據與轉發的視圖名

<code>package com.zlt.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.zlt.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;
}

}/<code>
  • 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) 來返回數據與轉發的視圖名

<code>package com.zlt.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.zlt.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");
}

}/<code>


4. ModelAndView的第三種用法

設置重定向

<code>package com.zlt.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.zlt.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");
}

}/<code>

本系列教程源碼下載歡迎私聊獲取~



分享到:


相關文章: