SpringMVC接收前台传递过来的值的实例

2025-05-29 0 44

之前控制器方法获得前台传来的有三种方式:

1.通过HttpServletRequest:

?

1

2

3

4

5
@RequestMapping(value="/index1")

public String helloaction1(HttpServletRequest request){

System.out.println(request.getParameter("nnn")); //获得前台name为nnn的元素的值

return "index";

}

2.通过参数名获得:

?

1

2

3

4

5
@RequestMapping(value="/index1")

public String helloaction1(String nnn){ //这里名字要与前端元素名字一致才能获得

System.out.println(nnn);

return "index";

}

3.通过@RequestParam注解获得:

?

1

2

3

4

5

6
@RequestMapping(value="/index")

public String helloaction(@RequestParam(value="nnn",required=false)String nnn1, Model model){ //nnn要与前端一致,在此处可以理解为参数nnn1的别名

System.out.println(nnn1);

model.addAttribute("hello", "这是用action传过来的值:"+nnn1);

return "index";

}

SpringMvc还能通过将vo作为参数获得vo的各个属性:

?

1

2

3

4

5

6
@RequestMapping(value="/index2")

public String helloaction2(User user){

System.out.println(user.getAccount());

System.out.println(user.getPassword());

return "index";

}

使用对象进行获取数据的时候要注意,前端页面的元素name属性要与vo的各个属性名字一致

以上这篇SpringMVC接收前台传递过来的的实例就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持快网idc。

原文链接:http://11189738.blog.51cto.com/11179738/1745137

收藏 (0) 打赏

感谢您的支持,我会继续努力的!

打开微信/支付宝扫一扫,即可进行扫码打赏哦,分享从这里开始,精彩与您同在
点赞 (0)

声明:本站所有文章,如无特殊说明或标注,均为本站原创发布。任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。

快网idc优惠网 建站教程 SpringMVC接收前台传递过来的值的实例 https://www.kuaiidc.com/112342.html

相关文章

发表评论
暂无评论