2

struts 2 xml フィールド検証 (tomcat7) で netbeans 8 を使用して Web アプリを作成しています。形。フォームに入力せずに送信ボタンをクリックすると、成功ページが表示され、フォームを空白のままにしてもエラーは発生しません。LoginAction-validation.xmlをフォームに接続する手順がありませんか? 多分私は更新されたプラグインが必要ですか?フォームの検証のために、Libraries フォルダーに次のものがあります。

Struts2 コア 2.3.15 - xwork-core-2.3.15.3.jar

Struts2 コア 2.3.15 - struts2-core-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>

成功.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>

エラー.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>
4

1 に答える 1

0

Apache DTD directoryでわかるように、そのようなものはありませんxwork-validator-2.3dtd(拡張子にもタイプミスがあります)。最新の xwork-validator は 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

于 2014-04-01T07:59:34.760 に答える