fuliangliang的Blog

合抱之木,生于毫末;九层之台,起于累土;千里之行,始于足下。

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来代替数组就可以避免这个的错误.

阅读更多
想对作者说点什么? 我来说一句

没有更多推荐了,返回首页

加入CSDN,享受更精准的内容推荐,与500万程序员共同成长!
关闭
关闭