一、RequestMapping注解
RequestMapping注解的作用是建立請求URL和處理方法之間的對應關系
RequestMapping注解可以作用在方法和類上
1. 作用在類上:第一級的訪問目錄
2. 作用在方法上:第二級的訪問目錄
3. 細節:路徑可以不編寫 / 表示應用的根目錄開始
1.RequestMapping的屬性
- 1. path 指定請求路徑的url
- 2. value value屬性和path屬性是一樣的
- 3. mthod 指定該方法的請求方式
@Controller
@RequestMapping(path = "/role") // 一級請求路徑
public class RoleController {/*** /role/save* method="當前方法允許請求方式能訪問"* params="請求路徑上傳參數"* @return*/@RequestMapping(path = "/save",method = {RequestMethod.GET})public String save(){System.out.println("保存角色...");return "suc";}@RequestMapping(value = "/delete")public String delete(){System.out.println("刪除角色...");return "suc";}
}
2.RequestMapping的請求參數綁定
(1). 綁定機制
- 1. 表單提交的數據都是k=v格式的 username=haha&password=123
- 2. SpringMVC的參數綁定過程是把表單提交的請求參數,作為控制器中方法的參數進行綁定的
- 3. 要求:提交表單的name和參數的名稱是相同的
(2). 支持的數據類型
- 1. 基本數據類型和字符串類型
- 2. 實體類型(JavaBean)
- 3. 集合數據類型(List、map集合等)
基本數據類型和字符串類型
- 1. 提交表單的name和參數的名稱是相同的
- 2. 區分大小寫
實體類型(JavaBean)
- 1. 提交表單的name和JavaBean中的屬性名稱需要一致
- 2. 如果一個JavaBean類中包含其他的引用類型,那么表單的name屬性需要編寫成:對象.屬性 例如:address.name
給集合屬性數據封裝
- 1. JSP頁面編寫方式:list[0].屬性
jsp代碼
<html>
<head><meta charset="utf-8"><title>入門程序</title>
</head>
<body>
<h3>入門</h3><a href="/SpringMVC/hello" >入門程序</a><h1>請求參數綁定入門程序</h1><form action="/SpringMVC/user/save" method="get"><input type="text" name="username"/><br/><input type="text" name="age"/><br/><input type="submit"/></form><h1>請求參數綁定入門程序(封裝到實體類)</h1><form action="/user/save1" method="post"><input type="text" name="username"/><br/><input type="text" name="age"/><br/><input type="submit"/></form><h1>請求參數綁定入門程序(封裝到實體類)</h1><form action="/user/save2" method="post"><input type="text" name="username"/><br/><input type="text" name="age"/><br/><input type="text" name="account.money"/><br/><input type="submit"/></form><h1>請求參數綁定入門程序(存在list集合)</h1><form action="/user/save3" method="post"><input type="text" name="username"/><br/><input type="text" name="age"/><br/><input type="text" name="account.money"/><br/><input type="text" name="accounts[0].money"/><br/><input type="text" name="accounts[1].money"/><br/><input type="submit"/></form>
</body>
</html>
JavaBean代碼
public class Account {private Double money;public Double getMoney() {return money;}public void setMoney(Double money) {this.money = money;}@Overridepublic String toString() {return "Account{" +"money=" + money +'}';}
}
public class User {private String username;private Integer age;private Account account;private List<Account> accounts;public String getUsername() {return username;}public void setUsername(String username) {this.username = username;}public Integer getAge() {return age;}public void setAge(Integer age) {this.age = age;}public Account getAccount() {return account;}public void setAccount(Account account) {this.account = account;}public List<Account> getAccounts() {return accounts;}public void setAccounts(List<Account> accounts) {this.accounts = accounts;}@Overridepublic String toString() {return "User{" +"username='" + username + '\'' +", age=" + age +", account=" + account +", accounts=" + accounts +'}';}
}
controller代碼
@Controller
@RequestMapping("/user")
public class UserController {@RequestMapping("/save")public String save(String username,Integer age){System.out.println(username);System.out.println(age);return "suc";}@RequestMapping("/save1")public String save1(User user){System.out.println(user.toString());return "suc";}@RequestMapping("/save2")public String save2(User user){System.out.println(user);return "suc";}@RequestMapping("/save3")public String save3(User user){System.out.println(user);return "suc";}}
在控制器中使用原生的ServletAPI對象
只需要在控制器的方法參數定義HttpServletRequest和HttpServletResponse對象
@RequestMapping(value = "/save6.do",method = {RequestMethod.POST})
public String save6(HttpServletRequest request, HttpServletResponse response){// 獲取到HttpSession對象System.out.println(request.getParameter("username"));HttpSession session = request.getSession();System.out.println(session);System.out.println(response);return "suc";
}
三、不常用的注解
1.RequestParam注解
1. 作用:把請求中的指定名稱的參數傳遞給控制器中的形參賦值
2. 屬性:
- 1. value:請求參數中的名稱
- 2. required:請求參數中是否必須提供此參數,默認值是true,必須提供
3. 代碼如下:
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;@Controller
@RequestMapping("/dept")
public class DeptController {@RequestMapping("/save")public String save(@RequestParam(value = "username",required = false) String name){System.out.println(name);return "suc";}
}