user5192032
user5192032

Reputation:

Multiple input (result name) in Struts2

I am working on an application that is using Struts2 framework. In action class I got two validatemethods, one for each action. In struts.xml I have input to validate a method and returns a corresponding a view but for the other action that needs validation too, how this approach would work? The only thing I need to know is whether I can change the default input to something else so that method2 gets validated if not then how can I go to different view after actions are validated.

Action Class:

public void validateSearch() {
    // validation
}

public void validateSubmit() {
    // validation
}

// Action Methods

public String search() {
    // business logic
}

public String submit() {
    // business logic
}

struts.xml

<result name="input">search.jsp</result>
<result name="????">submit.jsp</result>

In case of two input I don't get my views the way I want them. For submit I get a view of search. is there any way to configure this.

Upvotes: 0

Views: 1140

Answers (2)

Roman C
Roman C

Reputation: 1

ok, since you are still looking fro answers I will tell you that input name for result is conventional. You can change it any time if your action class implements ValidationWorkflowAware.

ValidationWorkflowAware classes can programmatically change result name when errors occurred This interface can be only applied to action which already implements ValidationAware interface!

public void validateSubmit() {
    // validation
    if (hasErrors())
        setInputResultName("inputSubmit");
}

The result config

<result name="inputSubmit">submit.jsp</result>    

Upvotes: 0

Andrea Ligios
Andrea Ligios

Reputation: 50203

You are probably using DMI (which is deprecated and highly discouraged), and have something like this:

<action name="foo" class="foo.bar.fooAction">
    <result name="success">foo.jsp</result>
    <result name="input">search.jsp</result>
</action>

You simply need to turn your two action methods into two real actions, like follows:

<action name="fooSearch" class="foo.bar.fooAction" method="search">
    <result name="success">foo.jsp</result>
    <result name="input">search.jsp</result>
</action>

<action name="fooSubmit" class="foo.bar.fooAction" method="submit">
    <result name="success">foo.jsp</result>
    <result name="input">submit.jsp</result>
</action>

then instead of:

<s:submit action="foo" method="search" />
<s:submit action="foo" method="submit" />

something like:

<s:submit action="fooSearch" />
<s:submit action="fooSubmit" />

Upvotes: 1

Related Questions