springMVC返回数据的四种方式

本文详细介绍了SpringMVC框架中四种常见的数据返回方法:通过request.setAttribute、使用ModelAndView、利用Model对象以及Map对象进行数据传递的具体实现。这些方法适用于不同场景,帮助开发者灵活处理前后端交互。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

转自: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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值