@Controller
//
名字不用写
dispatherservlet
智能的去找
@RequestMapping
(
"/hello"
)
public
class
HelloWorld {
//#RequestMapping
:请求映射
url
地址,映射到这个方法
@RequestMapping
(value=
"/world"
)
public
String helloworld() {
System.
out
.println(
"hello word"
);
return
"helloworld"
;
}
}
|
@RequestMapping
(value=
"/world"
,method=RequestMethod.
POST
)
@RequestMapping
(value=
"/world"
,method=RequestMethod.
GET
)
|
@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
=
"username"
>
用户名
<
input
type
=
"text"
id
=
"username"
name
=
"username"
/>
label
>
<
label
for
=
"password"
>
密码
<
input
type
=
"password"
id
=
"password"
name
=
"password
"
/>
label
>
<
button
>
登录
button
>
form
>
body
>
|
@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
}
|
<
form
action
=
"login"
method
=
"post"
>
<
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
>
|
@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
|
@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"
;
}
}
|
@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"
;
}
|
@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"
;
}
|
@RequestMapping
(
"get/
{id}
"
)
public
String get(
@PathVariable
(value=
"id"
)
Integer
id
) {
System.
out
.println(
"id:"
+
id
);
return
"user/edit"
;
}
|
<
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
>
|