0

In plain old servlets I can use doGet and doPost methods. Where in doGet i'm forwarding user to some page and in doPost i'm proccessing data entered from the page that I gave. That all happening in one servlet.

But the Struts2 works on Front Controller pattern and instead doGet/doPost I have only execute method. So how can I properly give user some page, so then he can see it, enter data, submit and application as result proccess it in execute ?

From what I know I have two options (example on registration form):

  1. Map page to another url:

    <action name="register_display">
        <result name="success" type="dispatcher">register.jsp</result>
    </action>
    
    <action name="register"
        class="magazine.action.client.RegisterClientAction"
        method="execute">
        <result name="success" type="redirectAction">/index</result>
        <result name="error" type="redirectAction">register_display
        </result>
    </action>
    
  2. Create whole package named display and place there all view from which POST can be performed:

    <package name="display" namespace="/display" extends="struts-default">
      <action name="register">
         <result name="success" type="dispatcher">register.jsp</result>
      </action>
    ...
    </package>
    

Is there any other options ? Which one is prefered ?

marknorkin
  • 3,904
  • 10
  • 46
  • 82
  • Show us the respective action from Struts.xml file. – Vinoth Krishnan Mar 02 '15 at 09:29
  • Create method, don't use `execute` method explicitly, create 2 actions (one w/o method, other with custom method). – Aleksandr M Mar 02 '15 at 09:56
  • You should use different action name unless you use REST style mapping of Struts actions. – Roman C Mar 02 '15 at 10:28
  • @RomanC I'm actually don't famillar with such mapping. but the point is that I should provide two methods in my action and mapped one for forwarding user to some page and another to submiting it ? – marknorkin Mar 02 '15 at 11:17
  • Why are you returning INPUT? That is usually used by the validaiton interceptor. – meskobalazs Mar 02 '15 at 11:24
  • @meskobalazs maybe I'm missleading something, but is such return can't be used ? I don't use interceptors for now, but have override `validate` method. from what i saw the mapping means ? would you explain ? – marknorkin Mar 02 '15 at 11:31
  • You do use interceptors, the whole framework is based on them, and the default interceptor stack uses the validation interceptor. The usual way is using SUCCESS for normal return, and ERROR for anything else, and letting the framework return INPUT when there was a validation error. – meskobalazs Mar 02 '15 at 11:34
  • @meskobalazs oh, then if I override `validate` method it will implicitly return INPUT ? ps I replaced input with success – marknorkin Mar 02 '15 at 11:36
  • If the validate method (or the validation annotations) encounters errors, it returns INPUT. Otherwise it continues normally. – meskobalazs Mar 02 '15 at 11:59
  • @Mark Actions have different code like in doPost, doGet method, but in Struts you have to give it a unique name, hence different url. With the same action name you can only map to one method of the action class. Read [it](http://stackoverflow.com/a/17166514/573032), it may help. [This](http://stackoverflow.com/a/28280790/573032) also useful. – Roman C Mar 02 '15 at 11:59
  • BTW it is not clear to me what you want to do. You want to POST a form, then trigger a GET request to another URL? – meskobalazs Mar 02 '15 at 12:04
  • @meskobalazs No. Look, before user will enter-submit data in some form on some page he should see this page. so how should I give him this page ? – marknorkin Mar 02 '15 at 12:12
  • @Mark Short answer: the same way as any other page. Longer answer: I did a couple projects where I had an interceptor that handled GET and POST differently, so my actions had `handleGet` and `handlePost` or just `get` and `post` respectively. In the long run it wasn't terribly valuable. Note there's also a forwarding action that's trivial to configure if you're using XML. It would also be simple to "solve" this in a number of ways for either XML or annotation configs. – Dave Newton Mar 02 '15 at 13:06

1 Answers1

1

In the standard Struts2 style, an Action class has only one work method, this is the execute method. However, you do not necessary have to follow this. You can define multiple actions in a single Action class.

For example you make a GET request to users, which is handled in the default execute method of UsersAction.

@Override
public String execute() {
    // fetch the list of users
    return SUCCESS;
}

Let's suppose you would like to add a new user in this same action, by POSTing to user_add. So you define an add method:

public String add() {
    // add the user
    return SUCCESS;
}

The struts.xml would look similar to this:

<package name="users" extends="defaultPackage">
    <action name="users" class="com.example.UsersAction">
        <result>users.jsp</result>
    </action>

    <action name="user_add" class="com.example.UsersAction" method="add">
        <result type="redirect">users</result>
    </action>
</package>

In your scenario, you would render your page, which the user should see after the run of the (maybe empty) execute method. Then, you would make the post request, which would be mapped to the other method of the Action class.

meskobalazs
  • 15,741
  • 2
  • 40
  • 63