學(xué)習(xí)的最大理由是想擺脫平庸,早一天就多一份人生的精彩;遲一天就多一天平庸的困擾。各位小伙伴,如果您:
想系統(tǒng)/深入學(xué)習(xí)某技術(shù)知識點(diǎn)…
一個(gè)人摸索學(xué)習(xí)很難堅(jiān)持,想組團(tuán)高效學(xué)習(xí)…
想寫博客但無從下手,急需寫作干貨注入能量…
熱愛寫作,愿意讓自己成為更好的人…
前言
一、SpringMVC獲取請求參數(shù)
1、通過ServletAPI獲取
2、通過控制器方法的形參獲取請求參數(shù)
3、@RequestParam
4、@RequestHeader
5、@CookieValue
6、通過POJO獲取請求參數(shù)
7、解決獲取請求參數(shù)的亂碼問題
二、域?qū)ο蠊蚕頂?shù)據(jù)
1、使用ServletAPI向request域?qū)ο蠊蚕頂?shù)據(jù)
2、使用ModelAndView向request域?qū)ο蠊蚕頂?shù)據(jù)
3、使用Model向request域?qū)ο蠊蚕頂?shù)據(jù)
4、使用map向request域?qū)ο蠊蚕頂?shù)據(jù)
5、使用ModelMap向request域?qū)ο蠊蚕頂?shù)據(jù)
6、Model、ModelMap、Map的關(guān)系
7、向session域共享數(shù)據(jù)
8、向application域共享數(shù)據(jù)
一、SpringMVC獲取請求參數(shù)
1、通過ServletAPI獲取
將HttpServletRequest作為控制器方法的形參,此時(shí)HttpServletRequest類型的參數(shù)表示封裝了當(dāng)前請求的請求報(bào)文的對象
@RequestMapping("/testParam")
public String testParam(HttpServletRequest request){
String username = request.getParameter("username");
String password = request.getParameter("password");
System.out.println("username:"+username+",password:"+password);
return "success";
}
2、通過控制器方法的形參獲取請求參數(shù)
在控制器方法的形參位置,設(shè)置和請求參數(shù)同名的形參,當(dāng)瀏覽器發(fā)送請求,匹配到請求映射時(shí),在DispatcherServlet中就會將請求參數(shù)賦值給相應(yīng)的形參
<a th:href="@{/testParam(username='admin',password=123456)}">測試獲取請求參數(shù)-->/testParam</a><br>
@RequestMapping("/testParam")
public String testParam(String username, String password){
System.out.println("username:"+username+",password:"+password);
return "success";
}
注:
若請求所傳輸?shù)恼埱髤?shù)中有多個(gè)同名的請求參數(shù),此時(shí)可以在控制器方法的形參中設(shè)置字符串?dāng)?shù)組或者字符串類型的形參接收此請求參數(shù)
若使用字符串?dāng)?shù)組類型的形參,此參數(shù)的數(shù)組中包含了每一個(gè)數(shù)據(jù)
若使用字符串類型的形參,此參數(shù)的值為每個(gè)數(shù)據(jù)中間使用逗號拼接的結(jié)果
3、@RequestParam
@RequestParam是將請求參數(shù)和控制器方法的形參創(chuàng)建映射關(guān)系
@RequestParam注解一共有三個(gè)屬性:
value:指定為形參賦值的請求參數(shù)的參數(shù)名
required:設(shè)置是否必須傳輸此請求參數(shù),默認(rèn)值為true
若設(shè)置為true時(shí),則當(dāng)前請求必須傳輸value所指定的請求參數(shù),若沒有傳輸該請求參數(shù),且沒有設(shè)置defaultValue屬性,則頁面報(bào)錯(cuò)400:Required String parameter ‘xxx’ is not present;若設(shè)置為false,則當(dāng)前請求不是必須傳輸value所指定的請求參數(shù),若沒有傳輸,則注解所標(biāo)識的形參的值為null
defaultValue:不管required屬性值為true或false,當(dāng)value所指定的請求參數(shù)沒有傳輸或傳輸?shù)闹禐?"時(shí),則使用默認(rèn)值為形參賦值
4、@RequestHeader
@RequestHeader是將請求頭信息和控制器方法的形參創(chuàng)建映射關(guān)系
@RequestHeader注解一共有三個(gè)屬性:value、required、defaultValue,用法同@RequestParam
5、@CookieValue
@CookieValue是將cookie數(shù)據(jù)和控制器方法的形參創(chuàng)建映射關(guān)系
@CookieValue注解一共有三個(gè)屬性:value、required、defaultValue,用法同@RequestParam
6、通過POJO獲取請求參數(shù)
可以在控制器方法的形參位置設(shè)置一個(gè)實(shí)體類類型的形參,此時(shí)若瀏覽器傳輸?shù)恼埱髤?shù)的參數(shù)名和實(shí)體類中的屬性名一致,那么請求參數(shù)就會為此屬性賦值
<form th:action="@{/testpojo}" method="post">
用戶名:<input type="text" name="username"><br>
密碼:<input type="password" name="password"><br>
性別:<input type="radio" name="sex" value="男">男<input type="radio" name="sex" value="女">女<br>
年齡:<input type="text" name="age"><br>
郵箱:<input type="text" name="email"><br>
<input type="submit">
</form>
@RequestMapping("/testpojo")
public String testPOJO(User user){
System.out.println(user);
return "success";
}
//最終結(jié)果-->User{id=null, username='張三', password='123', age=23, sex='男', email='123@qq.com'}
7、解決獲取請求參數(shù)的亂碼問題
解決獲取請求參數(shù)的亂碼問題,可以使用SpringMVC提供的編碼過濾器CharacterEncodingFilter,但是必須在web.xml中進(jìn)行注冊
<!--配置springMVC的編碼過濾器-->
<filter>
<filter-name>CharacterEncodingFilter</filter-name>
<filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>UTF-8</param-value>
</init-param>
<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>
注:
SpringMVC中處理編碼的過濾器一定要配置到其他過濾器之前,否則無效
二、域?qū)ο蠊蚕頂?shù)據(jù)
1、使用ServletAPI向request域?qū)ο蠊蚕頂?shù)據(jù)
@RequestMapping("/testServletAPI")
public String testServletAPI(HttpServletRequest request){
request.setAttribute("testScope", "hello,servletAPI");
return "success";
}
2、使用ModelAndView向request域?qū)ο蠊蚕頂?shù)據(jù)
@RequestMapping("/testModelAndView")
public ModelAndView testModelAndView(){
/**
* ModelAndView有Model和View的功能
* Model主要用于向請求域共享數(shù)據(jù)
* View主要用于設(shè)置視圖,實(shí)現(xiàn)頁面跳轉(zhuǎn)
*/
ModelAndView mav = new ModelAndView();
//向請求域共享數(shù)據(jù)
mav.addObject("testScope", "hello,ModelAndView");
//設(shè)置視圖,實(shí)現(xiàn)頁面跳轉(zhuǎn)
mav.setViewName("success");
return mav;
}
3、使用Model向request域?qū)ο蠊蚕頂?shù)據(jù)
@RequestMapping("/testModel")
public String testModel(Model model){
model.addAttribute("testScope", "hello,Model");
return "success";
}
4、使用map向request域?qū)ο蠊蚕頂?shù)據(jù)
@RequestMapping("/testMap")
public String testMap(Map<String, Object> map){
map.put("testScope", "hello,Map");
return "success";
}
5、使用ModelMap向request域?qū)ο蠊蚕頂?shù)據(jù)
@RequestMapping("/testModelMap")
public String testModelMap(ModelMap modelMap){
modelMap.addAttribute("testScope", "hello,ModelMap");
return "success";
}
6、Model、ModelMap、Map的關(guān)系
Model、ModelMap、Map類型的參數(shù)其實(shí)本質(zhì)上都是 BindingAwareModelMap 類型的文章來源:http://www.zghlxwxcb.cn/news/detail-761902.html
public interface Model{}
public class ModelMap extends LinkedHashMap<String, Object> {}
public class ExtendedModelMap extends ModelMap implements Model {}
public class BindingAwareModelMap extends ExtendedModelMap {}
7、向session域共享數(shù)據(jù)
@RequestMapping("/testSession")
public String testSession(HttpSession session){
session.setAttribute("testSessionScope", "hello,session");
return "success";
}
8、向application域共享數(shù)據(jù)
@RequestMapping("/testApplication")
public String testApplication(HttpSession session){
ServletContext application = session.getServletContext();
application.setAttribute("testApplicationScope", "hello,application");
return "success";
}
總結(jié)
以上就是SpringMVC之獲取請求參數(shù)和域?qū)ο蠊蚕頂?shù)據(jù)的相關(guān)知識點(diǎn),希望對你有所幫助。
積跬步以至千里,積怠惰以至深淵。時(shí)代在這跟著你一起努力哦!文章來源地址http://www.zghlxwxcb.cn/news/detail-761902.html
到了這里,關(guān)于SpringMVC之獲取請求參數(shù)和域?qū)ο蠊蚕頂?shù)據(jù)的文章就介紹完了。如果您還想了解更多內(nèi)容,請?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!