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

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

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

下面贴上代码:
[b]UserAction[/b]

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;
}
}
}


[b]struts.xml[/b]


"-//Apache Software Foundation//DTD Struts Configuration 2.1.7//EN"
"http://struts.apache.org/dtds/struts-2.1.7.dtd">





/admin/login.jsp
/admin/index.jsp


/index.jsp





这样抛出的异常为:

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)

[b]解决:[/b]

@SkipValidation
public String logout() {
System.out.println("------------------------");
HttpSession session = getSession();
session.removeAttribute("SESSION_KEY_USERID");
session.removeAttribute("SESSION_KEY_USERNAME");
return SUCCESS;
}

你可能感兴趣的:(java_struts2.0)