Table of Contents
一:SpringMVC获取参数
1:RequestParam
1:SpringMVC如何获取请求带来的各种信息 默认方式获取请求参数: 直接给方法入参上写一个和请求参数名相同的变量。这个变量就来接收请求参数的值; 带:有值,没带:null;
2:@RequestParam:获取请求参数的;参数默认是必须带的;
3:@RequestParam("user")和 @PathVariable("user")的区别
4:RequestParam的属性
2:RequestHeader
3:CookieValue
4:请求参数是一个POJO(简单对象)
5: SpringMVC可以直接在参数上写原生API;
二:解决SpringMVC乱码问题
1:请求乱码
1.1:get:现在tomcat已经做了设置,get乱码没问题
1.2:post乱码:在web.xml中增加拦截器,设置request.setCharacterEncoding("UTF-8");
2:响应乱码
如:@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";
}
}
RequestParam----从请求参数中获取user,xxx?user=123
PathVariable---是从请求路径中获取user,xxx/{user}
@RequestMapping("/hello03")
public String handle03(@RequestParam(value = "name",required = false,defaultValue = "666")String str) {
System.out.println("handle02..."+str);
return "success";
}
@RequestHeader:获取请求头中某个key的值; request.getHeader("User-Agent");
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";
}
* 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 +
'}';
}
}
前端提交参数:
后端接口接受:
@RequestMapping(value = "/books")
public String addBook(Book book) {
System.out.println("handle02..."+book);
return "success";
}
/**
* 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";
}
注:这个filter必须在最前边,不能在Restfilter之后,否则都取了参数了,再更改就没有意义了
CharacterEncodingFilter
org.springframework.web.filter.CharacterEncodingFilter
encoding
UTF-8
forceRequestEncoding
true
forceResponseEncoding
true
CharacterEncodingFilter
/*
源码参考:
response.setContentType("text/html;charset=utf-8")
或者像上边一样,用web.xml将响应编码改为UTF-8