Juan Antonio Juan Antonio - 1 month ago 7
Java Question

How to make two actions to work together in Struts 2

I need to load some data from a table to show in a JSP page.

Then, when the user pushes on a button, they'll send the form to save in a database.

I wonder if I can create a method to load the

init
data only (I don't want to call the method execute, which will save the data).

I've tried it with implements
Preparable
but it calls execute method. In Struts2 I have a method called init to load data but I can't do it in Struts2.

What should I do, create an action to load data (loading with execute method) and another action to save data?

Answer

You can create a method in action class call it whatever you want and map this method to the action. This method should return a JSP page as a result. All you have configure in the same file. prepare is extremely useful when you need to return a result without action execution, like an input result when validation fails.

In Struts2 every request that is mapped by the filter is an action and it has an action context. A request has two methods that most popular GET and POST. You create two separate actions for GET and POST request. This is actually needed to follow PRG pattern.

Comments