Struts2将列表转换为整数或双精度不起作用

时间:2013-09-03 06:09:27

标签: java struts2

我是Struts2的新手,并且正在使用Struts2来学习它。我在书中找到了一个示例,它将请求参数映射到列表,同时尝试将参数从默认类型字符串转换为double或integer。它假定列表没有类型信息(如java 5之前)。我正在例外。

的Login.jsp

 <s:form  action="LoginAction">
    <s:textfield name="username" label="Username"></s:textfield>
    <s:password name="password" label="Password"></s:password>
    <s:textfield name="age" label="Age"></s:textfield>
    <s:textfield name="name" label="First Name"></s:textfield>
    <s:textfield name="name" label="Second Name"></s:textfield>
    <s:textfield name="name" label="Last Name"></s:textfield>
    <s:submit value="Login"></s:submit>
</s:form>

的LoginAction

package org.shammu.actions;

import java.util.List;

import com.opensymphony.xwork2.ActionSupport;

@SuppressWarnings("serial")
public class LoginAction extends ActionSupport{

private String username;
private String password;
private int age;
private List name;
public String getUsername() {
    return username;
}
public void setUsername(String username) {
    this.username = username;
}
public String getPassword() {
    return password;
}
public void setPassword(String password) {
    this.password = password;
}
public List getName() {
    return name;
}
public void setName(List name) {
    this.name = name;
}
public int getAge() {
    return age;
}
public void setAge(int age) {
    this.age = age;
}

}

堆栈跟踪

Sep 3, 2013 11:06:22 AM      
com.opensymphony.xwork2.conversion.impl.DefaultConversionFileProcessor error
SEVERE: Problem loading properties for org.shammu.actions.LoginAction
java.lang.InstantiationException: java.lang.Double
at java.lang.Class.newInstance0(Unknown Source)
at java.lang.Class.newInstance(Unknown Source)
at com.opensymphony.xwork2.ObjectFactory.buildBean(ObjectFactory.java:131)
at com.opensymphony.xwork2.ObjectFactory.buildBean(ObjectFactory.java:162)
at com.opensymphony.xwork2.ObjectFactory.buildBean(ObjectFactory.java:151)
at com.opensymphony.xwork2.conversion.impl.DefaultTypeConverterCreator.
       createTypeConverter(DefaultTypeConverterCreator.java:23)
at com.opensymphony.xwork2.conversion.impl.DefaultConversionFileProcessor.
       process(DefaultConversionFileProcessor.java:68)
at com.opensymphony.xwork2.conversion.impl.XWorkConverter.addConverterMapping
       (XWorkConverter.java:485)
at com.opensymphony.xwork2.conversion.impl.XWorkConverter.buildConverterMapping
       (XWorkConverter.java:549)
at com.opensymphony.xwork2.conversion.impl.XWorkConverter.getConverter
       (XWorkConverter.java:417)
at com.opensymphony.xwork2.conversion.impl.XWorkConverter.convertValue
       (XWorkConverter.java:282)
at com.opensymphony.xwork2.ognl.OgnlTypeConverterWrapper.convertValue
       (OgnlTypeConverterWrapper.java:39)
at ognl.OgnlRuntime.getConvertedType(OgnlRuntime.java:1080)
at ognl.OgnlRuntime.getConvertedTypes(OgnlRuntime.java:1097)
at ognl.OgnlRuntime.getConvertedMethodAndArgs(OgnlRuntime.java:1123)
at ognl.OgnlRuntime.getAppropriateMethod(OgnlRuntime.java:1209)
at ognl.OgnlRuntime.callAppropriateMethod(OgnlRuntime.java:1223)
at ognl.OgnlRuntime.setMethodValue(OgnlRuntime.java:1494)
at ognl.ObjectPropertyAccessor.setPossibleProperty(ObjectPropertyAccessor.java:85)
at ognl.ObjectPropertyAccessor.setProperty(ObjectPropertyAccessor.java:162)
at com.opensymphony.xwork2.ognl.accessor.ObjectAccessor.setProperty
       (ObjectAccessor.java:27)
at ognl.OgnlRuntime.setProperty(OgnlRuntime.java:2332)
at com.opensymphony.xwork2.ognl.accessor.CompoundRootAccessor.setProperty
       (CompoundRootAccessor.java:78)
at ognl.OgnlRuntime.setProperty(OgnlRuntime.java:2332)
at ognl.ASTProperty.setValueBody(ASTProperty.java:127)
at ognl.SimpleNode.evaluateSetValueBody(SimpleNode.java:220)
at ognl.SimpleNode.setValue(SimpleNode.java:301)
at ognl.Ognl.setValue(Ognl.java:737)
at com.opensymphony.xwork2.ognl.OgnlUtil.setValue(OgnlUtil.java:234)
at com.opensymphony.xwork2.ognl.OgnlValueStack.trySetValue(OgnlValueStack.java:183)
at com.opensymphony.xwork2.ognl.OgnlValueStack.setValue(OgnlValueStack.java:170)
at com.opensymphony.xwork2.ognl.OgnlValueStack.setParameter 
       (OgnlValueStack.java:148)
at com.opensymphony.xwork2.interceptor.ParametersInterceptor.setParameters
       (ParametersInterceptor.java:318)
at com.opensymphony.xwork2.interceptor.ParametersInterceptor.doIntercept
       (ParametersInterceptor.java:231)
at com.opensymphony.xwork2.interceptor.MethodFilterInterceptor.intercept
       (MethodFilterInterceptor.java:98)
at com.opensymphony.xwork2.DefaultActionInvocation.invoke
       (DefaultActionInvocation.java:246)
