Spring MVC接受表单自动封装特性实例解析
这篇文章主要介绍了SpringMVC接受表单自动封装特性实例解析,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
SpringMVC中的Controller可以以实体类接受来自客户端的form表单,表单的字段自动构成实体类对象
客户端的表单
实体类
publicclassUser{ privateStringname; privateIntegerage; privateStringhobby; publicUser(){ this.name="未初始化"; this.age=10; this.hobby="coding"; } publicUser(Stringname){ this.name=name; this.age=10; this.hobby="coding"; } publicUser(Stringname,Integerage){ this.name=name; this.age=age; this.hobby="coding"; } publicUser(Stringname,Integerage,Stringhobby){ this.name=name; this.age=age; this.hobby=hobby; } publicIntegergetAge(){ returnage; } publicStringgetHobby(){ returnhobby; } publicStringgetName(){ returnname; } publicvoidsetAge(Integerage){ this.age=age; } publicvoidsetHobby(Stringhobby){ this.hobby=hobby; } publicvoidsetName(Stringname){ this.name=name; } @Override publicStringtoString(){ return"User{"+ "name='"+name+'\''+ ",age="+age+ ",hobby='"+hobby+'\''+ '}'; } }
服务端接收
@Controller @RequestMapping("/test") publicclassTestController{ @RequestMapping(value="/user",method=RequestMethod.POST) //控制器会自动实例化参数 publicStringuser(Useruser){ System.out.println(user); return"redirect:/test/user"; } @RequestMapping(value="/user",method=RequestMethod.GET) publicStringuser(){ return"form"; } }
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持毛票票。
声明:本文内容来源于网络,版权归原作者所有,内容由互联网用户自发贡献自行上传,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任。如果您发现有涉嫌版权的内容,欢迎发送邮件至:czq8825#qq.com(发邮件时,请将#更换为@)进行举报,并提供相关证据,一经查实,本站将立刻删除涉嫌侵权内容。