struts2 OGNL实例化数组的问题.

我在Action中使用了:

private  SingerDto[] singerDtos;

 定义了getter和setter方法:

public   void  setSingerDtos(SingerDto[] singerDtos) {
  
this .singerDtos  =  singerDtos;
 }
    
 
public   SingerDto[]  getSingerDtos() {
  
return  singerDtos;
 }

我在页面表单中使用:

歌手姓名: < input  type ="text"  name ="singerDtos[0].name" >< br >
< select  name ="singerDtos[0].region" >
           
< option  value ="china"  selected > 华人歌手 </ option >
            
< option  value ="korea" > 韩国歌手 </ option >
            
< option  value ="japan" > 日本歌手 </ option >
            
< option  value ="occident" > 欧美歌手 </ option >
 
</ select >

结果抛出异常:

66218 [http-8080-Processor24] DEBUG com.opensymphony.xwork2.interceptor.ParametersInterceptor  - Setting params bandName => [  ] singerDtos[0].region => [ china ] singerDtos[0].name => [ fdsfd ] uploadContentType => [ audio/mp3 ] singerDtos[0].sex => [ m ] songName => [ ddd ] type => [ 1 ] uploadFileName => [ 20076857868205.mp3 ] upload => [ C:/Program Files/apache-tomcat-5.5.25/work/Catalina/localhost/Music/upload_25bd70bb_115a4017d5e__8000_00000001.tmp ]
66234 [http-8080-Processor24] DEBUG com.opensymphony.xwork2.util.XWorkConverter  - Property: bandName
66234 [http-8080-Processor24] DEBUG com.opensymphony.xwork2.util.XWorkConverter  - Class: edu.jlu.fuliang.action.UploadSongAction
66250 [http-8080-Processor24] DEBUG com.opensymphony.xwork2.util.XWorkConverter  - converter is null for property bandName. Mapping size: 0
66250 [http-8080-Processor24] DEBUG com.opensymphony.xwork2.util.XWorkConverter  - field-level type converter for property [bandName] = none found
66250 [http-8080-Processor24] DEBUG com.opensymphony.xwork2.util.XWorkConverter  - global-level type converter for property [bandName] = none found
66250 [http-8080-Processor24] DEBUG com.opensymphony.xwork2.util.XWorkConverter  - falling back to default type converter [com.opensymphony.xwork2.util.XWorkBasicConverter@1cbcc56]
66250 [http-8080-Processor24] DEBUG com.opensymphony.xwork2.util.InstantiatingNullHandler  - Entering nullPropertyValue [target=[edu.jlu.fuliang.action.UploadSongAction@d43997, com.opensymphony.xwork2.DefaultTextProvider@4486de], property=singerDtos]
66265 [http-8080-Processor24] ERROR com.opensymphony.xwork2.util.InstantiatingNullHandler  - Could not create and/or set value back on to object
org.springframework.beans.factory.BeanCreationException: Error creating bean with name '[Ledu.jlu.fuliang.dto.SingerDto;': Could not resolve matching constructor
 at org.springframework.beans.factory.support.ConstructorResolver.autowireConstructor(ConstructorResolver.java:178)
 at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.autowireConstructor(AbstractAutowireCapableBeanFactory.java:800)
 at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.autowire(AbstractAutowireCapableBeanFactory.java:258)
 at com.opensymphony.xwork2.spring.SpringObjectFactory.buildBean(SpringObjectFactory.java:145)
 at com.opensymphony.xwork2.util.InstantiatingNullHandler.createObject(InstantiatingNullHandler.java:123)
 at com.opensymphony.xwork2.util.InstantiatingNullHandler.nullPropertyValue(InstantiatingNullHandler.java:104)
 at ognl.ASTProperty.getValueBody(ASTProperty.java:94)
 at ognl.SimpleNode.evaluateGetValueBody(SimpleNode.java:170)
 at ognl.SimpleNode.getValue(SimpleNode.java:210)
 at ognl.ASTChain.setValueBody(ASTChain.java:168)
 at ognl.SimpleNode.evaluateSetValueBody(SimpleNode.java:177)
 at ognl.SimpleNode.setValue(SimpleNode.java:246)

我们发现ParametersInterceptor被调用后,委托给SpringObjectFactory来实例化Ledu.jlu.fuliang.dto.SingerDtoorg.springframework.beans.factory.BeanCreationException: Error creating bean with name '[Ledu.jlu.fuliang.dto.SingerDto;': Could not resolve matching constructor结果实例化失败.SingerDto应该不受Spring ioc容器管理吧,难道是这个问题,在struts 2 参考文档中没有找到类似的例子,后来我把SingerDto[] singerDtos;改成List<SingerDto> singerDtos,结果好使了.我又在网上搜搜struts2是否支持数组,结果在一个网站上搜到这个结果:

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.

作者同样使用了List<Integer>代替了Integer[]结果成功.struts2是否支持创建数组还是一个bug,现在还不知道,但List来代替数组就可以避免这个的错误.

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值