SpringMVC中ModelAndView用法小結(jié)
ModelAndView 作用
1.返回到指定的頁(yè)面
ModelAndView構(gòu)造方法可以指定返回的頁(yè)面名稱
例:return new ModelAndView(“redirect:/m07.jsp”);
通過(guò)setViewName()方法跳轉(zhuǎn)到指定的頁(yè)面
例:mav.setViewName(“hello”);
2.返回參數(shù)到指定頁(yè)面的request作用于中
使用addObject()設(shè)置需要返回的值,addObject()有幾個(gè)不同參數(shù)的方法,可以默認(rèn)和指定返回對(duì)象的名字,參數(shù)會(huì)返回到新頁(yè)面的request作用域中
ModelAndView 的3種用法
1.ModelAndView的第一種用法,先創(chuàng)建ModelAndView對(duì)象,再通過(guò)它的方法去設(shè)置數(shù)據(jù)與轉(zhuǎn)發(fā)的視圖名
- setViewName(String viewName):設(shè)置此 ModelAndView 的視圖名稱, 由 DispatcherServlet 通過(guò) ViewResolver 解析
- addObject(String attributeName, Object attributeValue):通過(guò)key/value的方式綁定數(shù)據(jù)
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的控制器(業(yè)務(wù)控制器)
* 定義的方法就是一個(gè)請(qǐng)求處理的方法
* @author caleb
*
*/
@Controller
@RequestMapping("/user")
public class TestController {
/**
* 利用ModelAndView來(lái)轉(zhuǎn)發(fā)數(shù)據(jù),給前端視圖
* @return
*/
@RequestMapping("/m06")
public ModelAndView m06() {
ModelAndView modelAndView = new ModelAndView();
modelAndView.setViewName("m06");
modelAndView.addObject("message", "Hello World, Hello Kitty");
return modelAndView;
}
}2.ModelAndView的第二種方法,可以直接通過(guò)帶有參數(shù)的構(gòu)造方法 ModelAndView(String viewName, String attributeName, Object attributeValue) 來(lái)返回?cái)?shù)據(jù)與轉(zhuǎn)發(fā)的視圖名
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的控制器(業(yè)務(wù)控制器)
* 定義的方法就是一個(gè)請(qǐng)求處理的方法
* @author caleb
*
*/
@Controller
@RequestMapping("/user")
public class TestController {
/**
* 利用ModelAndView來(lái)轉(zhuǎn)發(fā)數(shù)據(jù),給前端視圖
* @return
*/
@RequestMapping("/m07")
public ModelAndView m07() {
return new ModelAndView("m07", "message", "Hello World");
}
}3.ModelAndView的第三種用法,設(shè)置重定向
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的控制器(業(yè)務(wù)控制器)
* 定義的方法就是一個(gè)請(qǐng)求處理的方法
* @author caleb
*
*/
@Controller
@RequestMapping("/user")
public class TestController {
/**
* ModelAndView默認(rèn)轉(zhuǎn)發(fā)
* ModelAndView還是可以設(shè)置重定向
* 1. 重定向另一個(gè)控制器
* 2. 重定向具體的jsp頁(yè)面
* @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使用實(shí)例
要點(diǎn):
1.@RequestMapping 注解的使用
2.modelandview 的使用
3.jsp頁(yè)面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");//跳轉(zhuǎn)新的頁(yè)面名稱
mav.addObject("address", "中國(guó)廣東省廣州市");//傳入request作用域參數(shù)
return mav;
}
}跳轉(zhuǎn)前jsp頁(yè)面鏈接設(shè)置
<a href="mvc/testModelAndView" rel="external nofollow" >Test ModelAndView</a>
跳轉(zhuǎn)后jsp頁(yè)面以及request作用于取值
<title>New Page</title>
</head>
<body>
<h1>ModelAndView 跳轉(zhuǎn)</h1>
<br>
${requestScope.address}
<br>
${address }
<br>
</body>到此這篇關(guān)于SpringMVC中ModelAndView用法小結(jié)的文章就介紹到這了,更多相關(guān)SpringMVC ModelAndView內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
- SpringMVC中Model和ModelAndView的EL表達(dá)式取值方法
- SpringMVC的ModelAndView傳值方法
- SpringMVC ModelAndView的用法使用詳解
- springmvc處理模型數(shù)據(jù)ModelAndView過(guò)程詳解
- Springmvc ModelAndView原理及用法詳解
- SpringMVC通過(guò)模型視圖ModelAndView渲染視圖的實(shí)現(xiàn)
- SpringMvc返回modelandview返回的頁(yè)面無(wú)法跳轉(zhuǎn)問(wèn)題及解決
- SpringMVC數(shù)據(jù)頁(yè)響應(yīng)ModelAndView實(shí)現(xiàn)頁(yè)面跳轉(zhuǎn)
- SpringMVC中ModelAndView的使用及說(shuō)明
相關(guān)文章
深入探索Spring AOP從原理到實(shí)戰(zhàn)操作
本文詳細(xì)介紹了SpringAOP的基本概念、實(shí)現(xiàn)機(jī)制、核心術(shù)語(yǔ)及實(shí)戰(zhàn)應(yīng)用,SpringAOP通過(guò)動(dòng)態(tài)代理機(jī)制,在運(yùn)行時(shí)為目標(biāo)對(duì)象創(chuàng)建代理,實(shí)現(xiàn)對(duì)方法的增強(qiáng),本文探討了SpringAOP的高級(jí)特性,并列舉了日志記錄、事務(wù)管理、權(quán)限控制和性能監(jiān)控等應(yīng)用場(chǎng)景,感興趣的朋友跟隨小編一起看看吧2025-12-12
Java如何獲取@TableField,@TableName注解的值
這篇文章主要介紹了Java如何獲取@TableField,@TableName注解的值,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-01-01
mybatis連接mysql的實(shí)現(xiàn)過(guò)程
通過(guò)配置Maven的pom文件,可以簡(jiǎn)化MyBatis連接數(shù)據(jù)庫(kù)的過(guò)程,免去手動(dòng)下載和導(dǎo)入各種依賴包的麻煩,本文介紹了如何利用Maven導(dǎo)入MyBatis及其他相關(guān)依賴,如Junit、MySQL連接驅(qū)動(dòng)、Druid連接池和Dbutil等,以簡(jiǎn)化數(shù)據(jù)庫(kù)操作和測(cè)試2024-10-10
Java Spring的依賴注入理解及@Autowired用法示例詳解
文章介紹了Spring依賴注入(DI)的概念、三種實(shí)現(xiàn)方式(構(gòu)造器、Setter、字段注入),區(qū)分了@Autowired(注入Bean)與@Value(注入簡(jiǎn)單類型)的用途,并說(shuō)明@Bean方法參數(shù)可自動(dòng)裝配依賴,無(wú)需顯式注解,感興趣的朋友跟隨小編一起看看吧2025-07-07
mybatis plus實(shí)體類中字段映射mysql中的json格式方式
這篇文章主要介紹了mybatis plus實(shí)體類中字段映射mysql中的json格式方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-08-08
SpringBoot 配合 SpringSecurity 實(shí)現(xiàn)自動(dòng)登錄功能的代碼
這篇文章主要介紹了SpringBoot 配合 SpringSecurity 實(shí)現(xiàn)自動(dòng)登錄功能的代碼,代碼簡(jiǎn)單易懂,對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-09-09
MyBatis-Plus中更新操作的兩種實(shí)現(xiàn)
本文主要介紹了MyBatis-Plus中更新操作的兩種實(shí)現(xiàn),主要是通過(guò)id更新和條件更新,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2023-04-04

