我正在使用 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>
测试.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>
网页.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并在页面中显示'success',我看在“调试”内容中,其中没有任何错误消息。但奇怪的是:[ERROR] name:name 的验证错误是必需的。上面的信息打印在eclipse控制台(我在eclipse中启动tomcat)错误信息意思是TestAction-test-validation.xml必须由struts2执行,但是为什么它没有重定向到test_input.jsp?我已经定义了一个输入结果。
我的配置有问题还是缺少其他配置?
有什么想法吗?