Struts 2 post back default

125 views Asked by At

In the Struts documentation, it says:

Post back default

Another common workflow stategy is to first render a page using an alternate method, like input and then have it submit back to the default execute method.

How to do it using annotation only? It seems that only the execute() method is called.

1

There are 1 answers

0
Roman C On

In the documentation it's said to render a page can be used an alternate method like input. This means that when you submit a form on the page it can return back with the input result. Usually it happens automatically during validation process if the validation fails or it hasErrors. Then you can submit the form back to the default action's execute method. You don't need to specify a method in the action configuration. Also if you didn't specify the action attribute in the form tag then the same action will execute which was used to render a page.

Configuring actions you can use the same page for success result when rendering a page using GET method and input when POST method is requested.

To use annotations to configure actions mapping you can use a Convention Plugin.

Also note, to map a class method to the action you should put @Action annotation directly on this method rather than on the class.

More detailed explanation and documentation you can find here.

@Namespace("/")
public class ProductAction extends ActionSupport  {

 public String execute() { 
   return SUCCESS;

 }

 @Action(value="product",
   results=@Result(location="/product-list.jsp")
 )
 public String search() {
   return SUCCESS;
 }
}

Notice, that the method execute is not mapped, so it will not execute. If you need that method execute you should create mapping to it. For this purpose you could place annotation on class or on method execute.