springmvc 返回实体类传参是调用实体类的set方法吗

springmvc表单提交实体类封装,参数多样化 - 开源中国社区
当前访客身份:游客 [
当前位置:
使用springmvc开发项目,想用到springmvc自带的表单封装功能,但是好像这个功能有点不符合我的要求,如图所示:
图中画蓝线的表单数据就需要手动处理,想问一下springmvc实体类自动组装会自动转型吗?比如下面这个蓝线,我存储这个用户,表里其实是这个用户的ID,那springmvc会不会自动忽略这个查询实体类的过程,直接将这个ID插入表中?
共有6个答案
<span class="a_vote_num" id="a_vote_num_
这些都能处理。 databind
--- 共有 2 条评论 ---
: 数据绑定,你能百度下吗?
(2年前)&nbsp&
没见过databind啊
(2年前)&nbsp&
<span class="a_vote_num" id="a_vote_num_
不需要手动处理这些参数,想想看,要是有个100个字段,你这不是死掉了吗??
--- 共有 2 条评论 ---
:ajax提交 json的话直接{param:{user:{id:"xxx"}}}
浏览器直接提交的话,应该是xxx.xxx.xx这种格式封装对象吧,哥没用过,或许可以在这个方面猜想,框架不都这么做的么
(2年前)&nbsp&
我也不想这样处理啊,关键是有些数据拿过来需要再次加工,比如那个关联的用户,拿过来是一个String类型的ID,但是实体类是一个关联的用户对象,这个该怎么处理呢?
(2年前)&nbsp&
<span class="a_vote_num" id="a_vote_num_
百度下,springmvc ,磨刀不误砍柴工。
<span class="a_vote_num" id="a_vote_num_
百度:springmvc 数据绑定
<span class="a_vote_num" id="a_vote_num_
为什么那么多request.getParameter('')
$.get('', {pageNo:5,pageSize:1},function(result){});
用了springMVC就可以直接public String getList(int pageNo,int pageSize)
@Controller里定义的方法参数名称就是前端入参的名称,还可以对象.指定前端传参名称.rsetful等等...
<span class="a_vote_num" id="a_vote_num_
你为什么不直接在方法参数中直接传对象呢
--- 共有 1 条评论 ---
最开始就用的是直接传对象,但是对象的有些数据需要人为操作,比如那个关联的用户对象,springmvc会自动转化吗?
(2年前)&nbsp&
更多开发者职位上
有什么技术问题吗?
合抱之木...的其它问题
类似的话题springmvc 不同controller forward 传参怎么传?有哪几种方式?_百度知道学习笔记_springmvc注解形式的开发参数接收 - 推酷
学习笔记_springmvc注解形式的开发参数接收
springmvc基于注解的开发
注解第一个例子
创建web项目
在springmvc的配置文件中指定注解驱动,配置扫描器
&!-- sprimgmvc 注解驱动 --&
&!-- &mvc:annotation-driven /& --&
&!-- springmvc的扫描器,一旦有扫描器的定义上面的mvc:annotation.. 就不需要了,扫描器已经有哪里注解驱动的功能 --&
&context:component-scan base-package=&cn.edu.hqu.springmvc&&&/context:component-scan&
&!--视图解析器 前缀+ viewName +后缀 --&
&bean class=&org.springframework.web.servlet.view.InternalResourceViewResolver&&
&!-- webroot到某一指定的文件夹的路径 --&
&property name=&prefix& value=&/WEB-INF/&&&/property&
&!-- 视图名称的后缀 --&
&property name=&suffix& value=&.jsp&&&/property&
在cn.hqu.springmvc下 &新 &建 &TestController
package cn.edu.hqu.
import org.springframework.stereotype.C
import org.springframework.web.bind.annotation.RequestM
@Controller//用来标注当前类是springmvc的控制层的类
@RequestMapping(&/test&) //controller的唯一标识或者命名空间
public class TestController
* 方法的返回值是ModelAndVIew中的viewName
@RequestMapping(&/hello.do&)//用来访问控制层的方法的注解
public String hello(){
return &jsp/index&;
@controller:标识当前类是控制层的一个具体的实现
@requestMapping:放在方法上面用来指定某个方法的路径,当它放在类上的时候相当于命名空间需要组合方法上的requestmapping来访问。
类上面有@RequestMapping(&/
&)访问:localhost:8080/springmvc-2/
没有的话:localhost:8080/springmvc-2/hello.do
部署,访问;
注解方式参数
在TestController
* HttpServletRequest可以直接定义在参数的列表
* @param request
@RequestMapping(&/toPerson.do&)
public String toPerson(HttpServletRequest request) {
String result = request.getParameter(&name&);
System.out.println(result);
return &jsp/index&;
重启tomcat访问:http://localhost:8080/springmvc-2/test/toPerson.do?name=jerome
控制台输出:jerome
* 在参数列表上直接定义要接收的参数名称,只要参数名称能匹配的上就能接收所传过来的数据
* 可以自动转换成参数列表里面的类型,注意的是值与类型之间是可以转换的
* @param name
@RequestMapping(&/toPerson1.do&)
public String toPerson1(String name,Integer age,String address){
System.out.println(name+& &+age+& &+address);
return &jsp/index&;
地址栏和代码两边的参数要一致;
重启tomcat,访问:
http://localhost:8080/springmvc-2/test/toPerson1.do?name=jerome&age=23&address=china
控制台输出:jerome 23 china
如果有时间类型要 注册属性编辑
* 注册时间类型的属性编辑器
* @param binder
@InitBinder
public void initBinder(ServletRequestDataBinder binder){
binder.registerCustomEditor(Date.class, new CustomDateEditor(new SimpleDateFormat(&yyyy-MM-dd&), true));
@RequestMapping(&/toPerson1.do&)
public String toPerson1(String name,Integer age,String address,Date birthday){
System.out.println(name+& &+age+& &+address+& &+birthday);
return &jsp/index&;
重启tomcat,访问
http://localhost:8080/springmvc-2/test/toPerson1.do?name=jerome&age=23&address=china&birthday=
控制台输出:jerome 23 china Wed Dec 12 00:00:00 CST 2012
Springmvc接收实体类:
在cn.edu.hqu.entity 新建一个类Person,将上面的四个参数写进去,get set方法和toString方法;
* 传递的参数名字必须要和实体类的属性set方法后面的字符串匹配的上才能接收到参数
* @param person
@RequestMapping(&/toPerson2.do&)
public String toPerson2(Person person){
System.out.println(person);
return &jsp/index&;
重启tomcat 执行
http://localhost:8080/springmvc-2/test/to
.do?name=jerome&age=23&address=china&birthday=
控制台输出:
Person [name=jerome, address=china, birthday=Wed Dec 12 00:00:00 CST 2012, age=23]
浏览器参数首字母大写没影响;
如果多个实体类:添加实体类User 属性:name age;get set toString方法;
* 传递的参数名字必须要和实体类的属性set方法后面的字符串匹配的上才能接收到参数
* 请求中的参数只要是能和参数列表里面的变量名或者实体里面的set后面的字符串匹配的上就能接受到
* @param person
@RequestMapping(&/toPerson2.do&)
public String toPerson2(Person person, User user){
System.out.println(person);
System.out.println(user);
return &jsp/index&;
重启tomcat 执行
http://localhost:8080/springmvc-2/test/toPerson2.do?name=jerome&age=23&address=china&birthday=
控制台输出:
Person [name=jerome, address=china, birthday=Wed Dec 12 00:00:00 CST 2012, age=23]
User [name=jerome, age=23]
接收一个数组;
* 对数组的接收
* @param names
@RequestMapping(&/toPerson3.do&)
public String toPerson3(String[] names){
for(String result:names){
System.out.println(result);
return &jsp/index&;
重启tomcat 执行
http://localhost:8080/springmvc-2/test/toPerson3.do?names=jerome&names=jelly
控制台输出:
已发表评论数()
请填写推刊名
描述不能大于100个字符!
权限设置: 公开
仅自己可见
正文不准确
标题不准确
排版有问题
主题不准确
没有分页内容
图片无法显示
视频无法显示
与原文不一致spring&MVC传递参数的方法_Qinrb_新浪博客
spring&MVC传递参数的方法
开场白:spring mvc 通过客户端request请求通过Controller
servlet产生的结果传到jsp视图页面展示,再通过service连接数据库
&#65279;spring
mvc实现过程&
这为大家介绍spring mvc如何通过注解的方式从页面传参数
1.创建web项目
&#65279;spring
2.导入spring
mvc所需要的架包
3.配置web.xml,引入DispatcherServlet处理的请求必须在同一个web.xml文件里使用url-mapping定义映射,通过CharacterEncodingFilter处理乱码问题.
&#65279;web.xml
org.springframework.web.servlet.DispatcherServlet
contextConfigLocation
/WEB-INF/spring-mvc.xml
& CharacterEncodingFilter
org.springframework.web.filter.CharacterEncodingFilter
& & & utf-8
& CharacterEncodingFilter
4.创建spring-mavc.xml&#8203;配置spring 配置文件
spring-mavc.xml&#8203;
&#65279;spring-mavc.xml&#8203;
&xmlns:mvc="http://www.springframework.org/schema/mvc"
&xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
&xmlns:context="http://www.springframework.org/schema/context"
xmlns:tx="http://www.springframework.org/schema/tx"
&xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd"&
5.创建jsp页面,通过pos提交,访问action="user/add.do"&
&#65279;jsp
6.创建一个包&#8203;,并创建class类
&#65279;类名为UserController
7.在类中写相应的方法
(1)注入spring mvc配置文件,user为jsp前缀
(1)通过HttpServletRequest
request&#8203;请求获取参数,返回到相应的页面
HttpServletRequest
request&#65279;&
(2)通过注解,通过定义参数直接从页面获取参数
&#65279;直接定义获取参数
(3)注解 通过RequestParam获取参数
&#65279;RequestParam
(4)通过对象获取多个参数
(1)创建实体对象,并为其生成get和set方法
&#65279;实体
(2)通过对象的形式获取参数
&#65279;对象获取参数
&#8203;8.时间处理方式,有的时候我们从页面传的是时间对象,如何转换为时间格式存储数据库
(1)通过装换实现
&#65279;存储时间方法
&#65279;时间编辑器&&
(2)直接定义时间存储
&#65279;直接定义
以上就是spring
mvc从页面获取参数的一些方法,这些只是一些简单的方式,后面我讲给大家分享AJAX传参,下一期分享,希望对大家有所帮助!
&#8203;&#8203;
博客等级:
博客积分:0
博客访问:1,216
关注人气:0
荣誉徽章:spring mvc 如何从前台表单传递集合参数并绑定集合对象。
[问题点数:40分,结帖人NewMoons]
spring mvc 如何从前台表单传递集合参数并绑定集合对象。
[问题点数:40分,结帖人NewMoons]
不显示删除回复
显示所有回复
显示星级回复
显示得分回复
只显示楼主
2013年3月 总版技术专家分月排行榜第二
2014年2月 Java大版内专家分月排行榜第一2013年8月 Java大版内专家分月排行榜第一2013年5月 Java大版内专家分月排行榜第一2013年4月 Java大版内专家分月排行榜第一2013年3月 Java大版内专家分月排行榜第一2013年2月 Java大版内专家分月排行榜第一
匿名用户不能发表回复!|
每天回帖即可获得10分可用分!小技巧:
你还可以输入10000个字符
(Ctrl+Enter)
请遵守CSDN,不得违反国家法律法规。
转载文章请注明出自“CSDN(www.csdn.net)”。如是商业用途请联系原作者。

我要回帖

更多关于 springmvc 返回实体类 的文章

 

随机推荐