论坛首页 Java企业应用论坛

SpingMVC如何实现集合参数

浏览 31455 次
精华帖 (0) :: 良好帖 (0) :: 新手帖 (0) :: 隐藏帖 (1)
作者 正文
   发表时间:2013-01-10  
sootv 写道
我不知道是不是你想要的结果,
我的理解是你要从界面传送多个user对象至后台,可以通过这样,解决
①封装user对象,List<User> user
②controller里面参数List<User> user
②界面上写比如 user[0].username,user[1].username,user[2].username
  传到后台之后,springMVC自动给user集合放入3个对象



我就是用这种方式获得页面的LIST,报下面的错误:
严重: Servlet.service() for servlet [springServlet] in context with path [/bid] threw exception [Request processing failed; nested exception is org.springframework.beans.BeanInstantiationException: Could not instantiate bean class [java.util.List]: Specified class is an interface] with root cause
org.springframework.beans.BeanInstantiationException: Could not instantiate bean class [java.util.List]: Specified class is an interface
at org.springframework.beans.BeanUtils.instantiateClass(BeanUtils.java:101)
at org.springframework.web.method.annotation.ModelAttributeMethodProcessor.createAttribute(ModelAttributeMethodProcessor.java:132)
at org.springframework.web.servlet.mvc.method.annotation.ServletModelAttributeMethodProcessor.createAttribute(ServletModelAttributeMethodProcessor.java:81)
at org.springframework.web.method.annotation.ModelAttributeMethodProcessor.resolveArgument(ModelAttributeMethodProcessor.java:103)
at org.springframework.web.method.support.HandlerMethodArgumentResolverComposite.resolveArgument(HandlerMethodArgumentResolverComposite.java:75)
at org.springframework.web.method.support.InvocableHandlerMethod.getMethodArgumentValues(InvocableHandlerMethod.java:156)
0 请登录后投票
   发表时间:2013-01-11  
hxsmile 写道
sootv 写道
我不知道是不是你想要的结果,
我的理解是你要从界面传送多个user对象至后台,可以通过这样,解决
①封装user对象,List<User> user
②controller里面参数List<User> user
②界面上写比如 user[0].username,user[1].username,user[2].username
  传到后台之后,springMVC自动给user集合放入3个对象



我就是用这种方式获得页面的LIST,报下面的错误:
严重: Servlet.service() for servlet [springServlet] in context with path [/bid] threw exception [Request processing failed; nested exception is org.springframework.beans.BeanInstantiationException: Could not instantiate bean class [java.util.List]: Specified class is an interface] with root cause
org.springframework.beans.BeanInstantiationException: Could not instantiate bean class [java.util.List]: Specified class is an interface
at org.springframework.beans.BeanUtils.instantiateClass(BeanUtils.java:101)
at org.springframework.web.method.annotation.ModelAttributeMethodProcessor.createAttribute(ModelAttributeMethodProcessor.java:132)
at org.springframework.web.servlet.mvc.method.annotation.ServletModelAttributeMethodProcessor.createAttribute(ServletModelAttributeMethodProcessor.java:81)
at org.springframework.web.method.annotation.ModelAttributeMethodProcessor.resolveArgument(ModelAttributeMethodProcessor.java:103)
at org.springframework.web.method.support.HandlerMethodArgumentResolverComposite.resolveArgument(HandlerMethodArgumentResolverComposite.java:75)
at org.springframework.web.method.support.InvocableHandlerMethod.getMethodArgumentValues(InvocableHandlerMethod.java:156)

就看看这个东西,说实话,我功力不够深厚,没办法给出具体回复
但是,这种问题,最好把源码关联上,DEBUG一下,就会明白
0 请登录后投票
   发表时间:2013-01-11  
hxsmile 写道
sootv 写道
我不知道是不是你想要的结果,
我的理解是你要从界面传送多个user对象至后台,可以通过这样,解决
①封装user对象,List<User> user
②controller里面参数List<User> user
②界面上写比如 user[0].username,user[1].username,user[2].username
  传到后台之后,springMVC自动给user集合放入3个对象



