Reputation: 3976
I am using struts2 validation framework, I just want to validate test() method in TestAction class, here are my code and configuration:
@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 and TestAction-test-validation.xml are in the same package.
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>
I begin to visit show.do by browser, an input and button appears, I don't type anything in input, just directly click the button, the pages goes to test.do and show 'success' in the page, and I look at the 'debug' content, no any error message in it. but the strange thing is that: [ERROR] Validation error for name:name is required. the above message is printed in eclipse console (I start tomcat in eclipse) the error message means TestAction-test-validation.xml must be executed by struts2, but why it doesn't redirect to test_input.jsp? I already define an input result.
Is there something wrong in my configuration or I lack other configuration ?
Any thoughts?
Upvotes: 1
Views: 2210
Reputation: 50203
Try extending ActionSupport
from your Action.
public class TestAction extends ActionSupport {
It's always a good idea to include it, usually there are no reasons to not extends it in every Action of your project.
P.S: this way you can return SUCCESS
instead of "success"
, etc
Upvotes: 1