關(guān)于SpringMVC請求域?qū)ο蟮臄?shù)據(jù)共享問題
SpringMVC支持路徑中的占位符。
可以通過路徑的方式來傳參。restful風(fēng)格。使用{}
做占位符在路徑中指定參數(shù),使用@PathVariable
注解在參數(shù)列表中指定。
<a th:href="@{/test/1}">傳了參數(shù)</a> @RequestMapping("/test/{id}") public String test(@PathVariable("id")Integer id){ System.out.println(id); return "index"; }
如果使用了占位符則請求地址必須有值,否則會報404錯誤。
獲取請求參數(shù)
使用ServletAPI獲?。ɑ静挥茫?/p>
@RequestMapping("/testParam") public String Param(HttpServletRequest request){ String userName = request.getParameter("userName"); String password = request.getParameter("password"); return "index"; }
通過控制器的形參獲?。ūWC參數(shù)名相同的情況下)牛逼
<a th:href="@{/testParam(username='admin',password='123')}">傳了參數(shù)</a> @RequestMapping("/testParam") public String testParam(String username,String password){ System.out.println("username:"+username+",password:"+password); return "index"; }
RequestParam
請求參數(shù)和控制器形參創(chuàng)建映射關(guān)系。
- Value
- Required
- DefaultValue
使用實體類接受請求參數(shù)
@RequestMapping("/testPojo") public String testPojo(User user){ System.out.println(user); return "index"; }
配置過濾器,處理亂碼問題
<filter> <filter-name>CharacterEncodingFilter</filter-name> <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class> <!--設(shè)置字符集--> <init-param> <param-name>encoding</param-name> <param-value>UTF-8</param-value> </init-param> <!--強制響應(yīng)字符集--> <init-param> <param-name>forceResponseEncoding</param-name> <param-value>true</param-value> </init-param> </filter> <filter-mapping> <filter-name>CharacterEncodingFilter</filter-name> <url-pattern>/*</url-pattern> </filter-mapping>
域?qū)ο蠊蚕頂?shù)據(jù)
使用原生ServletAPI向request域?qū)ο蠊蚕頂?shù)據(jù)(不用)
@RequestMapping("/test") public String test(HttpServletRequest request){ request.setAttribute("hello","hello"); return "index"; }
使用ModelAndView
對象
返回值類型為ModelAndView
//使用ModelAndView對象的方式 @RequestMapping("/") public ModelAndView toIndex(HttpServletRequest request){ ModelAndView mav = new ModelAndView(); //設(shè)置共享數(shù)據(jù) mav.addObject("result","mavResult"); //設(shè)置視圖名稱 //視圖名稱=邏輯視圖名稱。 mav.setViewName("index"); return mav; }
使用Model
對象
Model是一個接口,因此不能像ModelAndView那樣去new。
//使用Model對象的方式 @RequestMapping("/") public String toIndexModel(Model model){ //設(shè)置共享數(shù)據(jù) model.addAttribute("result","ModelResult"); return "index"; }
使用Map集合
//使用Map對象的方式 @RequestMapping("/") public String toIndexModel(Map<String,Object> map){ //設(shè)置共享數(shù)據(jù) map.put("result","MapResult"); return "index"; }
使用ModelMap
ModelMap的實例是由mvc框架自動創(chuàng)建并作為控制器方法參數(shù)傳入,無需也不能自己創(chuàng)建。
如自己創(chuàng)建,則無法共享數(shù)據(jù)。
//使用ModelMap對象的方式 @RequestMapping("/") public String toIndexModel(ModelMap modelMap){ //設(shè)置共享數(shù)據(jù) modelMap.addAttribute("result","ModelMapResult"); return "index"; }
到此這篇關(guān)于SpringMVC請求域?qū)ο蟮臄?shù)據(jù)共享的文章就介紹到這了,更多相關(guān)SpringMVC請求域?qū)ο髢?nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
- SpringMVC詳解如何映射請求數(shù)據(jù)
- Java超詳細(xì)講解SpringMVC如何獲取請求數(shù)據(jù)
- SpringMVC 重新定向redirect請求中攜帶數(shù)據(jù)方式
- 使用springmvc的controller層獲取到請求的數(shù)據(jù)方式
- Springmvc獲取前臺請求數(shù)據(jù)過程解析
- Springmvc處理ajax請求并返回json數(shù)據(jù)
- SpringMVC 跨重定向請求傳遞數(shù)據(jù)的方法實現(xiàn)
- SpringMVC解析JSON請求數(shù)據(jù)問題解析
- SpringMVC請求數(shù)據(jù)詳解講解
相關(guān)文章
mybatisPlus條件構(gòu)造器常用方法小結(jié)
這篇文章主要介紹了mybatisPlus條件構(gòu)造器常用方法,首先是.select和其他條件,本文結(jié)合示例代碼給大家介紹的非常詳細(xì),需要的朋友可以參考下2022-10-10Spring Boot2+JPA之悲觀鎖和樂觀鎖實戰(zhàn)教程
這篇文章主要介紹了Spring Boot2+JPA之悲觀鎖和樂觀鎖實戰(zhàn)教程,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2021-10-10spring boot-2.1.16整合swagger-2.9.2 含yml配置文件的代碼詳解
這篇文章主要介紹了spring boot-2.1.16整合swagger-2.9.2 含yml配置文件,本文通過實例代碼給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2020-08-08