我是 struts2 的新手,在提交表单和使用验证时出现错误。
查看我的 struts.xml
<action name="alltagpage" class="com.ksh.tags.Initialization">
<result name="success">/alltagspage.jsp</result>
</action>
<action name="submittags" class="com.ksh.tags.TagActionClass">
<result name="input">/alltagspage.jsp</result>
<result name="success">/tagsresult.jsp</result>
</action>
当我传递这个 alltagpage 请求时,它正在初始化“初始化”类并初始化单选按钮的内容。
public class Initialization {
private List<String> languages;
private List<KeyValue> genders;
public Initialization()
{
System.out.println("Model COnstructor is called");
languages = new ArrayList<String>();
languages.add("English");
languages.add("Hindi");
languages.add("Gujarati");
KeyValue keyValue0 = new KeyValue();
keyValue0.setKey(0);
keyValue0.setValue("Male");
KeyValue keyValue1 = new KeyValue();
keyValue1.setKey(1);
keyValue1.setValue("Female");
KeyValue keyValue2 = new KeyValue();
keyValue2.setKey(2);
keyValue2.setValue("Unknown");
genders = new ArrayList<KeyValue>();
genders.add(keyValue0);
genders.add(keyValue1);
genders.add(keyValue2);
}
getters and setters..
public String execute()
{
return "success";
}
}
以下是我提交表格时将调用的课程。
public class TagActionClass extends ActionSupport implements ModelDriven<Model> {
Initialization initialization = new Initialization();
Model model = new Model();
public String execute()
{
return "success";
}
@Override
public Model getModel()
{
return model;
}
public void validate()
{
System.out.println("Kshitij :"+ getModel().getTextfield());
System.out.println("Kshitij :"+ getModel().getSelectedlanguage());
System.out.println("Kshitij :"+ getModel().getSelectedgender());
if(getModel().getTextfield() == null || getModel().getTextfield().isEmpty())
{
addActionError("Text Field is empty");
}
}
}
当我提交带有空用户名的表单时,它会触发以下错误。我知道为什么会这样。我需要解决方案。
org.apache.jasper.JasperException: tag 'radio', field 'list', name 'selectedlanguage': The requested list key 'languages' could not be resolved as a collection/array/map/enumeration/iterator type. Example: people or people.{name} - [unknown location]
org.apache.jasper.servlet.JspServletWrapper.handleJspException(JspServletWrapper.java:534)
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:457)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:390)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:333)
javax.servlet.http.HttpServlet.service(HttpServlet.java:722)
org.apache.struts2.dispatcher.ServletDispatcherResult.doExecute(ServletDispatcherResult.java:159)
org.apache.struts2.dispatcher.StrutsResultSupport.execute(StrutsResultSupport.java:186)
com.opensymphony.xwork2.DefaultActionInvocation.executeResult(DefaultActionInvocation.java:373)
com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:277)
com.opensymphony.xwork2.validator.ValidationInterceptor.doIntercept(ValidationInterceptor.java:263)
org.apache.struts2.interceptor.validation.AnnotationValidationInterceptor.doIntercept(AnnotationValidationInterceptor.java:68)
com.opensymphony.xwork2.interceptor.MethodFilterInterceptor.intercept(MethodFilterInterceptor.java:98)
com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:248)
com.opensymphony.xwork2.interceptor.ConversionErrorInterceptor.intercept(ConversionErrorInterceptor.java:133)
com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:248)
com.opensymphony.xwork2.interceptor.ParametersInterceptor.doIntercept(ParametersInterceptor.java:207)
com.opensymphony.xwork2.interceptor.MethodFilterInterceptor.intercept(MethodFilterInterceptor.java:98)
com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:248)
com.opensymphony.xwork2.interceptor.ParametersInterceptor.doIntercept(ParametersInterceptor.java:207)
com.opensymphony.xwork2.interceptor.MethodFilterInterceptor.intercept(MethodFilterInterceptor.java:98)
com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:248)
com.opensymphony.xwork2.interceptor.StaticParametersInterceptor.intercept(StaticParametersInterceptor.java:190)
com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:248)
org.apache.struts2.interceptor.MultiselectInterceptor.intercept(MultiselectInterceptor.java:75)
com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:248)
org.apache.struts2.interceptor.CheckboxInterceptor.intercept(CheckboxInterceptor.java:94)
com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:248)
org.apache.struts2.interceptor.FileUploadInterceptor.intercept(FileUploadInterceptor.java:243)
com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:248)
com.opensymphony.xwork2.interceptor.ModelDrivenInterceptor.intercept(ModelDrivenInterceptor.java:100)
com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:248)