2018.5.29
1、@RequestMapping的位置
- 可以加在类名的前面或者方法名前面,或者同时加在两个位置
- 形成一个严谨的路径是两个位置的mapping合起来必须最前面有"/"
- 最终的路径是两个位置的路径的组合。
- value是默认的名称,可以省略,但是如果有其他参数,不能省略
如下配置的访问路径是:协议://主机:端口/虚拟路径/hello/world
@Controller
注解作用:注册Bean到Spring上下文,Bean的默认ID为类名称首字母小写,也可以自己指定。
@Controller
//
名字不用写
dispatherservlet
智能的去找
@RequestMapping
(
"/hello"
)
public
class
HelloWorld {
//#RequestMapping
:请求映射
url
地址,映射到这个方法
@RequestMapping
(value=
"/world"
)
public
String helloworld() {
System.
out
.println(
"hello word"
);
return
"helloworld"
;
}
}
|
二、@RequestMapping的请求方式
GET/POST
@RequestMapping
(value=
"/world"
,method=RequestMethod.
POST
)
@RequestMapping
(value=
"/world"
,method=RequestMethod.
GET
)
|
- 如果不指定method那么可以接受任何形式的请求。
- 如果请求方式不正确:
405错误:请求方式错误,原本是get用post
三、处理器请求参数
1、自动的表单参数匹配(POST/GET):表单中控件的
name的值和controller层方法的参数名一致则匹配。
表单的controller和form:
@Controller
//
名字不用写
dispatherservlet
智能的去找
@RequestMapping
(
"/hello"
)
public
class
HelloWorld {
//#RequestMapping
:请求映射
url
地址,映射到这个方法
@RequestMapping
(value=
"/world"
,method=RequestMethod.
GET
)
@RequestMapping
(
"/loginform"
)
public
String loginForm() {
return
"login"
;
}
}
|
<
body
>
<
form
action
=
"login"
method
=
"post"
>
<!--label for="id"
自动获取对应
id
的文本框,获取焦点,如果
input
写在
label
里面可以不写
for. -->
<
label
for
=
"username"
>
用户名
<
input
type
=
"text"
id
=
"username"
name
=
"username"
/></
label
>
<
label
for
=
"password"
>
密码
<
input
type
=
"password"
id
=
"password"
name
=
"password
"
/></
label
>
<
button
>
登录
</
button
>
</
form
>
</
body
>
|
提交表单controller;
@RequestMapping
(value=
"/login"
,method=RequestMethod.
POST
)
public
String login(String
username
,String
password
) {
System.
out
.
println
(
username
);
System.
out
.
println
(
password
);
System.
out
.
println
(
"
执行登录
"
);
/*return "
helloworld
";//http://localhost:8080/springmvc-01-hello/hello/login
*/
return
"redirect:world"
;
//http://localhost:8080/springmvc-01-hello/hello/world
}
|
2、注解形式的参数匹配:传的是“name”的值
name为realname和controller参数不匹配
<
form
action
=
"login"
method
=
"post"
>
<!--label for="id"
自动获取对应
id
的文本框,获取焦点,如果
input
写在
label
里面可以不写
for. -->
<
label
for
=
"username"
>
用户名
<
input
type
=
"text"
id
=
"username"
name
=
"realname"
/></
label
>
<
label
for
=
"password"
>
密码
<
input
type
=
"password"
id
=
"password"
name
=
"password"
/></
label
>
<
button
>
登录
</
button
>
</
form
>
|
如果参数和
jsp
中表单中
name
的值不匹配,可以用
@RequestParam
(
value="form.name"
)注解来匹配
@RequestMapping
(value=
"/login"
,method=RequestMethod.
POST
)
public
String login(
@RequestParam
(value=
"realname"
)String
username
,String
password
) {
//
如果参数和
jsp
中表单中
name
的值不匹配,可以用
@RequestParam
(
value="name"
)注解来匹配
System.
out
.println(
username
);
System.
out
.println(
password
);
System.
out
.println(
"
执行登录
"
);
/*return "
helloworld
";//http://localhost:8080/springmvc-01-hello/hello/login
*/
return
"redirect:world"
;
//http://localhost:8080/springmvc-01-hello/hello/world
|
3.url地址中get形式的参数匹配
controller:
@Controller
@RequestMapping
(
"/user"
)
//
窄化的注解
别忘了
"/"
public
class
UserController {
@RequestMapping
(
"/list"
)
public
String list(Integer
currentpage
,Integer
pagesize
) {
System.
out
.println(
"currentpage:"
+
currentpage
);
System.
out
.println(
"pagesize:"
+
pagesize
);
return
"user/list"
;
}
}
|
4.可以用defaultValue属性设置参数的默认值
@RequestMapping
(
"/list"
)
public
String list(
@RequestParam
(value=
"currentpage"
,
defaultValue=
"1"
)Integer
currentpage
,
@RequestParam
(value=
"pagesize"
,defaultValue=
"10"
)Integer
pagesize
) {
System.
out
.println(
"currentpage:"
+
currentpage
);
System.
out
.println(
"pagesize:"
+
pagesize
);
return
"user/list"
;
}
|
2018.5.30
5.必须使用包装类型的参数
因为当参数不存在时,springmvc会将参数的值转换成null
而使用基本类型会出现转换异常。
6.设置参数是否可选
可以使用
required属性设置参数是否是可选参数
@RequestMapping
(
"/list"
)
public
String
list(
@RequestParam
(value=
"currentpage"
,
required=
fals
e
)Integer
currentpage
,
@RequestParam
(value=
"pagesize"
,required=
false
)Integer
pagesize
) {
System.
out
.println(
"currentpage:"
+
currentpage
);
System.
out
.println(
"pagesize:"
+
pagesize
);
return
"user/list"
;
}
|
四、路径参数
比较流行的resful 参数简洁化
普通风格 resful风格
user/list?id=1 user/list 查询所有用户
user/update 更新内容放在post user/list/1 get 查询记录为1的记录
user/save 同上 user/list/1 delete 删除为1的用户
user/delete?id=1 user/list/1 post(put)user 更新一个用户对象
user/listAll user/list post user 插入一个用户对象
get/{占位符}
@RequestMapping
(
"get/
{id}
"
)
public
String get(
@PathVariable
(value=
"id"
)
Integer
id
) {
System.
out
.println(
"id:"
+
id
);
return
"user/edit"
;
}
|
五、请求转发和请求跳转
请求转发:@RequestMapping
(value=
"/login"
,method=RequestMethod.
POST
)
请求跳转:
return
"redirect:world"
;
六、解决post乱码
<!-- 解决SpringMVC的post乱码(字符编码过滤器) -->
<
filter
>
<
filter-name
>
characterEncodingFilter
</
filter-name
>
<
filter-class
>
org.springframework.web.filter.CharacterEncodingFilter
</
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
>
characterEncodingFilter
</
filter-name
>
<
url-pattern
>
/*
</
url-pattern
>
</
filter-mapping
>
|
七、解决GET乱码的方案
springmvc中 get不会乱码