日期:2014-05-19  浏览次数:20613 次

struts2表单数据验证,没有起作用。帮我看看是哪里问题
Action:
Java code

package action;

import com.opensymphony.xwork2.ActionSupport;

import entity.User;

public class RegistAction extends ActionSupport{

private User user;
public String regist(){
    System.out.println(user.getUsername());
    return "success";
}
public String form(){
    return "success";
}
public User getUser() {
    return user;
}
public void setUser(User user) {
    this.user = user;
}

}




struts-xml:
XML code

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
    "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
    "http://struts.apache.org/dtds/struts-2.0.dtd">
 <struts>
    <package name="cb" extends="struts-default" >
          <action name="regist" class="action.RegistAction" method="regist">
             <result name="success" type="dispatcher">
                /jsp/success.jsp
              </result>
           </action>
            <action name="form" class="action.RegistAction" method="form">
             <result name="success" type="dispatcher">
               /jsp/registform.jsp
             </result>
           </action>
    </package>
</struts>    



RegistAction-validation.xml
XML code

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
    "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
    "http://struts.apache.org/dtds/struts-2.0.dtd">
 <struts>
    <package name="cb" extends="struts-default" >
          <action name="regist" class="action.RegistAction" method="regist">
             <result name="success" type="dispatcher">
                /jsp/success.jsp
              </result>
           </action>
            <action name="form" class="action.RegistAction" method="form">
             <result name="success" type="dispatcher">
               /jsp/registform.jsp
             </result>
           </action>
    </package>
</struts>    




JSP

HTML code

<%@page contentType="text/html;charset=utf-8"%>
<%@taglib uri="/struts-tags" prefix="s"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
        <body>
          <s:fielderror cssClass="color:red"/>
          <s:form method="post" action="regist" name="taskForm"  >
            <s:textfield label="用户名" name="user.username" cssErrorStyle="background-color:#ff9999" />
            <s:password label="密码" name="user.password"  cssErrorStyle="background-color:#ff9999"/>    
            <s:textfield label="年龄" name="user.age" cssErrorStyle="background-color:#ff9999" />
            <s:textf