struts2报错:Could not resolve matching constructor 与 通过form提交的方式向后台传数组...

后台Action中有String数组expPros:
private String[] expPros;
///get set


在form表单中试图通过以下方式向action中expPros赋值时:
<input   type="text"   name="expPros[0]"   value="extProp1_VALUE">   
<input type="text" name="expPros[1]" value="extProp2_VALUE">
<input type="text" name="expPros[2]" value="extProp3_VALUE">


结果报错:[quote]org.springframework.beans.factory.BeanCreationException: Error creating bean with name '[Ljava.lang.String;': Could not resolve matching constructor[/quote]

如果写成:
<input   type="text"   name="expPros[]"   value="extProp1_VALUE">   
<input type="text" name="expPros[]" value="extProp2_VALUE">
<input type="text" name="expPros[]" value="extProp3_VALUE">


就不会抛上面错误了,但三个input输入框的值不会被赋给expPros:[quote]01/15/2011 01:40:19.203 ERROR ParametersInterceptor,204 - ParametersInterceptor - [setParameters]: Unexpected Exception caught setting 'expPros[]' on 'class com.sgai.itms.equipment.web.ItmsEquipDetailsAction: Error setting expression 'expPros[]' with value '[Ljava.lang.String;@2a519b'

String[] arr = request.getParameterValues("expPros");
///arr为null

[/quote]


具体原因:
struts2 OGNL实例化数组的问题.
[url]http://blog.csdn.net/fuliangliang/archive/2007/10/15/1826560.aspx[/url][quote]Struts doesn't support array creation due to an issue with size determination. Kinda what I though earlier. So, I've changed it over to use a List<Integer> and that works fine.

The docs are very misleading on this point. I'd be more than happy to clean up the type conversion doc and also create an collection handling doc to help folks out with this issue. Let me know if I can help out at all.[/quote]


据此得出,通过struts2的action(或直接使用request)的方式向后台传java 数组的正确方式是各个input的name都取成后台数组名即可(如这里的expPros),[color=red][size=medium][b]不可以加[][/b][/size][/color]:[quote][b]三个input的值可以被传给Action中的数组expPros;并且也可以通过[color=red]request.getParameterValues("expPros")[/color]的方式得到一个String数组[/b]
<input   type="text"   name="expPros"   value="extProp1_VALUE">   
<input type="text" name="expPros" value="extProp2_VALUE">
<input type="text" name="expPros" value="extProp3_VALUE">

String[] arr = request.getParameterValues("expPros"); /// 同Action的属性expPros一样,arr也为"[extProp1_VALUE, extProp2_VALUE, extProp3_VALUE]"
[/quote]
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值