2014-03-31 92 views
2

我使用netbeans 8和struts 2 xml字段驗證(tomcat7)來創建一個web應用程序。形成。當我點擊提交按鈕而沒有填寫表格時,成功頁面顯示爲空白,沒有任何錯誤。我是否錯過了將LoginAction-validation.xml連接到表單的步驟?也許我需要更新的插件?我在表單驗證我的庫文件夾下面的:Struts2表單驗證不起作用

Struts2的核心2.3.15 - XWork的核心 - 2.3.15.3.jar

Struts2的核心2.3.15 - struts2的核心 - 23.15.3.jar

LoginAction.java

package login; 

import com.opensymphony.xwork2.ActionSupport; 

public class LoginAction extends ActionSupport { 

    public String getNotes() { 
     return notes; 
    } 

    public void setNotes(String notes) { 
     this.notes = notes; 
    } 

    public String getOperatingSystem() { 
     return operatingSystem; 
    } 

    public void setOperationSystem(String operatingSystem) { 
     this.operatingSystem = operatingSystem; 
    } 

    public String getVersion() { 
     return version; 
    } 

    public void setVersion(String pass) { 
     this.version = version; 
    } 
    String operatingSystem; 
    String version; 
    String notes; 

    @Override 
    public String execute(){ 
      return "SUCCESS"; 
    } 
} 

的index.jsp

<!DOCTYPE html> 
<%@page contentType="text/html" pageEncoding="UTF-8"%> 
<%@taglib uri="/struts-tags" prefix="s" %> 
    <head> 
     <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> 
     <title>Jive</title> 
    </head> 
    <body> 
     <h1>Code Sample</h1>  
     <s:form action="LoginAction" method="post"> 
       <s:textfield label="Operating System" name="operatingSystem"/> 
       <s:textfield label="Version" name="version"/> 
       <s:textfield label="Notes" name="notes"/> 
       <s:submit cssClass="btn btn-primary"/> 
     </s:form> 
    </body> 
</html> 

的web.xml

<?xml version="1.0" encoding="UTF-8"?> 
<web-app version="3.0" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"> 
    <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> 
    <session-config> 
     <session-timeout> 
      30 
     </session-timeout> 
    </session-config> 
    <welcome-file-list> 
     <welcome-file>index.jsp</welcome-file> 
    </welcome-file-list> 
</web-app> 

struts.xml中

<!DOCTYPE struts PUBLIC 
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN" 
"http://struts.apache.org/dtds/struts-2.0.dtd"> 

<struts> 
    <!-- Configuration for the default package. --> 
    <package name="default" extends="struts-default"> 
     <action name="LoginAction" class="login.LoginAction" method="execute"> 
      <result name="SUCCESS">/success.jsp</result> 
      <result name="input">/index.jsp</result> 
     </action> 
    </package> 
</struts> 

的LoginAction-validation.xml中

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

<validators> 
<field name="operatingSystem"> 
    <field-validator type="requiredstring"> 
     <message key="errors.required">Operation System is required.</message> 
    </field-validator> 
</field> 
<field name="version"> 
    <field-validator type="requiredstring"> 
     <message key="errors.required" /> 
    </field-validator> 
</field> 
</validators> 

的success.jsp

<!DOCTYPE html> 
<%@page contentType="text/html" pageEncoding="UTF-8"%> 
<%@taglib uri="/struts-tags" prefix="s" %> 

<html> 
    <head> 
     <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> 
     <title>JSP Page</title> 
    </head> 
    <body> 
     <h1> Success </h1> 
    </body> 
</html> 

error.jsp文件

<!DOCTYPE html> 
<%@page contentType="text/html" pageEncoding="UTF-8"%> 

<html> 
    <head> 
     <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> 
     <title>JSP Page</title> 
    </head> 
    <body> 
     <h1>Error!</h1> 
    </body> 
</html> 
+0

你有代碼中有很多拼寫錯誤/錯誤。修復它們並查看它是否有效。 –

回答

0

正如你可以在the Apache DTD directory中看到的那樣,沒有像xwork-validator-2.3dtd這樣的東西(在擴展中也有一個錯字)。最新XWork的驗證器是1.0.3,它是一個你應該使用:

<!DOCTYPE validators PUBLIC "-//Apache Struts//XWork Validator 1.0.3//EN" 
      "http://struts.apache.org/dtds/xwork-validator-1.0.3.dtd"> 

也提醒始終使用的庫相同的版本,例如2.3.15.3(或更好2.3.16.1)到處

+0

順便說一句,不錯的第一個問題 –

+0

它工作正常?如果有幫助,請記住接受答案 –