使用xml文件验证struts不起作用

时间:2015-10-20 09:32:33

标签: java xml validation jsp struts2

的index.jsp

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<%@ taglib prefix="s" uri="/struts-tags" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"  
"http://www.w3.org/TR/html4/loose.dtd">
<html>
    <head>
         <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
         <title>Add new product</title>
    </head>
    <body>
        <s:form action="emp1" method="post">
            <s:textfield label="Name" name="name" ></s:textfield>
            <s:textfield label="Age" name="age" ></s:textfield>     
            <s:submit value="Save" align="left"></s:submit>     
        </s:form>
    </body>
</html>

的web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
xmlns="http://java.sun.com/xml/ns/javaee" 
xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" 
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee 
http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" 
version="3.0">

    <display-name>DemoValidation</display-name>

    <welcome-file-list>
        <welcome-file>index.jsp</welcome-file>
    </welcome-file-list>

    <filter>
        <filter-name>struts2</filter-name>
        <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
    </filter>

    <filter-mapping>
        <filter-name>struts2</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>

</web-app>

struts.xml中

<?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>    
    <constant name="struts.devMode" value="false" />
    <package name="default" extends="struts-default">
        <action name="emp1" class="controller.Employee">
            <result name="success">/success.jsp</result>
            <result name="input">/index.jsp</result>            
        </action>
    </package>
</struts>

Employee.java

package controller;

import com.opensymphony.xwork2.ActionSupport;

public class Employee extends ActionSupport{

    private String ename;
    private int age;

    public String execute(){
        return SUCCESS;
    }

    public String getEname() {
        return ename;
    }
    public void setEname(String ename) {
        this.ename = ename;
    }
    public int getAge() {
        return age;
    }
    public void setAge(int age) {
        this.age = age;
    }
}

员工-validation.xml中

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE validators PUBLIC 
"-//Apache Struts//XWork Validator 1.0.3//EN"
"http://struts.apache.org/dtds/xwork-validator-1.0.3.dtd"

<validators>

    <field name="ename">
        <field-validator type="requiredstring">
            <param name="trim">true</param>
            <message>Name is required</message>
        </field-validator>
    </field>

    <field name="age">          
        <field-validator type="int">
            <param name="trim">true</param>
            <param name="min">21</param>
            <param name="max">40</param>
            <message>Age should be between 21 to 40</message>
        </field-validator>      
    </field>

 </validators>

我使用上面的代码来演示struts2的验证。即使验证失败,也不会触发验证,显示成功页面。请仔细阅读代码并向我推荐更改。

1 个答案:

答案 0 :(得分:2)

  1. 应用新过滤器,因为FilterDispatcher is deprecated;

  2. 更改错误的定义
    <!DOCTYPE validators PUBLIC
        "-//Apache Struts//XWork Validator Definition 1.0.3//EN"
        "http://struts.apache.org/dtds/xwork-validator-definition-1.0.3.dtd">
    

    <!DOCTYPE validators PUBLIC 
        "-//Apache Struts//XWork Validator 1.0.3//EN"
        "http://struts.apache.org/dtds/xwork-validator-1.0.3.dtd">
    
  3. 正如你在Apache目录(http://struts.apache.org/dtds/)中看到的那样, xwork-validator 1.01.0.21.0.3但是 xwork-validator-definition 仅为1.0,而且不是正确的。