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的屬性
@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");
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(簡單對象)
* 1)、將POJO中的每一個屬性,從request參數中嘗試獲取出來,並封裝即可;
* 2)、還可以級聯封裝;屬性的屬性,只需要name=a.b即可;
實體類:
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;
* InputStream: ServletInputStream inputStream = request.getInputStream();
* OutputStream: ServletOutputStream outputStream = response.getOutputStream();
* Reader: BufferedReader reader = request.getReader();
@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