我就是用这种方式获得页面的LIST,报下面的错误:
严重: Servlet.service() for servlet [springServlet] in context with path [/bid] threw exception [Request processing failed; nested exception is org.springframework.beans.BeanInstantiationException: Could not instantiate bean class [java.util.List]: Specified class is an interface] with root cause
org.springframework.beans.BeanInstantiationException: Could not instantiate bean class [java.util.List]: Specified class is an interface
at org.springframework.beans.BeanUtils.instantiateClass(BeanUtils.java:101)
at org.springframework.web.method.annotation.ModelAttributeMethodProcessor.createAttribute(ModelAttributeMethodProcessor.java:132)
at org.springframework.web.servlet.mvc.method.annotation.ServletModelAttributeMethodProcessor.createAttribute(ServletModelAttributeMethodProcessor.java:81)
at org.springframework.web.method.annotation.ModelAttributeMethodProcessor.resolveArgument(ModelAttributeMethodProcessor.java:103)
at org.springframework.web.method.support.HandlerMethodArgumentResolverComposite.resolveArgument(HandlerMethodArgumentResolverComposite.java:75)
at org.springframework.web.method.support.InvocableHandlerMethod.getMethodArgumentValues(InvocableHandlerMethod.java:156)

你可以把你的代码片段贴下帮你看下

难道你直接 @ModelAttribute("***")  List<***> list 这样吗?
0 请登录后投票
   发表时间:2013-01-12   最后修改:2013-01-12
这是我的最佳实践:
主从表的修改操作中,对于从表:
先把主表对应的明细全部删除.
然后把当前的明细全部INSERT.
insert 的时候要把没有主键的明细,给一个主键 (新增的明细没有主键),遗留的明细本事就有主键.同时避免了删除的明细的问题.
public void save(Order order,Map[] orderDetailMapArr,boolean isNew){
		String parentId=null;
		if(isNew){
			parentId=seqGenerator.generateNextSequence("order_info_id");
			order.setId(parentId);
			this.create(order);
		}else{
			parentId=order.getId();
			this.deleteOrderDetailById(parentId);
			this.modify(order);
		}
		this.batchSaveOrderDetail(parentId, orderDetailMapArr);	
	}
	private void batchSaveOrderDetail(String parentId,Map[] orderDetailMapArr){
		for(int i=0;i<orderDetailMapArr.length;i++){
			if(StringUtils.null2Str(orderDetailMapArr[i].get("orderDetailId")).equals("")){
				String orderDetailId=seqGenerator.generateNextSequence("order_detail_id");
				orderDetailMapArr[i].put("orderDetailId", orderDetailId);
			}
		}
		String insertStr="INSERT INTO ORDER_DETAIL_INFO(ID, IDX, PRODUCT_ID, PRODUCT_NAME, PRODUCT_PRICE, PRODUCT_UNIT, QUANTITY, AMOUNT) " +
				"values(:orderDetailId,'"+parentId+"',:productId,:productName,:productPrice,:productUnit,:quantity,:amount)";
		this.namedParameterJdbcTemplate.batchUpdate(insertStr, orderDetailMapArr);
	}


主表的操作就很简单了.
private void create(Order order){
		Timestamp now =new Timestamp(System.currentTimeMillis());
		order.setCreateTime(now);
		String insertStr="INSERT INTO ORDER_INFO(ID, CREATE_TIME, ORDER_STATE, CONSIGNEE, ADDRESS, TEL_NO, MOBILE_NO, EMAIL, PAY_TYPE,FREIGHT, EXPECT_RECEIVE_DATE, INVOICE_TYPE, INVOICE_TITLE, INVOICE_CONTENT)" +
				"VALUES(:id,:createTime,:orderState,:consignee,:address,:telNo,:mobileNo,:email,:payType,:freight,:expectReceiveDate,:invoiceType,:invoiceTitle,:invoiceContent)";
		SqlParameterSource ps = new BeanPropertySqlParameterSource(order);
		this.namedParameterJdbcTemplate.update(insertStr, ps);
	}
	private void modify(Order order){
		String updateStr="update  order_info set ORDER_STATE=:orderState,CONSIGNEE=:consignee,ADDRESS=:address,TEL_NO=:telNo," +
				"MOBILE_NO=:mobileNo,EMAIL=:email,PAY_TYPE=:payType,FREIGHT=:freight,EXPECT_RECEIVE_DATE=:expectReceiveDate," +
				"INVOICE_TYPE=:invoiceType,INVOICE_TITLE=:invoiceTitle,INVOICE_CONTENT=:invoiceContent where id=:id";
		SqlParameterSource ps=new BeanPropertySqlParameterSource(order);
		this.namedParameterJdbcTemplate.update(updateStr, ps);
	}