at com.opensymphony.xwork2.interceptor.ParametersInterceptor.doIntercept
       (ParametersInterceptor.java:239)
at com.opensymphony.xwork2.interceptor.MethodFilterInterceptor.intercept
       (MethodFilterInterceptor.java:98)
at com.opensymphony.xwork2.DefaultActionInvocation.invoke
       (DefaultActionInvocation.java:246)
at com.opensymphony.xwork2.interceptor.StaticParametersInterceptor.intercept
       (StaticParametersInterceptor.java:191)
at com.opensymphony.xwork2.DefaultActionInvocation.invoke
       (DefaultActionInvocation.java:246)
at org.apache.struts2.interceptor.MultiselectInterceptor.intercept
        (MultiselectInterceptor.java:73)
at com.opensymphony.xwork2.DefaultActionInvocation.invoke
       (DefaultActionInvocation.java:246)
at org.apache.struts2.interceptor.CheckboxInterceptor.intercept
       (CheckboxInterceptor.java:91)
at com.opensymphony.xwork2.DefaultActionInvocation.invoke
       (DefaultActionInvocation.java:246)
at org.apache.struts2.interceptor.FileUploadInterceptor.intercept
       (FileUploadInterceptor.java:252)
at com.opensymphony.xwork2.DefaultActionInvocation.invoke
       (DefaultActionInvocation.java:246)
at com.opensymphony.xwork2.interceptor.ModelDrivenInterceptor.intercept
       (ModelDrivenInterceptor.java:100)
at com.opensymphony.xwork2.DefaultActionInvocation.invoke
       (DefaultActionInvocation.java:246)
at com.opensymphony.xwork2.interceptor.ScopedModelDrivenInterceptor.intercept
       (ScopedModelDrivenInterceptor.java:141)
at com.opensymphony.xwork2.DefaultActionInvocation.invoke
       (DefaultActionInvocation.java:246)
at com.opensymphony.xwork2.interceptor.ChainingInterceptor.intercept
       (ChainingInterceptor.java:145)
at com.opensymphony.xwork2.DefaultActionInvocation.invoke
      (DefaultActionInvocation.java:246)
at com.opensymphony.xwork2.interceptor.PrepareInterceptor.doIntercept
      (PrepareInterceptor.java:171)
at com.opensymphony.xwork2.interceptor.MethodFilterInterceptor.intercept
      (MethodFilterInterceptor.java:98)
at com.opensymphony.xwork2.DefaultActionInvocation.invoke
      (DefaultActionInvocation.java:246)
at com.opensymphony.xwork2.interceptor.I18nInterceptor.intercept
      (I18nInterceptor.java:161)
at com.opensymphony.xwork2.DefaultActionInvocation.invoke
      (DefaultActionInvocation.java:246)
at org.apache.struts2.interceptor.ServletConfigInterceptor.intercept
      (ServletConfigInterceptor.java:164)
at com.opensymphony.xwork2.DefaultActionInvocation.invoke
      (DefaultActionInvocation.java:246)
at com.opensymphony.xwork2.interceptor.AliasInterceptor.intercept
     (AliasInterceptor.java:193)
at com.opensymphony.xwork2.DefaultActionInvocation.invoke
      (DefaultActionInvocation.java:246)
at com.opensymphony.xwork2.interceptor.ExceptionMappingInterceptor.intercept
     (ExceptionMappingInterceptor.java:189)
at com.opensymphony.xwork2.DefaultActionInvocation.invoke
       (DefaultActionInvocation.java:246)
at org.apache.struts2.impl.StrutsActionProxy.execute(StrutsActionProxy.java:54)
at org.apache.struts2.dispatcher.Dispatcher.serviceAction(Dispatcher.java:563)
at org.apache.struts2.dispatcher.ng.ExecuteOperations.executeAction
    (ExecuteOperations.java:77)
at org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter.doFilter
    (StrutsPrepareAndExecuteFilter.java:99)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter
    (ApplicationFilterChain.java:243)
at org.apache.catalina.core.ApplicationFilterChain.doFilter
     (ApplicationFilterChain.java:210)
at org.apache.catalina.core.StandardWrapperValve.invoke
     (StandardWrapperValve.java:222)
at org.apache.catalina.core.StandardContextValve.invoke
    (StandardContextValve.java:123)
at org.apache.catalina.authenticator.AuthenticatorBase.invoke
    (AuthenticatorBase.java:472)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:171)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:99)
at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:953)
at org.apache.catalina.core.StandardEngineValve.invoke
    (StandardEngineValve.java:118)
at org.apache.catalina.connector.CoyoteAdapter.service
    (CoyoteAdapter.java:408)
at org.apache.coyote.http11.AbstractHttp11Processor.process
    (AbstractHttp11Processor.java:1023)
at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process
       (AbstractProtocol.java:589)
at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:312)
at java.util.concurrent.ThreadPoolExecutor$Worker.runTask(Unknown Source)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
at java.lang.Thread.run(Unknown Source)

此处,我的转化文件位于动作类的同一文件夹中 LoginAction-conversion.properties 只有一行Element-name=java.lang.Double。 帮帮我

2 个答案:

答案 0 :(得分:2)

这应该是Element_name=java.lang.Double

答案 1 :(得分:0)

LoginAction-conversion.properties文件中,您实际上并不知道结果类型需要什么,但必须使用哪个转换器。创建转换器以扩展以下类的最简单方法: org.apache.struts2.util.StrutsTypeConverter

您必须为以下方法提供实现: convertFromString convertToString

在您的属性文件中,您会有类似

的内容

年龄= <<your extension of org.apache.struts2.util.StrutsTypeConverter>>

您还应该查看以下指南:Struts 2.3.x TypeConversion