struts2 validate 不执行 ,为什么?
郁闷,写了个简单的测试用例,结果这个校验始终不执行 ,网上没有需找到答案,请大侠赐教
前台:login.jsp
<s:fielderror></s:fielderror>
<s:form action="Login" validate="true" method="post">
<s:textfield label="用户名" name="username" />
<s:textfield label="密码" name="password" />
<s:submit value="登录" />
</s:form>
action:LoginAction.java
package com.study.action;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
public class LoginAction extends ActionSupport{
private static final long serialVersionUID = 1L;
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;
}
public String execute() throws Exception {
System.out.println("execute() runing......");
if("hyq".equals(getUsername())&&"123".equals(getPassword())){
ActionContext.getContext().getSession().put("user", getUsername());
return "success";
}else{
return "error";
}
}
//增加校验但没有执行,为什么呢?
public void validateExecute() {
System.out.println("validate() runing......");
if(getUsername()==null||"".equals(getUsername().trim())){
add
FieldError("username","username is must!");
}
if(getPassword()==null||"".equals(getPassword().trim())){
addFieldError("password","password is must!");
}
}
}
struts.xml
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
"http://struts.apache.org/dtds/struts-2.0.dtd">
<struts>
<!-- Include webwork default (from the Struts JAR). -->
<include file="struts-default.xml"/>
<!-- Configuration for the default package. -->
<package name="strutsqs" extends="struts-default">
<!-- Default interceptor stack. -->
<!-- <default-interceptor-ref name="paramsPrepareParamsStack"/>-->
<action name="Login" class="com.study.action.LoginAction">
<result name="input">/jsp/login.jsp</result>
<result name="success">/jsp/welcome.jsp</result>
<result name="error">/jsp/errors.jsp</result>
<!-- we don't need the full stack here -->
<interceptor-ref name="basicStack"/>
</action>
<action name="GetBooks" class="com.study.action.GetBooksAction" >
<result name="login">/jsp/login.jsp</result>
<result name="success">/jsp/showBook.jsp</result>
</action>
</package>
</struts>
------解决方案--------------------
<interceptor-ref name="basicStack"/>
楼主仔细看清楚,基础拦截器里面有没有使用到ValidationInteceptor。
把这个拦截器去掉,在把方法名换成validate就可以了。