使用注释在struts2中进行验证

时间:2013-12-23 20:22:54

标签: jsp validation struts2

在Struts2(2.3.16)中实现验证时,一旦加载页面,我就会收到以下异常。

Dec 24, 2013 1:43:52 AM org.apache.struts2.dispatcher.Dispatcher error
SEVERE: Could not find action or result
/TestStruts/admin_side/test.action
No result defined for action actions.TestAction and result input
    at com.opensymphony.xwork2.DefaultActionInvocation.executeResult(DefaultActionInvocation.java:373)
    at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:275)
    at com.opensymphony.xwork2.validator.ValidationInterceptor.doIntercept(ValidationInterceptor.java:265)
    at org.apache.struts2.interceptor.validation.AnnotationValidationInterceptor.doIntercept(AnnotationValidationInterceptor.java:68)
    at com.opensymphony.xwork2.interceptor.MethodFilterInterceptor.intercept(MethodFilterInterceptor.java:98)
    at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:246)
    at org.apache.struts2.interceptor.DeprecationInterceptor.intercept(DeprecationInterceptor.java:41)
    at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:246)
    at com.opensymphony.xwork2.interceptor.ConversionErrorInterceptor.intercept(ConversionErrorInterceptor.java:138)
    at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:246)
    at com.opensymphony.xwork2.interceptor.ParametersInterceptor.doIntercept(ParametersInterceptor.java:249)
    at com.opensymphony.xwork2.interceptor.MethodFilterInterceptor.intercept(MethodFilterInterceptor.java:98)
    at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:246)
    at com.opensymphony.xwork2.interceptor.ParametersInterceptor.doIntercept(ParametersInterceptor.java:249)
    at com.opensymphony.xwork2.interceptor.MethodFilterInterceptor.intercept(MethodFilterInterceptor.java:98)
    at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:246)
    at org.apache.struts2.interceptor.MultiselectInterceptor.intercept(MultiselectInterceptor.java:73)
    at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:246)
    at org.apache.struts2.interceptor.CheckboxInterceptor.intercept(CheckboxInterceptor.java:91)
    at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:246)
    at com.opensymphony.xwork2.interceptor.PrepareInterceptor.doIntercept(PrepareInterceptor.java:171)
    at com.opensymphony.xwork2.interceptor.MethodFilterInterceptor.intercept(MethodFilterInterceptor.java:98)
    at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:246)
    at org.apache.struts2.interceptor.ServletConfigInterceptor.intercept(ServletConfigInterceptor.java:164)
    at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:246)
    at com.opensymphony.xwork2.interceptor.ExceptionMappingInterceptor.intercept(ExceptionMappingInterceptor.java:189)
    at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:246)
    at org.apache.struts2.impl.StrutsActionProxy.execute(StrutsActionProxy.java:54)
    at org.apache.struts2.dispatcher.Dispatcher.serviceAction(Dispatcher.java:562)
    at org.apache.struts2.dispatcher.ng.ExecuteOperations.executeAction(ExecuteOperations.java:77)
    at org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter.doFilter(StrutsPrepareAndExecuteFilter.java:99)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:243)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:222)
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:123)
    at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:472)
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:171)
    at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:99)
    at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:936)
    at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:118)
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:407)
    at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1004)
    at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:589)
    at org.apache.tomcat.util.net.AprEndpoint$SocketProcessor.run(AprEndpoint.java:1822)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1110)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:603)
    at java.lang.Thread.run(Thread.java:722)

动作类:

@Namespace("/admin_side")
@ResultPath("/WEB-INF/content")
public final class TestAction extends ActionSupport implements Serializable, ValidationAware
{
    @Autowired
    private final transient TransporterService transporterService=null;
    private static final long serialVersionUID = 1L;
    private static final String SUCCESS = "success";

    private String name;
    private String email;

    @Action(value = "test", results = {
    @Result(name="success", location="Test.jsp"),
    @Result(name = "error", location = "Test.jsp"),
    }, interceptorRefs = @InterceptorRef("validationWorkflowStack"))
    public String test() throws Exception
    {
        return SUCCESS;
    }

    public String getName() {
        return name;
    }

    @RequiredFieldValidator(message = "The name is required")
    public void setName(String name) {
        this.name = name;
    }

    public String getEmail() {
        return email;
    }

    @RequiredStringValidator(type= ValidatorType.FIELD, message = "The email is required.")
    public void setEmail(String email) {
        this.email = email;
    }
}

Test.jsp页面

<s:actionerror />
<s:fielderror />

<s:form namespace="/admin_side" action="test" validate="true">

    <s:textfield id="name" name="name" label="Enter your name"/>
    <s:textfield id="email" name="email" label="Enter your email"/>
    <s:submit id="btnSubmit" name="btnSubmit" value="Submit"/>

</s:form>

为什么不起作用?

1 个答案:

答案 0 :(得分:1)

根据OP的要求添加了答案,关于评论中的解决方案。

检查完所有字段后,如果其中任何字段无法验证,则返回字符串“INPUT”,并且永远不会运行execute()方法。

您的注释帐户是否为SUCCESS和ERROR(虽然您没有生成ERROR字符串......应该用INPUT替换ERROR,您的问题就会消失)。

另外struts2标签应该自动显示错误,除非你使用简单主题(我使用的),在这种情况下,s2字段错误标记通常应该使用name属性,这样你就可以在UI旁边设置一个特定的字段错误元素(当然不是必需的,但很好)。