Table of Contents
1:SpringMVC如何获取请求带来的各种信息 默认方式获取请求参数: 直接给方法入参上写一个和请求参数名相同的变量。这个变量就来接收请求参数的值; 带:有值,没带:null;
2:@RequestParam:获取请求参数的;参数默认是必须带的;
3:@RequestParam("user")和 @PathVariable("user")的区别
1.1:get:现在tomcat已经做了设置,get乱码没问题
1.2:post乱码:在web.xml中增加拦截器,设置request.setCharacterEncoding("UTF-8");
一:SpringMVC获取参数
1:RequestParam
1:SpringMVC如何获取请求带来的各种信息 默认方式获取请求参数: 直接给方法入参上写一个和请求参数名相同的变量。这个变量就来接收请求参数的值; 带:有值,没带:null;
2:@RequestParam:获取请求参数的;参数默认是必须带的;
如:@RequestParam("user") ---String username username =request.getParameter("user")
@Controller
public class ParamsController {
@RequestMapping("/hello")
public String handle01(String name) {
System.out.println("handle01..."+name);
return "success";
}
@RequestMapping("/hello02")
public String handle02(@RequestParam("name1")String str) {
System.out.println("handle02..."+str);
return "success";
}
}
3:@RequestParam("user")和 @PathVariable("user")的区别
RequestParam----从请求参数中获取user,xxx?user=123
PathVariable---是从请求路径中获取user,xxx/{user}
4:RequestParam的属性
- value() :取的参数名
- required() :是否为必填
- defaultValue() :如果没带值,那么取默认值
@RequestMapping("/hello03")
public String handle03(@RequestParam(value = "name",required = false,defaultValue = "666")String str) {
System.out.println("handle02..."+str);
return "success";
}
2:RequestHeader
@RequestHeader:获取请求头中某个key的值; request.getHeader("User-Agent");
- value() :取的参数名
- required() :是否为必填
- defaultValue() :如果没带值,那么取默认值
3:CookieValue
CookieValue:获取某个cookie的值; 以前的操作获取某个cookie;
Cookie[] cookies = request.getCookies(); for(Cookie c:cookies){
if(c.getName().equals("JSESSIONID")){ String
cv = c.getValue(); } }
现在:
@RequestMapping("/handle01")
public String handle02(
@RequestParam(value = "user", required = false, defaultValue = "你没带") String username,
@RequestHeader(value = "AHAHA", required = false, defaultValue = "她也没带") String userAgent,
@CookieValue(value="JSESSIONID",required=false)String jid) {
System.out.println("这个变量的值:" + username);
System.out.println("请求头中浏览器的信息:" + userAgent);
System.out.println("cookie中的jid的值"+jid);
return "success";
}
4:请求参数是一个POJO(简单对象)
* SpringMVC会自动的为这个POJO进行赋值
* 1)、将POJO中的每一个属性,从request参数中尝试获取出来,并封装即可;
* 2)、还可以级联封装;属性的属性,只需要name=a.b即可;
* 3)、请求参数的参数名和对象中的属性名一一对应就行
实体类:
public class Book {
private String name;
private String author;
private Integer price;
public Book(String name, String author, Integer price) {
this.name = name;
this.author = author;
this.price = price;
}
public Book() {
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getAuthor() {
return author;
}
public void setAuthor(String author) {
this.author = author;
}
public Integer getPrice() {
return price;
}
public void setPrice(Integer price) {
this.price = price;
}
@Override
public String toString() {
return "Book{" +
"name='" + name + '\'' +
", author='" + author + '\'' +
", price=" + price +
'}';
}
}
前端提交参数:
<form action="books" method="post"> 姓名:<input name="name" type="text"><br/> 作者:<input name="author" type="text"><br/> 价格:<input name="price" type="text"><br/> <input type="submit"> </form>
后端接口接受:
@RequestMapping(value = "/books")
public String addBook(Book book) {
System.out.println("handle02..."+book);
return "success";
}
5: SpringMVC可以直接在参数上写原生API;
/**
* SpringMVC可以直接在参数上写原生API;
*
* HttpServletRequest
* HttpServletResponse
* HttpSession
*
* java.security.Principal
* Locale:国际化有关的区域信息对象
* InputStream: ServletInputStream inputStream = request.getInputStream();
* OutputStream: ServletOutputStream outputStream = response.getOutputStream();
* Reader: BufferedReader reader = request.getReader();
* Writer: PrintWriter writer = response.getWriter();
*/
@RequestMapping("/handle03")
public String handle03(HttpSession session,
HttpServletRequest request,HttpServletResponse response) throws IOException {
request.setAttribute("reqParam", "我是请求域中的");
session.setAttribute("sessionParam", "额我是Session域中的");
return "success";
}
二:解决SpringMVC乱码问题
1:请求乱码
1.1:get:现在tomcat已经做了设置,get乱码没问题
1.2:post乱码:在web.xml中增加拦截器,设置request.setCharacterEncoding("UTF-8");
注:这个filter必须在最前边,不能在Restfilter之后,否则都取了参数了,再更改就没有意义了
<!--设置拦截器,将请求的编码改为utf-8-->
<filter>
<filter-name>CharacterEncodingFilter</filter-name>
<filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
<!--eNCODEING是请求编码-->
<init-param>
<param-name>encoding</param-name>
<param-value>UTF-8</param-value>
</init-param>
<!--forceRequestEncoding是否对请求编码-->
<init-param>
<param-name>forceRequestEncoding</param-name>
<param-value>true</param-value>
</init-param>
<!--forceResponseEncoding是否对响应编码-->
<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>
源码参考:
2:响应乱码
response.setContentType("text/html;charset=utf-8")
或者像上边一样,用web.xml将响应编码改为UTF-8