2015-10-20 50 views
0

的index.jsp使用XML文件struts的驗證工作不

<%@ 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的驗證使用上述代碼。驗證不會被觸發,即使驗證失敗,成功頁面也會顯示。請仔細閱讀代碼並向我推薦這些更改。

+0

你把你的Employee-validation.xml文件放在哪裏? –

+0

在我的Employee.java所在的文件夾中 – user3678383

+1

哪個S2版本?你應該使用新的過濾器,[FilterDispatcher is deprecated](http://stackoverflow.com/a/17103563/1654265)。 –

回答

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"> 
    

正如你可以在Apache目錄(http://struts.apache.org/dtds/)看到有XWork的驗證器1.01.0.21.0.3xwork-validator-defi根據the documentation,nition僅爲1.0,不是正確的。

+0

不,它沒有工作 – user3678383

+1

非常奇怪。您能否通過根據所作修改更新代碼來修改您的問題? DTD,新的過濾器等......即使縮小了名稱並更改了名稱,代碼也必須與真實代碼相同,否則您可以避免發佈關鍵問題,而不會注意。還要注意構造函數(必須總是有一個沒有參數的,隱式的,或者當指定了另一個時,顯式的),getters/setters用於一個字母小寫的變量和另一個大寫,例如Eclipse創建的「fBar」以錯誤的方式創建 –

+0

會做到這一點,讓你知道 – user3678383