最后一个问题:怎么把明细汇聚成集合.
SpringMvc可能很麻烦,这是我框架里的做法:
Order order 是主表的,@ScalePara("orderDetailId") Map[] orderDetailMapArr 是二维子表对应的.
@Url("SaveNew.ajax")
	@Ajax
	public Data saveNewOrder(Order order,@ScalePara("orderDetailId") Map[] orderDetailMapArr){
		boolean isOk=true;
		try {
			orderDao.save(order,orderDetailMapArr,true);
		} catch (Exception e) {
			isOk=false;
			e.printStackTrace();
		}
		Data data=new Data();
		data.add("isOk", isOk);
		data.add("msg", isOk?"添加订单信息成功!":"操作失败,无法建立订单信息.系统出错 !");
		data.add("id", order.getId());
		return data;
	}
0 请登录后投票
   发表时间:2013-01-18  
我这里也报这个异常:

controller代码是:
bindData(WhereInfo[] wheres) {

}

请求参数是:
wheres[0].key='aaaa'&wheres[0].value="bbbbb"

报的异常时:
org.springframework.web.util.NestedServletException: Request processing failed; nested exception is org.springframework.beans.BeanInstantiationException: Could not instantiate bean class [[Lcom.mawujun.utils.page.WhereInfo;]: No default constructor found; nested exception is java.lang.NoSuchMethodException: [Lcom.mawujun.utils.page.WhereInfo;.<init>()
	at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:927)
	at org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:811)
	at javax.servlet.http.HttpServlet.service(HttpServlet.java:668)
	at org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:796)
	at org.springframework.test.web.servlet.TestDispatcherServlet.service(TestDispatcherServlet.java:66)
	at javax.servlet.http.HttpServlet.service(HttpServlet.java:770)
	at org.springframework.mock.web.MockFilterChain$ServletFilterProxy.doFilter(MockFilterChain.java:168)
	at org.springframework.mock.web.MockFilterChain.doFilter(MockFilterChain.java:136)
	at org.springframework.test.web.servlet.MockMvc.perform(MockMvc.java:134)
	at com.mawujun.mvc.SpringMVCControllerTest.bindPageRequestNormal(SpringMVCControllerTest.java:237)
	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
	at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
	at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
	at java.lang.reflect.Method.invoke(Method.java:601)
	at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:47)
	at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
	at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:44)
	at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
	at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:26)
	at org.springframework.test.context.junit4.statements.RunBeforeTestMethodCallbacks.evaluate(RunBeforeTestMethodCallbacks.java:74)
	at org.springframework.test.context.junit4.statements.RunAfterTestMethodCallbacks.evaluate(RunAfterTestMethodCallbacks.java:83)
	at org.springframework.test.context.junit4.statements.SpringRepeat.evaluate(SpringRepeat.java:72)
	at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:231)
	at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:88)
	at org.junit.runners.ParentRunner$3.run(ParentRunner.java:238)
	at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:63)
	at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:236)
	at org.junit.runners.ParentRunner.access$000(ParentRunner.java:53)
	at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:229)
	at org.springframework.test.context.junit4.statements.RunBeforeTestClassCallbacks.evaluate(RunBeforeTestClassCallbacks.java:61)
	at org.springframework.test.context.junit4.statements.RunAfterTestClassCallbacks.evaluate(RunAfterTestClassCallbacks.java:71)
	at org.junit.runners.ParentRunner.run(ParentRunner.java:309)
	at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.run(SpringJUnit4ClassRunner.java:174)
	at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:50)
	at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
	at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:467)
	at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:683)
	at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:390)
	at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:197)
Caused by: org.springframework.beans.BeanInstantiationException: Could not instantiate bean class [[Lcom.mawujun.utils.page.WhereInfo;]: No default constructor found; nested exception is java.lang.NoSuchMethodException: [Lcom.mawujun.utils.page.WhereInfo;.<init>()
	at org.springframework.beans.BeanUtils.instantiateClass(BeanUtils.java:108)
	at org.springframework.web.bind.annotation.support.HandlerMethodInvoker.resolveModelAttribute(HandlerMethodInvoker.java:770)
	at org.springframework.web.bind.annotation.support.HandlerMethodInvoker.resolveHandlerArguments(HandlerMethodInvoker.java:363)
	at org.springframework.web.bind.annotation.support.HandlerMethodInvoker.invokeHandlerMethod(HandlerMethodInvoker.java:171)
	at org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter.invokeHandlerMethod(AnnotationMethodHandlerAdapter.java:439)
	at org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter.handle(AnnotationMethodHandlerAdapter.java:427)
	at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:925)
	at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:856)
	at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:915)
	... 38 more
