相当郁闷啊。addFieldError是将错误信息存储到ValidationAwareSupport validationAware中定义的 Map<String, List<String>> errors中。struts2的actin是prototype型的。每次访问的action都是新new 的实例。但是为社么我每次访问lginAction,查看<s:debug/>。fieldErrors是累加的,即存储了前一次的错误信息!!!!所以第二次访问开始,一直都被workflow拦截器拦截到错误,进不了action!!!! 为什么呢????{loginError=[登录密码错误!, 用户名错误!, 用户名错误!, 登录密码错误!, 登录密码错误!, 登录密码错误!, 登录密码错误!, 登录密码错误!]}
package com.fendou.authority.web.action;import java.util.Map;import org.apache.struts2.interceptor.SessionAware;import com.fendou.authority.webservice.GeneralException_Exception;
import com.fendou.authority.webservice.ISecurityHelper;
import com.fendou.authority.webservice.UserVO;
import com.opensymphony.xwork2.ActionSupport;public class LoginAction extends ActionSupport implements SessionAware { /**
 * 
 */
private static final long serialVersionUID = 7787995295802692952L;
private String username;
private String password;
private UserVO visitor = null;
private ISecurityHelper securityHelper;
private Map<String,Object> session; 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;
} public void setSecurityHelper(ISecurityHelper securityHelper) {
this.securityHelper = securityHelper;
} public void setSession(Map<String, Object> session) {
this.session = session;
} public String execute() throws Exception{

if(visitor!=null){
 session.put("visitor", visitor);
}
return SUCCESS;
} @Override
public void validate() {
try {
visitor = securityHelper.queryUserByName(username, password);
} catch (GeneralException_Exception e) {
addFieldError("loginError",e.getMessage());
}
}


}

解决方案 »

  1.   

    我的登陆通过WebService调用另外项目的,debug可以进入另外项目中定义的.queryUserByName(username, password);。不过这不会有什么影响。主要就是ValueStack 中的fieldErrors会叠加。记录上一次的workflow拦截器拦截到错误不进入action
      

  2.   


    重写了validate方法;        @Override
    public void validate() {
    System.out.println("before..." + this.hasErrors());
    try {
    visitor = securityHelper.queryUserByName(username, password);
    } catch (GeneralException_Exception e) {
    addFieldError("loginError",e.getMessage());
    }
    System.out.println("after..." + this.hasErrors());
    }信息: Server startup in 58611 ms
    before...false
    [2010-04-03 10:32:16,573 ERROR] LogAspect - com.fendou.security.service.impl.SecurityHelper>>>登录密码错误!
    [2010-04-03 10:32:16,573 ERROR] LogAspect - com.fendou.platform.exception.GeneralException: 登录密码错误!
    2010-4-3 10:32:16 org.apache.cxf.phase.PhaseInterceptorChain doDefaultLogging
    信息: Application {http://impl.service.security.fendou.com/}SecurityHelperService#{http://api.service.security.fendou.com/}queryUserByName has thrown exception, unwinding now: com.fendou.platform.exception.GeneralException: 登录密码错误!
    after...true
    before...true
    [2010-04-03 10:32:24,557 ERROR] LogAspect - com.fendou.security.service.impl.SecurityHelper>>>登录密码错误!
    [2010-04-03 10:32:24,557 ERROR] LogAspect - com.fendou.platform.exception.GeneralException: 登录密码错误!
    2010-4-3 10:32:24 org.apache.cxf.phase.PhaseInterceptorChain doDefaultLogging
    信息: Application {http://impl.service.security.fendou.com/}SecurityHelperService#{http://api.service.security.fendou.com/}queryUserByName has thrown exception, unwinding now: com.fendou.platform.exception.GeneralException: 登录密码错误!
    after...true
    before...true
    [2010-04-03 10:32:41,571 ERROR] LogAspect - com.fendou.security.service.impl.SecurityHelper>>>用户名错误!
    [2010-04-03 10:32:41,571 ERROR] LogAspect - com.fendou.platform.exception.GeneralException: 用户名错误!
    2010-4-3 10:32:41 org.apache.cxf.phase.PhaseInterceptorChain doDefaultLogging
    信息: Application {http://impl.service.security.fendou.com/}SecurityHelperService#{http://api.service.security.fendou.com/}queryUserByName has thrown exception, unwinding now: com.fendou.platform.exception.GeneralException: 用户名错误!
    after...true
    后面就一直为true了
      

  3.   

    你没有设置spring中的bean为prototype,默认是singleton的。设置一下就OK了!记得结贴,给分啊!
      

  4.   

    哦,对啊。设置scope="prototype"就OK了。bean是有spring管理的,不是struts2!
     
       <bean id="loginAction" class="com.fendou.authority.web.action.LoginAction" scope="prototype">
       <property name="securityHelper" ref="securityHelperService" />
       </bean>