前言:
年末了,忙了一年了卻發現系統的整理的東西很少,一些基礎的東西都未做整理,這里就將它隨便整理一下,增加一些印象,當然在網上看到一些好的資料也會整理下來以備后用。今天整理一下springMVC獲取參數的幾種方式。
正題:
1、直接把表單的參數寫在Controller相應的方法的形參中,適用於get方式提交,不適用於post方式提交。
/**
* 1.直接把表單的參數寫在Controller相應的方法的形參中
* @param canshu1
* @param canshu2
* @return
*/
@RequestMapping("/demo")
public String demo(String canshu1,String canshu2) {
System.out.println("canshu1 is:"+canshu1);
System.out.println("canshu2 is:"+canshu2);
return "demo/index";
}
url形式:http://localhost:8090/demo/demo?canshu1=111&canshu2=222提交的參數需要和Controller方法中的入參名稱一致。
2、通過HttpServletRequest接收,post方式和get方式都可以。
/**
* 2、通過HttpServletRequest接收
* @param request
* @return
*/
@RequestMapping("/demo1")
public String demo1(HttpServletRequest request) {
String canshu1=request.getParameter("canshu1");
String canshu2=request.getParameter("canshu2");
System.out.println("canshu1is:"+canshu1);
System.out.println("canshu2is:"+canshu2);
return "demo/index";
}
3、通過一個bean來接收,post方式和get方式都可以。
(1)建立一個和表單中參數對應的bean
package demo.model;
public class UserModel {
private String username;
private String password;
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
}
(2)用這個bean來封裝接收的參數
/**
* 3、通過一個bean來接收
* @param user
* @return
*/
@RequestMapping("/addUser3")
public String addUser3(UserModel user) {
System.out.println("username is:"+user.getUsername());
System.out.println("password is:"+user.getPassword());
return "demo/index";
}
4、通過@PathVariable獲取路徑中的參數
/**
* 4、通過@PathVariable獲取路徑中的參數
* @param username
* @param password
* @return
*/
@RequestMapping(value="/demo2/{username}/{password}",method=RequestMethod.GET)
public String addUser4(@PathVariable String username,@PathVariable String password) {
System.out.println("username is:"+username);
System.out.println("password is:"+password);
return "demo/index";
}
例如,訪問http://localhost:8090/demo/demo/name/111111 路徑時,則自動將URL中模板變量{username}和{password}綁定到通過@PathVariable注解的同名參數上,即入參后username=name、password=111111。
5、使用@ModelAttribute注解獲取POST請求的FORM表單數據
Jsp表單如下:
<form action ="<%=request.getContextPath()%>/demo5" method="post">
用戶名: <input type="text" name="username"/><br/>
密 碼: <input type="password" name="password"/><br/>
<input type="submit" value="提交"/>
<input type="reset" value="重置"/>
</form>
Java Controller如下:
/**
* 5、使用@ModelAttribute注解獲取POST請求的FORM表單數據
* @param user
* @return
*/
@RequestMapping(value="/demo5",method=RequestMethod.POST)
public String demo5(@ModelAttribute("user") UserModel user) {
System.out.println("username is:"+user.getUsername());
System.out.println("password is:"+user.getPassword());
return "demo/index";
}
6、用注解@RequestParam綁定請求參數到方法入參
當請求參數username不存在時會有異常發生,可以通過設置屬性required=false解決,例如: @RequestParam(value="username", required=false)
/**
* 6、用注解@RequestParam綁定請求參數到方法入參
* @param username
* @param password
* @return
*/
@RequestMapping(value="/demo6",method=RequestMethod.GET)
public String addUser6(@RequestParam("username") String username,@RequestParam("password") String password) {
System.out.println("username is:"+username);
System.out.println("password is:"+password);
return "demo/index";
}

