How to use multiple forms / actions in one view in Struts 2

I have a search box that appears on every page. The JSP code for the search box is inserted on each page through the tiles.

In the search box there is a form and class of actions SearchActionthat need to preload some properties for drop-down lists. The class SearchActionhas a method input()that performs this initialization.

Some pages also have their own form in the main area. Also with their own class of actions. They also have a method input()that does some preloading.

  • Can I use two actions in the same view?
  • How each form can have access to its properties of its actions.
  • And how do I manage to call the input method of both classes of actions before displaying the JSP?

Update:

I am adding a shorthand example, since it is probably not entirely clear what I'm trying to do. This is the registration page register.jspwith RegisterAction. And the page also contains a search form. (BTW: I left getter / setter and other things in action classes to keep it short):

register.jsp:

<s:form action="executeSearch">
    <s:textfield key="name" label="Name"/>
    <s:textfield key="age"  label="Age"/>
    <s:submit/>
</s:form>

<s:form action="executeRegister">
    <s:textfield key="firstName" label="First Name"/>
    <s:textfield key="lastName" label="Last Name"/>
    <s:textfield key="age" label="Age"/>
    <s:submit/>
</s:form>

struts.xml:

<action name="*Search" class="action.SearchAction" method="{1}">
    <result name="success">/searchresult.jsp</result>
</action>

<action name="*Register" class="action.RegisterAction" method="{1}">
    <result name="input">/register.jsp</result>
    <result name="success">/registerOk.jsp</result>
</action>

SearchAction.java:

public class SearchAction extends ActionSupport {

    private String name;
    private int age;

    @Override
    public String input() throws Exception {
        // preload the search form with some demo data
        name = "test";
        age = 20;
        return INPUT;
    }

    @Override
    public String execute() throws Exception {
        return SUCCESS;
    }

    ...
}

RegisterAction.java:

public class RegisterAction extends ActionSupport {

    private String firstName;
    private String lastName;
    private int age;

    @Override
    public String input() throws Exception {
        // preload the register form with some demo data
        firstName = "John";
        lastName = "Rambo";
        age = 10;
        return INPUT;
    }

    @Override
    public String execute() throws Exception {
        return SUCCESS;
    }

    ...
}

Let's say I call an action inputRegister.action. Then called RegisterAction.input(). Properties set. And the result SUCCESScauses the display of register.jsp.

. . , ValueStack. SearchAction, . age . , RegisterAction -properties ( ValueStack). SearchAction. 20, 10.

Rendered Jsp Page

. , .

+4
3

, . . , , ValueStack, OGNL. . , , , OGNL. , input().

EDIT:

, . . , bean - .

<s:form action="executeSearch">
    <s:textfield key="searchBean.name" label="Name"/>
    <s:textfield key="searchbean.age"  label="Age"/>
    <s:submit/>
</s:form>

<s:form action="executeRegister">
    <s:textfield key="firstName" label="First Name"/>
    <s:textfield key="lastName" label="Last Name"/>
    <s:textfield key="age" label="Age"/>
    <s:submit/>
</s:form> 

: key name, label value.

searchBean, . bean , #session.. OGNL .

+1

(, ): , , , , .

, Ajax, . , http://struts.jgeppert.com/struts2-jquery-showcase/index.action. , . Ajax, , , , . struts 2 jquery .

Ajax ( - ), . EACH, , struts , . , , - , !

+1

Ok. . Google , <s:action /> JSP , . , ValueStack . :

<!-- Initializes search model (preloading and stuff). -->
<s:action var="searchAction" name="inputSearch" >
    <s:param name="origin" value="inputRegister"/>
</s:action>

<!-- Pushing search action on ValueStack for the search form. -->
<s:push value="#searchAction">
    <s:form action="executeSearch">
        <s:hidden name="origin"/>
        <s:textfield key="name" label="Name"/>
        <s:textfield key="age"  label="Age"/>
        <s:submit/>
    </s:form>
</s:push>

<!-- This is the main form. It knows nothing about the search action. -->
<s:form action="executeRegister">
    <s:textfield key="firstName" label="First Name"/>
    <s:textfield key="lastName" label="Last Name"/>
    <s:textfield key="age" label="Age"/>
    <s:submit/>
</s:form>

. , :

  • . , . , "origin". . , .
  • The search action is initialized very late when the JSP rendering is already fully advanced. Exceptions at this point can be problematic. I had to make sure to catch all the Exceptions.
0
source

Source: https://habr.com/ru/post/1598532/


All Articles