我是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请求时,它正在初始化“Initialization”类并初始化radiobutton的内容。
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)
答案 0 :(得分:2)
在我看来,这是struts2验证中第一个也是最常见的问题。
当验证失败时,struts2会重定向到您的“输入”结果(在“struts.xml”中定义),并且它不会执行您的任何操作方法。如果输入是JSP,它将尝试呈现该JSP ... 你没有包含yor JSP,但它可能包含radio,select或其他标签,它们希望从你的动作中收集一个集合来初始化它们,但是当验证失败时它们不会得到任何东西,所以这就是错误。
解决方案:您需要在操作中使用一些方法来创建jsp中始终需要的所需集合或其他参数。您可以在您的动作构造函数中执行此操作,即使验证失败,也会调用它,但这是一个肮脏的解决方案。好的解决方案是使用Preparable拦截器(包含在Struts2拦截器的DefaultStack中)。 然后,即使验证失败,您也将拥有一个始终被调用的prepare()方法。
http://struts.apache.org/release/2.0.x/docs/prepare-interceptor.html