Caused by: java.lang.NoSuchMethodException: [Lcom.mawujun.utils.page.WhereInfo;.<init>()
	at java.lang.Class.getConstructor0(Class.java:2721)
	at java.lang.Class.getDeclaredConstructor(Class.java:2002)
	at org.springframework.beans.BeanUtils.instantiateClass(BeanUtils.java:105)
	... 46 more


0 请登录后投票
   发表时间:2013-02-19  
jinnianshilongnian 写道
hxsmile 写道
sootv 写道
我不知道是不是你想要的结果,
我的理解是你要从界面传送多个user对象至后台,可以通过这样,解决
①封装user对象,List<User> user
②controller里面参数List<User> user
②界面上写比如 user[0].username,user[1].username,user[2].username
  传到后台之后,springMVC自动给user集合放入3个对象



我就是用这种方式获得页面的LIST,报下面的错误:
严重: Servlet.service() for servlet [springServlet] in context with path [/bid] threw exception [Request processing failed; nested exception is org.springframework.beans.BeanInstantiationException: Could not instantiate bean class [java.util.List]: Specified class is an interface] with root cause
org.springframework.beans.BeanInstantiationException: Could not instantiate bean class [java.util.List]: Specified class is an interface
at org.springframework.beans.BeanUtils.instantiateClass(BeanUtils.java:101)
at org.springframework.web.method.annotation.ModelAttributeMethodProcessor.createAttribute(ModelAttributeMethodProcessor.java:132)
at org.springframework.web.servlet.mvc.method.annotation.ServletModelAttributeMethodProcessor.createAttribute(ServletModelAttributeMethodProcessor.java:81)
at org.springframework.web.method.annotation.ModelAttributeMethodProcessor.resolveArgument(ModelAttributeMethodProcessor.java:103)
at org.springframework.web.method.support.HandlerMethodArgumentResolverComposite.resolveArgument(HandlerMethodArgumentResolverComposite.java:75)
at org.springframework.web.method.support.InvocableHandlerMethod.getMethodArgumentValues(InvocableHandlerMethod.java:156)

你可以把你的代码片段贴下帮你看下

难道你直接 @ModelAttribute("***")  List<***> list 这样吗?


我也碰到这样的问题,spring mvc controller好像不支持List等集合作为参数,只支持基本类型作为参数的?
0 请登录后投票
   发表时间:2013-02-19  
jinnianshilongnian 写道
hxsmile 写道
sootv 写道
我不知道是不是你想要的结果,
我的理解是你要从界面传送多个user对象至后台,可以通过这样,解决
①封装user对象,List<User> user
②controller里面参数List<User> user
②界面上写比如 user[0].username,user[1].username,user[2].username
  传到后台之后,springMVC自动给user集合放入3个对象



我就是用这种方式获得页面的LIST,报下面的错误:
严重: Servlet.service() for servlet [springServlet] in context with path [/bid] threw exception [Request processing failed; nested exception is org.springframework.beans.BeanInstantiationException: Could not instantiate bean class [java.util.List]: Specified class is an interface] with root cause
org.springframework.beans.BeanInstantiationException: Could not instantiate bean class [java.util.List]: Specified class is an interface
at org.springframework.beans.BeanUtils.instantiateClass(BeanUtils.java:101)
at org.springframework.web.method.annotation.ModelAttributeMethodProcessor.createAttribute(ModelAttributeMethodProcessor.java:132)
at org.springframework.web.servlet.mvc.method.annotation.ServletModelAttributeMethodProcessor.createAttribute(ServletModelAttributeMethodProcessor.java:81)
at org.springframework.web.method.annotation.ModelAttributeMethodProcessor.resolveArgument(ModelAttributeMethodProcessor.java:103)
at org.springframework.web.method.support.HandlerMethodArgumentResolverComposite.resolveArgument(HandlerMethodArgumentResolverComposite.java:75)
at org.springframework.web.method.support.InvocableHandlerMethod.getMethodArgumentValues(InvocableHandlerMethod.java:156)

你可以把你的代码片段贴下帮你看下

难道你直接 @ModelAttribute("***")  List<***> list 这样吗?

这是我的controller
@RequestMapping
	@ResponseBody
	public Map<Long, GameInfo> getMapGame(List<Long>  ids) throws Exception

异常:
   org.springframework.beans.BeanInstantiationException: Could not instantiate bean class [java.util.List]: Specified class is an interface
0 请登录后投票
   发表时间:2013-02-20  
定义一个form类就行了,
class Form {
  List<User> users;
}
Controller的方法:
public String test(Form form){
}
页面就是
users[0].id
users[0].name

users[1].id
users[1].name


因为SpringMvc查找在Controller方法里面的参数,如果是基本类型,就是根据名称去绑定表单数据,如果不是,就视为Form类,试图实例化一个它的对象,然后根据对象的property名称去绑定相应表单项的的数据。你在方法参数里定义一个List,当然就会报Could not instantiate bean class [java.util.List]的错误
0 请登录后投票
   发表时间:2013-03-12  
jinnianshilongnian 写道
hxsmile 写道
sootv 写道
我不知道是不是你想要的结果,
我的理解是你要从界面传送多个user对象至后台,可以通过这样,解决
①封装user对象,List<User> user
②controller里面参数List<User> user
②界面上写比如 user[0].username,user[1].username,user[2].username
  传到后台之后,springMVC自动给user集合放入3个对象



我就是用这种方式获得页面的LIST,报下面的错误:
严重: Servlet.service() for servlet [springServlet] in context with path [/bid] threw exception [Request processing failed; nested exception is org.springframework.beans.BeanInstantiationException: Could not instantiate bean class [java.util.List]: Specified class is an interface] with root cause
org.springframework.beans.BeanInstantiationException: Could not instantiate bean class [java.util.List]: Specified class is an interface
at org.springframework.beans.BeanUtils.instantiateClass(BeanUtils.java:101)
at org.springframework.web.method.annotation.ModelAttributeMethodProcessor.createAttribute(ModelAttributeMethodProcessor.java:132)
at org.springframework.web.servlet.mvc.method.annotation.ServletModelAttributeMethodProcessor.createAttribute(ServletModelAttributeMethodProcessor.java:81)
at org.springframework.web.method.annotation.ModelAttributeMethodProcessor.resolveArgument(ModelAttributeMethodProcessor.java:103)
at org.springframework.web.method.support.HandlerMethodArgumentResolverComposite.resolveArgument(HandlerMethodArgumentResolverComposite.java:75)
at org.springframework.web.method.support.InvocableHandlerMethod.getMethodArgumentValues(InvocableHandlerMethod.java:156)

你可以把你的代码片段贴下帮你看下

难道你直接 @ModelAttribute("***")  List<***> list 这样吗?



Controller的方法体
public ModelAndView updateBidForm(List<Tc03> tc03){

页面的表单
name="tc03[${s.index}].f01"   name="tc03[${s.index}].f02"  等等
Tc03是对象,麻烦看看这样写对吗
0 请登录后投票
   发表时间:2013-03-13  
jinnianshilongnian 写道
hellostory 写道
sootv 写道
我不知道是不是你想要的结果,
我的理解是你要从界面传送多个user对象至后台,可以通过这样,解决
①封装user对象,List<User> user
②controller里面参数List<User> user
②界面上写比如 user[0].username,user[1].username,user[2].username
  传到后台之后,springMVC自动给user集合放入3个对象

个人追问下:对于总单明细(主从表)更新操作(同时保存)时,后台如何判断哪行是新增的,哪行是修改后的,还有哪行已经被删除了?一直没有比较合适优雅的解决方法,你们是如何处理的?


可不可以这样,通过js操作:

1、使用div/tr 标识每一行
2、如果是修改  在tr/div中加入update标识
3、如果是删除,在tr/div中加上delete标识
4、提交表单前 删除其他的tr 然后submit

这个其实没有那么复杂  只要判断当前行时候存在主键ID就可以  对于多对象提交到后台我个人是通过json实现的   到后台转化为对象  判断Id然进行增加还是修改  你想实现的这种东西我做过了 如果不明白的话 我们可以交流下
0 请登录后投票
论坛首页 Java企业应用版

跳转论坛:
Global site tag (gtag.js) - Google Analytics