-
Notifications
You must be signed in to change notification settings - Fork 70
Spring MVC
luoml edited this page Nov 22, 2018
·
6 revisions
-
value处理方法的请求URL -
method处理方法的Http Method类型,如:RequestMethod.GET -
params请求URL中必须包含(或不包含)的某些参数-
params="var"请求URL中必须包含var参数,如:/find?var=test -
params="!var"请求URL中不能包含var参数 -
params="var=test"请求URL中必须包含var=test,如:/find?var=test -
params="var!=test"请求URL中不能包含var=test
-
-
@RequestMapping支持Ant风格的通配符
-
?匹配任意一个字符 -
*匹配任意多个字符 -
**匹配多层路径
-
@RequestMapping("/user/{id}", method = RequestMethod.GET)
public User get(@PathVariable("id") Integer id) { ... }
// 可以通过 required 设置参数是否必填
@RequestMapping("/user", method = RequestMethod.GET)
public User get(@RequestParam("id") Integer id) { ... }
// jsp
<form method="post" action="*.do">
UserName: <input id="username" type="text" name="username"/>
Password: <input id="password" type="password" name="password"/>
<input type="submit" value="Submit" />
</form>
// po
public class User {
private String username;
private String password;
......
}
// Controller
@RequestMapping(method = RequestMethod.POST)
public void login(@ModelAttribute("user") User user) { ... }
@RequestMapping("/user", method = RequestMethod.GET)
public User get(HttpServletRequest request) { ... }
- J2EE注解
- 默认按名称进行装配,名称可以通过name属性指定。
- 也可指定type,按类型进行装配。
- Spring注解
- 默认按类型装配,且依赖对象必须存在,如果允许null值,可以设置
required=false。 - 如果希望使用名称装配,可结合
@Qualifier注解使用。