Struts2-列表键无法解析

时间:2018-12-19 09:53:07

标签: java web struts2

am正在使用Struts 2单选按钮。我想从动作类中检索列表,但出现以下错误

这些是我的文件:

  

struts.xml-

<action name="radioButtonAction" 
     class="com.mkyong.common.action.RadioButtonAction" method="display">
    <result name="none">/radiobutton.jsp</result>
</action>

<action name="resultAction" class="com.mkyong.common.action.RadioButtonAction">
    <result name="success">/result.jsp</result>
</action>

  

radiobutton.jsp-

<body>
<h1>Struts 2 radio button example</h1>
<s:form action="resultAction" namespace="/">
<h2>
<s:radio label="Gender" name="yourGender" list="genders" value="defaultGenderValue" /> 
<s:radio label="Gender" name="yourLanguage" list="languages" 
  listKey="languageCode" listValue="languageDisplay" value="defaultLanguageValue" />     
<s:radio label="Answer" name="yourAnswer" list="#{'1':'Yes','2':'No'}" value="2" />
</h2> 
<s:submit value="submit" name="submit" />   
</s:form>
</body>
  

RadioButtonAction.java-

package com.mkyong.common.action;

import java.util.ArrayList;
import java.util.List;

import com.mkyong.common.model.Language;
import com.opensymphony.xwork2.ActionSupport;

public class RadioButtonAction extends ActionSupport{

private List<String> genders;
private List<Language> languages ;

private String yourGender;
private String yourAnswer;
private String yourLanguage;

private static final String MALE = "male";
private static final String FEMALE = "female";
private static final String UNKNOWN = "unknown";

public RadioButtonAction(){

    genders = new ArrayList<String>();
    genders.add(MALE);
    genders.add(FEMALE);
    genders.add(UNKNOWN);

    languages = new ArrayList<Language>();
    languages.add( new Language("EN", "English") );
    languages.add( new Language("FR", "France") );
    languages.add( new Language("CN_ZH", "Chinese") );
    languages.add( new Language("DE", "German") );

}

//return default gender value
public String getDefaultGenderValue(){
    return UNKNOWN;
}

//return default language value
public String getDefaultLanguageValue(){
    return "CN_ZH";
}

public String execute() {
    return SUCCESS;
}
public String display() {
    return NONE;
}

//getter ad setter methods
}
  

Languge.java-

package com.mkyong.common.model;

public class Language{

private String languageCode;
private String languageDisplay;

//getter and setter methods

public Language(String languageCode, String languageDisplay) {
    this.languageCode = languageCode;
    this.languageDisplay = languageDisplay;
}
}

我所做的任何事情,仍然存在相同的错误,我知道为什么会这样...

SCHWERWIEGEND: Servlet.service() for servlet [jsp] in context with path [/RadioButton] threw exception [An exception occurred processing [radiobutton.jsp] at line [13]

10: 
11: <h2>
12:   <s:radio label="Gender" name="yourGender" list="genders"  value="defaultGenderValue" />
13:   
14:   <s:radio label="Gender" name="yourLanguage" list="languages" 
15:       listKey="languageCode" listValue="languageDisplay" value="defaultLanguageValue" />
16:   
Stacktrace:] with root cause
tag 'radio', field 'list', name 'yourGender': The requested list key    'genders' could not be resolved as a collection/array/map/enumeration/iterator type. Example: people or people.{name} - [unknown location]
at org.apache.struts2.components.Component.fieldError(Component.java:257)
at org.apache.struts2.components.Component.findValue(Component.java:353)
at org.apache.struts2.components.ListUIBean.evaluateExtraParams(ListUIBean.java:81)
at org.apache.struts2.components.Radio.evaluateExtraParams(Radio.java:70)
at org.apache.struts2.components.UIBean.evaluateParams(UIBean.java:884)
at org.apache.struts2.components.UIBean.end(UIBean.java:533)
at org.apache.struts2.views.jsp.ComponentTagSupport.doEndTag(ComponentTagSupport.java:39)
at org.apache.jsp.radiobutton_jsp._jspx_meth_s_005fradio_005f0(radiobutton_jsp.java:249)
at org.apache.jsp.radiobutton_jsp._jspx_meth_s_005fform_005f0(radiobutton_jsp.java:192)
at org.apache.jsp.radiobutton_jsp._jspService(radiobutton_jsp.java:143)
at org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:741)
at org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:477)
at org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:386)
at org.apache.jasper.servlet.JspServlet.service(JspServlet.java:330)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:741)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:231)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:53)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
at org.apache.struts2.dispatcher.filter.StrutsPrepareAndExecuteFilter.doFilter(StrutsPrepareAndExecuteFilter.java:138)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:199)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:96)
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:490)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:139)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:92)
at org.apache.catalina.valves.AbstractAccessLogValve.invoke(AbstractAccessLogValve.java:668)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:74)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:343)
at org.apache.coyote.http11.Http11Processor.service(Http11Processor.java:408)
at org.apache.coyote.AbstractProcessorLight.process(AbstractProcessorLight.java:66)
at org.apache.coyote.AbstractProtocol$ConnectionHandler.process(AbstractProtocol.java:791)
at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1417)
at org.apache.tomcat.util.net.SocketProcessorBase.run(SocketProcessorBase.java:49)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)
at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
at java.lang.Thread.run(Thread.java:748)    

如何解决这个问题?有人有主意吗?

1 个答案:

答案 0 :(得分:1)

您需要用于性别语言的吸气剂方法。

public List getGenders() {
    return genders;
}

与语言相同。