手机扫码查看
202java框架教程之springMVC常见注解
1.@RequestMapping的位置
@RequestMapping(“/uri”)
@Controller
@RequestMapping("/hello")
public class HelloWorld {
@RequestMapping("/world")
public String helloworld(){
System.out.println("hello world");
return "helloworld";
}
}
2.@RequestMapping的请求方式
POST形式:@RequestMapping(value = “/world”,method = RequestMethod.POST)
GET形式:@RequestMapping(value = “/world”,method = RequestMethod.GET)
如果不指定method,那么可以接受任何类型的请求。
如果请求方式不对,则报405错误
3.处理请求参数
A.自动参数匹配:表单中控件的name的值和controller层方法的参数名一致,则匹配
表单的controller和form
@Controller
@RequestMapping("/hello")
public class HelloWorld {
@RequestMapping(value = "/loginform")
public String loginform(){
return "login";
}
}
<form action="" method="post">
<label for="username">
username : <input type="text" id="username" name="username"><br>
</label>
<label for="password">
password : <input type="password" name="password" id="password">
</label>
<button>登录</button>
</form>
提交的表单controller
@Controller
@RequestMapping("/hello")
public class HelloWorld {
@RequestMapping(value = "/world",method = RequestMethod.GET)
public String helloworld(){
System.out.println("hello world");
return "helloworld";
}
@RequestMapping(value = "/loginform")
public String loginform(){
return "login";
}
@RequestMapping(value = "/login",method = RequestMethod.POST)
public String login(String username,String password){
System.out.println("执行登录");
System.out.println("username:"+username+"\n"+"password:"+password);
return "redirect:world";
}
}
B.如果不一致,使用@RequestParam() 注解
表单:<label>username : <input type=”text” id=”username” name=”username2″></label>
controller:
public String login(@RequestParam(value = “username2”) String username, String password){
C.url地址中get形式的参数匹配:
url:../user/userinfo?username=admin&password=admin
controller:
@RequestMapping("/user")
public class HelloWorld {
@RequestMapping(value = "/userinfo",method = RequestMethod.GET)
public String login(String username, String password){
System.out.println("执行登录");
System.out.println("username:"+username+"\n"+"password:"+password);
return "redirect:world";
}
创建jsp文件
D.可以用@RequestParam的defaultValue设置参数的默认值
public String login(
@RequestParam(value = "username",defaultValue = "admin") String username,
@RequestParam(value = "password",defaultValue = "admin") String password){
System.out.println("执行登录");
System.out.println("username:"+username+"\n"+"password:"+password);
return "redirect:world";
}
E.如果是整数类型,必须使用包装类型,如Integer
因为当参数不存在时,SpringMVC会将参数的值转换为null。
而使用基本类型会出现转换异常。
F.设置参数是否可选:
可以使用 required 属性设置参数是否可选参数
public String login(
@RequestParam(value = "username",required = false) String username, String password){
4.路径参数
@RequestMapping("/get/{id}")
public String get(@PathVariable(value = "id") Integer id){
System.out.println("id:"+id);
return "hello/edit";//创建 edit 的jsp页面
}
5.请求转发和跳转
请求转发:return “视图名称”
请求跳转:return “redirect:controller的mapping地址”
6.解决post乱码问题
<!-- 解决 SpringMVC的 post 乱码 -->
<filter>
<filter-name>requestContextFilter</filter-name>
<filter-class>org.springframework.web.filter.RequestContextFilter</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>UTF-8</param-value>
</init-param>
<init-param>
<param-name>forceEncoding</param-name>
<param-value>true</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>requestContextFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
7.CookieValue
@RequestMapping("/cookie")
public String getCookie(@CookieValue("JSESSIONID") String cookies){
System.out.println(cookies);
return "cookie";
}
8.@RequestHeader
获取请求头某一部分的值
@RequestMapping("/header")
public String getHeader(@RequestHeader(value = "Accept-Language") String header){
System.out.println(header);
return "header";
}
9.请求参数和请求头表达式
@RequestMapping(value = "/testparam",params = {"username","age!=10"})
public String testParam(String username,Integer age){
System.out.println(username+" "+age);
return "cookie";
}
传参:http://localhost:8080/testhome/testparam?username=admin&age=11



发表回复