私は struts2 検証フレームワークを使用しています。TestAction クラスの test() メソッドを検証したいだけです。コードと構成は次のとおりです。
@Namespace(value = "/")
@Result(name="input", location="test_input.jsp")
public class TestAction
{
private String name;
public String getName()
{
return name;
}
public void setName(String name)
{
this.name = name;
}
@Action(value = "test", results = { @Result(name = "success", location = "test.jsp")})
public String test() throws Exception
{
return "success";
}
@Action(value = "show", results = { @Result(name = "success", location = "test_input.jsp")})
public String show() throws Exception
{
return "success";
}
}
test_input.jsp:
<body>
<s:fielderror></s:fielderror>
<s:form action="test.do" theme="xhtml">
<s:textfield name="name" label="name"></s:textfield>
<s:submit value="submit"></s:submit>
</s:form>
</body>
test.jsp:
<body>
success
<s:debug></s:debug>
</body>
TestAction-test-validation.xml:
<validators>
<field name="name">
<field-validator type="requiredstring">
<message>name is required.</message>
</field-validator>
</field>
</validators>
TestAction.class と TestAction-test-validation.xml は同じパッケージにあります。
struts.xml:
<struts>
<constant name="struts.enable.DynamicMethodInvocation" value="true" />
<constant name="struts.devMode" value="true" />
<constant name="struts.ui.theme" value="simple" />
<constant name="struts.ognl.allowStaticMethodAccess" value="true" />
<constant name="struts.action.extension" value="do" />
<constant name="struts.objectFactory" value="spring" />
<constant name="struts.convention.result.path" value="/" />
<include file="struts-default.xml"></include>
<package name="default" namespace="/" extends="struts-default">
<default-interceptor-ref name="defaultStack"></default-interceptor-ref>
<global-results>
<result name="error">error.jsp</result>
</global-results>
<global-exception-mappings>
<exception-mapping exception="java.lang.Exception"
result="error" />
</global-exception-mappings>
</package>
</struts>
web.xml:
<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>*.do</url-pattern>
<dispatcher>REQUEST</dispatcher>
<dispatcher>FORWARD</dispatcher>
<dispatcher>INCLUDE</dispatcher>
</filter-mapping>
ブラウザーで show.do にアクセスし始めると、入力とボタンが表示されます。入力には何も入力せず、ボタンを直接クリックするだけで、ページが test.do に移動し、ページに「成功」が表示されます。 「デバッグ」コンテンツでは、エラーメッセージはありません。[エラー] name:name の検証エラーが必要です。上記のメッセージは Eclipse コンソールに出力されます (Eclipse で tomcat を開始します) エラー メッセージは、TestAction-test-validation.xml を struts2 で実行する必要があることを意味しますが、なぜ test_input.jsp にリダイレクトしないのでしょうか? すでに入力結果を定義しています。
構成に何か問題がありますか、それとも他の構成が不足していますか?
何かご意見は?