王也諸葛青sem優(yōu)化軟件選哪家
學(xué)習(xí)的最大理由是想擺脫平庸,早一天就多一份人生的精彩;遲一天就多一天平庸的困擾。各位小伙伴,如果您:
想系統(tǒng)/深入學(xué)習(xí)某技術(shù)知識(shí)點(diǎn)…
一個(gè)人摸索學(xué)習(xí)很難堅(jiān)持,想組團(tuán)高效學(xué)習(xí)…
想寫博客但無(wú)從下手,急需寫作干貨注入能量…
熱愛(ài)寫作,愿意讓自己成為更好的人…
文章目錄
- 前言
- 一、SpringMVC獲取請(qǐng)求參數(shù)
- 1、通過(guò)ServletAPI獲取
- 2、通過(guò)控制器方法的形參獲取請(qǐng)求參數(shù)
- 3、@RequestParam
- 4、@RequestHeader
- 5、@CookieValue
- 6、通過(guò)POJO獲取請(qǐng)求參數(shù)
- 7、解決獲取請(qǐng)求參數(shù)的亂碼問(wèn)題
- 二、域?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ù)
- 總結(jié)
前言
一、SpringMVC獲取請(qǐng)求參數(shù)
1、通過(guò)ServletAPI獲取
2、通過(guò)控制器方法的形參獲取請(qǐng)求參數(shù)
3、@RequestParam
4、@RequestHeader
5、@CookieValue
6、通過(guò)POJO獲取請(qǐng)求參數(shù)
7、解決獲取請(qǐng)求參數(shù)的亂碼問(wèn)題
二、域?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獲取請(qǐng)求參數(shù)
1、通過(guò)ServletAPI獲取
將HttpServletRequest作為控制器方法的形參,此時(shí)HttpServletRequest類型的參數(shù)表示封裝了當(dāng)前請(qǐng)求的請(qǐng)求報(bào)文的對(duì)象
@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、通過(guò)控制器方法的形參獲取請(qǐng)求參數(shù)
在控制器方法的形參位置,設(shè)置和請(qǐng)求參數(shù)同名的形參,當(dāng)瀏覽器發(fā)送請(qǐng)求,匹配到請(qǐng)求映射時(shí),在DispatcherServlet中就會(huì)將請(qǐng)求參數(shù)賦值給相應(yīng)的形參
<a th:href="@{/testParam(username='admin',password=123456)}">測(cè)試獲取請(qǐng)求參數(shù)-->/testParam</a><br>
@RequestMapping("/testParam")
public String testParam(String username, String password){System.out.println("username:"+username+",password:"+password);return "success";
}
注:
若請(qǐng)求所傳輸?shù)恼?qǐng)求參數(shù)中有多個(gè)同名的請(qǐng)求參數(shù),此時(shí)可以在控制器方法的形參中設(shè)置字符串?dāng)?shù)組或者字符串類型的形參接收此請(qǐng)求參數(shù)
若使用字符串?dāng)?shù)組類型的形參,此參數(shù)的數(shù)組中包含了每一個(gè)數(shù)據(jù)
若使用字符串類型的形參,此參數(shù)的值為每個(gè)數(shù)據(jù)中間使用逗號(hào)拼接的結(jié)果
3、@RequestParam
@RequestParam是將請(qǐng)求參數(shù)和控制器方法的形參創(chuàng)建映射關(guān)系
@RequestParam注解一共有三個(gè)屬性:
value:指定為形參賦值的請(qǐng)求參數(shù)的參數(shù)名
required:設(shè)置是否必須傳輸此請(qǐng)求參數(shù),默認(rèn)值為true
若設(shè)置為true時(shí),則當(dāng)前請(qǐng)求必須傳輸value所指定的請(qǐng)求參數(shù),若沒(méi)有傳輸該請(qǐng)求參數(shù),且沒(méi)有設(shè)置defaultValue屬性,則頁(yè)面報(bào)錯(cuò)400:Required String parameter ‘xxx’ is not present;若設(shè)置為false,則當(dāng)前請(qǐng)求不是必須傳輸value所指定的請(qǐng)求參數(shù),若沒(méi)有傳輸,則注解所標(biāo)識(shí)的形參的值為null
defaultValue:不管required屬性值為true或false,當(dāng)value所指定的請(qǐng)求參數(shù)沒(méi)有傳輸或傳輸?shù)闹禐?#34;"時(shí),則使用默認(rèn)值為形參賦值
4、@RequestHeader
@RequestHeader是將請(qǐng)求頭信息和控制器方法的形參創(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、通過(guò)POJO獲取請(qǐng)求參數(shù)
可以在控制器方法的形參位置設(shè)置一個(gè)實(shí)體類類型的形參,此時(shí)若瀏覽器傳輸?shù)恼?qǐng)求參數(shù)的參數(shù)名和實(shí)體類中的屬性名一致,那么請(qǐng)求參數(shù)就會(huì)為此屬性賦值
<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、解決獲取請(qǐng)求參數(shù)的亂碼問(wèn)題
解決獲取請(qǐng)求參數(shù)的亂碼問(wèn)題,可以使用SpringMVC提供的編碼過(guò)濾器CharacterEncodingFilter,但是必須在web.xml中進(jìn)行注冊(cè)
<!--配置springMVC的編碼過(guò)濾器-->
<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中處理編碼的過(guò)濾器一定要配置到其他過(guò)濾器之前,否則無(wú)效
二、域?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主要用于向請(qǐng)求域共享數(shù)據(jù)* View主要用于設(shè)置視圖,實(shí)現(xiàn)頁(yè)面跳轉(zhuǎn)*/ModelAndView mav = new ModelAndView();//向請(qǐng)求域共享數(shù)據(jù)mav.addObject("testScope", "hello,ModelAndView");//設(shè)置視圖,實(shí)現(xiàn)頁(yè)面跳轉(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 類型的
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之獲取請(qǐng)求參數(shù)和域?qū)ο蠊蚕頂?shù)據(jù)的相關(guān)知識(shí)點(diǎn),希望對(duì)你有所幫助。
積跬步以至千里,積怠惰以至深淵。時(shí)代在這跟著你一起努力哦!