2014-03-31 51 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

它工作正常?如果有帮助,请记住接受答案 –