gpt4 book ai didi

java - 当我使用 radio 、选择等时,Struts2 验证失败

转载 作者:行者123 更新时间:2023-12-02 07:35:40 26 4
gpt4 key购买 nike

我是 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";
}
}

以下是我的类(class),当我提交表单时,它将调用。

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)

最佳答案

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

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

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

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

关于java - 当我使用 radio 、选择等时,Struts2 验证失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12275287/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com