`
lfq618
  • 浏览: 87779 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

利用@SkipValidation注释解决validation()方法对所有的方法过滤

阅读更多
今天上午做一博客,在UserAction中有两个方法 :login() 和 logout(), 还有一个登陆时的validation(),登陆可以运行,但登出时,不运行logout()方法,直接抛出异常:
No result defined for action com.lfq.action.UserAction and result input  

查了半天资料才发现,当访问一个action时,会先执行其的validation()方法,若检验失败,会返回ACTION的默认返回值INPUT, 所以当登出时,他这样是不会执行logout()的。
解决的方法:
(1)、在不需要进行验证的方法加上annotation,即在方法前加上@SkipValidation
(2)为Action中的指定方法指定一个验证规则,命名为Actionname-alias-validation.xml

下面贴上代码:
UserAction
package com.lfq.action;   
  
import javax.servlet.http.Cookie;   
import javax.servlet.http.HttpSession;   
  
import com.lfq.dao.UserDAO;   
import com.lfq.dao.impl.UserDAOImpl;   
import com.lfq.entity.User;   
  
public class UserAction extends BaseAction {   
  
    /**  
     *   
     */  
    private static final long serialVersionUID = 1L;   
    private User user;   
    private String msg;  //消息   
    private String userName;   
    private String password;   
    public String getUserName() {   
        return userName;   
    }   
    public void setUserName(String userName) {   
        this.userName = userName;   
    }   
    public String getPassword() {   
        return password;   
    }   
    public void setPassword(String password) {   
        this.password = password;   
    }   
    private boolean rememberMe;   
    public boolean isRememberMe() {   
        return rememberMe;   
    }   
    public void setRememberMe(boolean rememberMe) {   
        this.rememberMe = rememberMe;   
    }   
    public User getUser() {   
        return user;   
    }   
    public void setUser(User user) {   
        this.user = user;   
    }   
    public String getMsg() {   
        return msg;   
    }   
    public void setMsg(String msg) {   
        this.msg = msg;   
    }   
       
       
    public String login() {   
        String userName = getUserName();   
        String password = getPassword();   
        if("".equals(userName) || "".equals(password)) {   
            this.msg = "用户名或者密码不允许为空!";   
            return INPUT;   
        }          
        UserDAO userDao = new UserDAOImpl();   
        int userId = userDao.valid(userName, password);   
        if(userId > 0) {   
            if(rememberMe){   
                String t =userId +","+ userName + "," + password;   
                Cookie cookie = new Cookie("COOKIE_KEY_REMEMBER_LOGIN", t);   
                cookie.setMaxAge(86400);   
                super.getResponse().addCookie(cookie);   
            }   
            super.getSession().setAttribute("SESSION_KEY_USERNAME", userName);   
            super.getSession().setAttribute("SESSION_KEY_USERID", userId);   
            return SUCCESS;   
               
        } else {   
            this.addFieldError("error", "用户名或密码错误");   
            return INPUT;   
        }          
    }   
       
    public String logout() {   
        System.out.println("------------------------");   
        HttpSession session = getSession();   
        session.removeAttribute("SESSION_KEY_USERID");   
        session.removeAttribute("SESSION_KEY_USERNAME");   
        return SUCCESS;   
    }   
       
    @Override  
    public void validate() {   
        // TODO Auto-generated method stub   
        if(getUserName() == null || getUserName().trim().equals("")) {   
            addFieldError("userName", getText("user.required"));   
            return;   
        }   
        if(getPassword() == null || getPassword().trim().equals("")) {   
            addFieldError("password", getText("pass.required"));   
            return;   
        }   
    }   
}  


struts.xml
<?xml version="1.0" encoding="UTF-8"?>   
<!DOCTYPE struts PUBLIC   
    "-//Apache Software Foundation//DTD Struts Configuration 2.1.7//EN"  
    "http://struts.apache.org/dtds/struts-2.1.7.dtd">   
  
<struts>   
    <include file="struts-default.xml" />   
    <package name="admin" extends="struts-default" namespace="/admin">   
        <action name="login" class="com.lfq.action.UserAction" method="login">   
            <result name="input">/admin/login.jsp</result>   
            <result name="success">/admin/index.jsp</result>   
        </action>   
        <action name="logout" class="com.lfq.action.UserAction" method="logout">   
            <result name="*">/index.jsp</result>   
        </action>   
    </package>   
</struts>  


这样抛出的异常为:
2011-5-26 13:57:14 com.opensymphony.xwork2.util.logging.commons.CommonsLogger error
严重: Could not find action or result
No result defined for action com.lfq.action.UserAction and result input
	at com.opensymphony.xwork2.DefaultActionInvocation.executeResult(DefaultActionInvocation.java:364)
	at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:266)
	at com.opensymphony.xwork2.validator.ValidationInterceptor.doIntercept(ValidationInterceptor.java:252)
	at org.apache.struts2.interceptor.validation.AnnotationValidationInterceptor.doIntercept(AnnotationValidationInterceptor.java:68)
	at com.opensymphony.xwork2.interceptor.MethodFilterInterceptor.intercept(MethodFilterInterceptor.java:87)
	at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:237)
	at com.opensymphony.xwork2.interceptor.ConversionErrorInterceptor.intercept(ConversionErrorInterceptor.java:122)
	at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:237)
	at com.opensymphony.xwork2.interceptor.ParametersInterceptor.doIntercept(ParametersInterceptor.java:195)
	at com.opensymphony.xwork2.interceptor.MethodFilterInterceptor.intercept(MethodFilterInterceptor.java:87)
	at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:237)
	at com.opensymphony.xwork2.interceptor.ParametersInterceptor.doIntercept(ParametersInterceptor.java:195)
	at com.opensymphony.xwork2.interceptor.MethodFilterInterceptor.intercept(MethodFilterInterceptor.java:87)
	at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:237)
	at com.opensymphony.xwork2.interceptor.StaticParametersInterceptor.intercept(StaticParametersInterceptor.java:179)
	at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:237)
	at org.apache.struts2.interceptor.MultiselectInterceptor.intercept(MultiselectInterceptor.java:75)
	at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:237)
	at org.apache.struts2.interceptor.CheckboxInterceptor.intercept(CheckboxInterceptor.java:94)
	at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:237)
	at org.apache.struts2.interceptor.FileUploadInterceptor.intercept(FileUploadInterceptor.java:235)
	at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:237)
	at com.opensymphony.xwork2.interceptor.ModelDrivenInterceptor.intercept(ModelDrivenInterceptor.java:89)
	at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:237)
	at com.opensymphony.xwork2.interceptor.ScopedModelDrivenInterceptor.intercept(ScopedModelDrivenInterceptor.java:130)
	at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:237)
	at org.apache.struts2.interceptor.debugging.DebuggingInterceptor.intercept(DebuggingInterceptor.java:267)
	at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:237)
	at com.opensymphony.xwork2.interceptor.ChainingInterceptor.intercept(ChainingInterceptor.java:126)
	at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:237)
	at com.opensymphony.xwork2.interceptor.PrepareInterceptor.doIntercept(PrepareInterceptor.java:138)
	at com.opensymphony.xwork2.interceptor.MethodFilterInterceptor.intercept(MethodFilterInterceptor.java:87)
	at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:237)
	at com.opensymphony.xwork2.interceptor.I18nInterceptor.intercept(I18nInterceptor.java:165)
	at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:237)
	at org.apache.struts2.interceptor.ServletConfigInterceptor.intercept(ServletConfigInterceptor.java:164)
	at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:237)
	at com.opensymphony.xwork2.interceptor.AliasInterceptor.intercept(AliasInterceptor.java:179)
	at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:237)
	at com.opensymphony.xwork2.interceptor.ExceptionMappingInterceptor.intercept(ExceptionMappingInterceptor.java:176)
	at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:237)
	at org.apache.struts2.impl.StrutsActionProxy.execute(StrutsActionProxy.java:52)
	at org.apache.struts2.dispatcher.Dispatcher.serviceAction(Dispatcher.java:488)
	at org.apache.struts2.dispatcher.FilterDispatcher.doFilter(FilterDispatcher.java:395)
	at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:235)
	at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
	at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:230)
	at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:175)
	at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:128)
	at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:104)
	at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)
	at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:261)
	at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:844)
	at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:581)
	at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:447)
	at java.lang.Thread.run(Thread.java:619)

解决:
@SkipValidation
public String logout() {
	System.out.println("------------------------");
	HttpSession session = getSession();
	session.removeAttribute("SESSION_KEY_USERID");
	session.removeAttribute("SESSION_KEY_USERNAME");
	return SUCCESS;
}
分享到:
评论

相关推荐

    Spring Validation方法实现原理分析

    Spring Validation方法实现原理分析 Spring Validation是Spring框架中的一种校验机制,用于验证JavaBean的属性是否符合JSR-303规范。该机制可以在应用程序中自动验证JavaBean的属性,从而确保数据的正确性和完整性...

    jquery-validation-1.17.0

    对于jQuery Validation,这个文件会指导开发者如何引入和使用验证规则,以及如何解决可能出现的问题,是快速上手的关键。 `test`目录包含的是单元测试文件,这些测试确保了插件在每次更新后仍能正常工作。开发者...

    jakarta.validation-api-2.0.2-API文档-中文版.zip

    赠送jar包:jakarta.validation-api-2.0.2.jar; 赠送原API文档:jakarta.validation-api-2.0.2-javadoc.jar; 赠送源代码:jakarta.validation-api-2.0.2-sources.jar; 赠送Maven依赖信息文件:jakarta.validation...

    validation-api-2.0.1.Final-API文档-中文版.zip

    赠送jar包:validation-api-2.0.1.Final.jar; 赠送原API文档:validation-api-2.0.1.Final-javadoc.jar; 赠送源代码:validation-api-2.0.1.Final-sources.jar; 赠送Maven依赖信息文件:validation-api-2.0.1....

    jakarta.validation-api-2.0.1-API文档-中文版.zip

    赠送jar包:jakarta.validation-api-2.0.1.jar; 赠送原API文档:jakarta.validation-api-2.0.1-javadoc.jar; 赠送源代码:jakarta.validation-api-2.0.1-sources.jar; 赠送Maven依赖信息文件:jakarta.validation...

    validation-api-2.0.1.Final-API文档-中英对照版.zip

    赠送jar包:validation-api-2.0.1.Final.jar; 赠送原API文档:validation-api-2.0.1.Final-javadoc.jar; 赠送源代码:validation-api-2.0.1.Final-sources.jar; 赠送Maven依赖信息文件:validation-api-2.0.1....

    validation-api-1.1.0.Final-API文档-中文版.zip

    赠送jar包:validation-api-1.1.0.Final.jar; 赠送原API文档:validation-api-1.1.0.Final-javadoc.jar; 赠送源代码:validation-api-1.1.0.Final-sources.jar; 包含翻译后的API文档:validation-api-1.1.0....

    springmvc校验器validation的所有配套jar包

    通过`Validation.buildDefaultValidatorFactory()`可以获取一个Validator实例,然后使用`validate()`方法对对象进行验证。 4. **集成Spring MVC**:在Spring MVC中,可以通过配置`LocalValidatorFactoryBean`作为...

    validation jar包

    Spring Validator则集成了Hibernate Validator,使得我们可以利用Spring的依赖注入和AOP特性轻松地在Spring应用中使用Bean Validation。在Spring配置中启用Validator后,我们可以在Service层或者Controller层进行...

    Bean Validation技术实现对Javabean的校验

    本篇文章将深入探讨Bean Validation技术,以及如何利用它来对Javabean进行校验。 一、Bean Validation简介 Bean Validation是Java EE的一部分,也是JSR 303和JSR 349规范的实现,后来在Java 8中升级为JSR 380。这个...

    jakarta.validation-api-2.0.1-API文档-中英对照版.zip

    赠送jar包:jakarta.validation-api-2.0.1.jar; 赠送原API文档:jakarta.validation-api-2.0.1-javadoc.jar; 赠送源代码:jakarta.validation-api-2.0.1-sources.jar; 赠送Maven依赖信息文件:jakarta.validation...

    Hibernate Bean Validation jar包

    Bean Validation 规范对 Java Bean 的验证流程如下:在实际使用中调用 Validator.validate(JavaBeanInstance) 方法后,Bean Validation 会查找在 JavaBeanInstance上所有的约束声明,对每一个约束调用对应的约束验证...

    使用FormValidation进行表单验证

    首先,`FormValidation`库提供了一种灵活的方法来验证表单字段,包括但不限于必填字段检查、电子邮件格式验证、手机号码验证等。它的API允许开发者自定义验证规则,创建复杂的验证逻辑。例如,你可以设置一个规则,...

    javax.validation-1.0.0.GA.jar

    在描述中提到的"结合hibernate-validator对实体做约束验证配置",意味着我们可以利用Hibernate Validator在实体类上添加注解,如@NotNull、@Size、@Min等,来定义数据的合法性规则。 举个例子,假设我们有一个用户...

    FormValidation完整源码和演示

    FormValidation是一款功能强大的前端表单验证框架,专为开发者提供高效、灵活的验证解决方案。它以其高度可定制性,对HTML data属性的完美支持以及内置的自定义验证器功能而备受推崇。本资源包含了FormValidation的...

    FormValidation.js

    **FormValidation.js 知识点详解** `FormValidation.js` 是一个强大且灵活的JavaScript库,专门用于前端表单验证。它提供了丰富的校验规则、样式定制以及对动态添加元素的验证支持,使得开发者能够轻松地创建高效、...

    Struts2中Validation数据验证框架教程

    这些方法可以对用户输入的数据进行检查,如非空验证、长度限制、格式校验等。如果验证失败,框架会将错误信息添加到ActionContext中,然后这些错误信息可以在JSP页面上展示给用户。 在Struts2 XML配置文件中,`...

    easyexcel+validation excel导入导出下载

    EasyExcel的优势在于其简洁的API设计和对内存的高效利用。它无需预先加载整个Excel文件到内存,而是采用流式读写,降低了对系统资源的占用,特别适合处理大数据量的Excel文件。使用EasyExcel进行Excel导入导出,主要...

    Bean Validation 规范文档

    Bean Validation规范要求Java版本为至少JDK 1.5以上,以利用其增强的语言特性如注解等。 #### 1.4 文档组织 文档按照章节结构组织,涵盖了从约束定义到验证过程的全面内容,便于读者系统性学习。 #### 1.5 变更...

Global site tag (gtag.js) - Google Analytics