`

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"> 


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


如果写成:
<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:
引用
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




具体原因:
struts2 OGNL实例化数组的问题.
http://blog.csdn.net/fuliangliang/archive/2007/10/15/1826560.aspx
引用
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.



据此得出,通过struts2的action(或直接使用request)的方式向后台传java 数组的正确方式是各个input的name都取成后台数组名即可(如这里的expPros),不可以加[]
引用
三个input的值可以被传给Action中的数组expPros;并且也可以通过request.getParameterValues("expPros")的方式得到一个String数组
<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]"
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics