转自:https://blog.youkuaiyun.com/itcats_cn/article/details/82119673
springMVC返回数据的四种方式:第一种,通过request.setAttribute进行返回。
1
2
3
4
5
6
7
8
9
|
@RequestMapping
(value=
"/welcomeF"
)
public
String WelcomeF(User user,HttpServletRequest request){
System.out.println(user.toString());
/*通过setAttribute来设置返回*/
request.setAttribute(
"name"
, user.getName());
request.setAttribute(
"password"
, user.getPassword());
request.setAttribute(
"password"
, user.getHobby());
return
"welcome"
;
}
|
第二种,通过ModelAndView进行返回。
1
2
3
4
5
6
7
8
|
@RequestMapping
(value=
"/welcomeSeven"
)
publicModelAndViewWelcomeSeven(User2 user2){
/*通过ModelAndView来返回数据*/
ModelAndView mav =
new
ModelAndView(
"welcome"
);
mav.addObject(
"name"
, user2.getName());
mav.addObject(
"date"
, user2.getUdate());
return
mav;
}
|
第三种,通过model对象进行返回。
1
2
3
4
5
6
7
|
@RequestMapping
(value=
"/welcomeEight"
)
public
String WelcomeEight(User2 user2,Model model){
/*通过Model对象来返回数据*/
model.addAttribute(
"name"
, user2.getName());
model.addAttribute(
"date"
, user2.getUdate());
return
"welcome"
;
}
|
第四种,通过Map对象进行返回。
1
2
3
4
5
6
7
|
@RequestMapping
(value=
"/welcomeNine"
)
public
String WelcomeNine(User2 user2,Map map){
/*通过Model对象来返回数据*/
map.put(
"name"
, user2.getName());
map.put(
"date"
, user2.getUdate());
return
"welcome"
;
}
|
转https://www.2cto.com/kf/201701/591767.html