SpringMVC中ModelAndView用法小結(jié)
ModelAndView 作用
1.返回到指定的頁面
ModelAndView構(gòu)造方法可以指定返回的頁面名稱
例:return new ModelAndView(“redirect:/m07.jsp”);
通過setViewName()方法跳轉(zhuǎn)到指定的頁面
例:mav.setViewName(“hello”);
2.返回參數(shù)到指定頁面的request作用于中
使用addObject()設(shè)置需要返回的值,addObject()有幾個不同參數(shù)的方法,可以默認(rèn)和指定返回對象的名字,參數(shù)會返回到新頁面的request作用域中
ModelAndView 的3種用法
1.ModelAndView的第一種用法,先創(chuàng)建ModelAndView對象,再通過它的方法去設(shè)置數(shù)據(jù)與轉(zhuǎn)發(fā)的視圖名
- setViewName(String viewName):設(shè)置此 ModelAndView 的視圖名稱, 由 DispatcherServlet 通過 ViewResolver 解析
- addObject(String attributeName, Object attributeValue):通過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ù)控制器) * 定義的方法就是一個請求處理的方法 * @author caleb * */ @Controller @RequestMapping("/user") public class TestController { /** * 利用ModelAndView來轉(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的第二種方法,可以直接通過帶有參數(shù)的構(gòu)造方法 ModelAndView(String viewName, String attributeName, Object attributeValue) 來返回數(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ù)控制器) * 定義的方法就是一個請求處理的方法 * @author caleb * */ @Controller @RequestMapping("/user") public class TestController { /** * 利用ModelAndView來轉(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ù)控制器) * 定義的方法就是一個請求處理的方法 * @author caleb * */ @Controller @RequestMapping("/user") public class TestController { /** * ModelAndView默認(rèn)轉(zhuǎn)發(fā) * ModelAndView還是可以設(shè)置重定向 * 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使用實(shí)例
要點(diǎn):
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");//跳轉(zhuǎn)新的頁面名稱 mav.addObject("address", "中國廣東省廣州市");//傳入request作用域參數(shù) return mav; } }
跳轉(zhuǎn)前jsp頁面鏈接設(shè)置
<a href="mvc/testModelAndView" rel="external nofollow" >Test ModelAndView</a>
跳轉(zhuǎn)后jsp頁面以及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)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
- SpringMVC中Model和ModelAndView的EL表達(dá)式取值方法
- SpringMVC的ModelAndView傳值方法
- SpringMVC ModelAndView的用法使用詳解
- springmvc處理模型數(shù)據(jù)ModelAndView過程詳解
- Springmvc ModelAndView原理及用法詳解
- SpringMVC通過模型視圖ModelAndView渲染視圖的實(shí)現(xiàn)
- SpringMvc返回modelandview返回的頁面無法跳轉(zhuǎn)問題及解決
- SpringMVC數(shù)據(jù)頁響應(yīng)ModelAndView實(shí)現(xiàn)頁面跳轉(zhuǎn)
- SpringMVC中ModelAndView的使用及說明
相關(guān)文章
Spring中@Autowired @Resource @Inject三個注解有什么區(qū)別
在我們使用Spring框架進(jìn)行日常開發(fā)過程中,經(jīng)常會使用@Autowired, @Resource, @Inject注解來進(jìn)行依賴注入,下面來介紹一下這三個注解有什么區(qū)別2023-03-03SpringBoot+WebSocket實(shí)現(xiàn)IM及時通訊的代碼示例
項目中碰到需要及時通訊的場景,使用springboot集成websocket,即可實(shí)現(xiàn)簡單的及時通訊,本文介紹springboot如何集成websocket、IM及時通訊需要哪些模塊、開發(fā)和部署過程中遇到的問題、以及實(shí)現(xiàn)小型IM及時通訊的代碼,需要的朋友可以參考下2023-10-10深入解析Java的設(shè)計模式編程中建造者模式的運(yùn)用
這篇文章主要介紹了深入解析Java的設(shè)計模式編程中建造者模式的運(yùn)用,同時文中也介紹了建造者模式與工廠模式的區(qū)別,需要的朋友可以參考下2016-02-02idea顯示springboot多服務(wù)啟動界面service操作
這篇文章主要介紹了idea顯示springboot多服務(wù)啟動界面service操作,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-09-09淺談byte和長度為8的boolean數(shù)組互相轉(zhuǎn)換
下面小編就為大家?guī)硪黄獪\談byte和長度為8的boolean數(shù)組互相轉(zhuǎn)換。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2016-11-11Java應(yīng)用服務(wù)器之tomcat會話復(fù)制集群配置的示例詳解
這篇文章主要介紹了Java應(yīng)用服務(wù)器之tomcat會話復(fù)制集群配置的相關(guān)知識,本文通過圖文并茂的形式給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2020-07-07