public
class
Address {
private
String
province
;
private
String
city
;
|
public class
User
{
private String
username;
private String
password;
private String
email;
private Integer
age;
private
Address
address;
|
<
form
action=
"save"
method=
"get"
>
<
label
for
=
""
>用户名:<
input
type
=
"text"
name=
"
username
"
/>
label
>
<
label
for
=
""
>密码:<
input
type
=
"text"
name=
"
password
"
/>
label
>
<
label
for
=
""
>年龄:<
input
type
=
"text"
name=
"
age
"
/>
label
>
<
label
for
=
""
>邮箱:<
input
type
=
"text"
name=
"
email
"
/>
label
>
<
label
for
=
""
>省份:<
input
type
=
"text"
name=
"
address.province
"
/>
label
>
<
label
for
=
""
>城市:<
input
type
=
"text"
name=
"
address.city
"
/>
label
>
form
>
|
@Controller
@RequestMapping("/user")
public
class
UserController
{
@RequestMapping("/form")
public
String form() {
return
"user/form"
;
}
|
@RequestMapping("/save")
public
String save(User user) {
System.
out
.println(user);
return
"user/success"
;
}
|
@RequestMapping(
"/edit"
)
public ModelAndView edit() {
User
user
= new User();
user
.
set
Username(
"路咨询"
);
user
.
set
Password(
"山东"
);
user
.
set
Age(
18
);
Address address = new Address();
address.
set
Province(
"山东"
);
address.
set
City(
"青岛"
);
user
.
set
Address(address);
ModelAndView mav = new ModelAndView();
mav.
set
ViewName(
"user/form"
);
mav.addObject(
"user"
,
user
);
return mav;
}
|
<
form
action
=
"save"
method
=
"get"
>
<
label
for
=
""
>
用户名:
<
input
type
=
"text"
name
=
"username"
value
=
"
${user.username}
"
/>
label
>
<
label
for
=
""
>
密码:
<
input
type
=
"text"
name
=
"password"
value
=
"
${user.password}
"
/>
label
>
<
label
for
=
""
>
年龄:
<
input
type
=
"text"
name
=
"age"
value
=
"
${user.age}
"
/>
label
>
<
label
for
=
""
>
邮箱:
<
input
type
=
"text"
name
=
"email"
value
=
"
${user.email}
"
/>
label
>
<
label
for
=
""
>
省份:
<
input
type
=
"text"
name
=
"address.province"
value
=
"
${user.address.province}
"
/>
label
>
<
label
for
=
""
>
城市:
<
input
type
=
"text"
name
=
"address.city"
value
=
"
${user.address.city}
"
/
label
>
<
button
>
提交
button
>
form
>
|
@RequestMapping(
"/edit1"
)
public String edit1(Map
User
user
= new User();
user
.
set
Username(
"路咨询1"
);
user
.
set
Password(
"山东1"
);
user
.
set
Age(
18
);
Address address = new Address();
address.
set
Province(
"山东1"
);
address.
set
City(
"青岛1"
);
user
.
set
Address(address);
map.put(
"user"
,
user
);
return
"user/form"
;
}
|
/**
* springmvc自带的model
* @param model
* @return
*/
@RequestMapping(
"/edit2"
)
public String edit2(Model model) {
User
user
= new User();
user
.
set
Username(
"路咨询1"
);
user
.
set
Password(
"山东1"
);
user
.
set
Age(
18
);
Address address = new Address();
address.
set
Province(
"山东1"
);
address.
set
City(
"青岛1"
);
user
.
set
Address(address);
model.addAttribute(
"user"
,
user
);
return
"user/form"
;
}
|
/*
* //
将模型放入
requestScope
中的同时,放入到
session
中。
*
局限性缺点:每传一次值
user
都会往
session
里放一次,耦合性太高。
* types={
数据类型
.class}
将基本类型数据放入
session
中,前台
${sessionScope}
取出
*/
@Controller
@RequestMapping
(
"/user"
)
@SessionAttributes
(value={
"user"
},types={String.
class
})
model
.addAttribute(
"user"
,
user
);
model
.addAttribute(
"strvalue"
,
"
路咨询
"
);
return
"user/form"
;
|
${sessionScope.strvalue}
|