0

我是 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)
4

1 回答 1

2

在我看来,这是 struts2 验证的第一个也是最常见的问题。

当验证失败时,struts2 重定向到您的“输入”结果(在您的“struts.xml”中定义)并且它不会执行您的任何操作方法。如果输入是 JSP,它将尝试呈现该 JSP...您没有包含您的 JSP,但它可能包含无线电、选择或其他希望从您的操作中接收集合以初始化它们的标签,但是当验证失败时,它们不会得到任何东西,所以这就是错误。

解决方案:您需要在您的操作中创建一些方法来创建您在 jsp 中始终需要的所需集合或其他参数。您可以在您的操作构造函数中执行此操作,即使验证失败,也会调用该构造函数,但这是一个肮脏的解决方案。好的解决方案是使用 Preparable 拦截器(包含在 Struts2 拦截器的 DefaultStack 中)。然后,您将拥有一个 prepare() 方法,即使验证失败,该方法也将始终被调用。

http://struts.apache.org/release/2.0.x/docs/prepare-interceptor.html

于 2013-04-10T11:20:20.497 回答