ModelAndView 作用
1.返回到指定的頁面
ModelAndView構造方法可以指定返回的頁面名稱
例:return new ModelAndView("redirect:/m07.jsp");
通過setViewName()方法跳轉到指定的頁面
例:mav.setViewName("hello");
2.返回參數到指定頁面的request作用於中
使用addObject()設置需要返回的值,addObject()有幾個不同參數的方法,可以默認和指定返回對象的名字,參數會返回到新頁面的request作用域中
ModelAndView 的3種用法
1.ModelAndView的第一種用法,先創建ModelAndView對象,再通過它的方法去設置數據與轉發的視圖名
setViewName(String viewName):設置此 ModelAndView 的視圖名稱, 由 DispatcherServlet 通過 ViewResolver 解析
addObject(String attributeName, Object attributeValue):通過key/value的方式綁定數據
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;
}
}
2.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");
}
}
3.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");
}
}
ModelAndView使用實例
要點:
1.@RequestMapping 注解的使用
2.modelandview 的使用
3.jsp頁面request作用域的取值
4.視圖解析器配置
ModelAndView 使用代碼
package com.dgr.controller;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;
@RequestMapping("mvc")
@Controller
public class TestRequestMMapping {
@RequestMapping(value="/testModelAndView")
public ModelAndView testModelAndView(){
ModelAndView mav = new ModelAndView();
mav.setViewName("hello");//跳轉新的頁面名稱
mav.addObject("address", "中國廣東省廣州市");//傳入request作用域參數
return mav;
}
}
跳轉前jsp頁面鏈接設置
<a href="mvc/testModelAndView">Test ModelAndView</a>
跳轉后jsp頁面以及request作用於取值
<title>New Page</title>
</head>
<body>
<h1>ModelAndView 跳轉</h1>
<br>
${requestScope.address}
<br>
${address }
<br>
</body>
視圖解析器配置