1

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

登录.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>

登录动作

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。帮我

4

2 回答 2

2

这应该是Element_name=java.lang.Double.

于 2013-09-03T11:08:59.910 回答
0

在您的LoginAction-conversion.properties文件中,您实际上并没有告诉结果类型需要是什么,而是必须使用什么转换器。创建转换器以扩展以下类的最简单方法: org.apache.struts2.util.StrutsTypeConverter

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

在你的属性文件中,你会有类似的东西

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

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

于 2013-09-03T11:58:49.190 回答