Reputation: 1464
My problem is that I have an Action and a List into this action that I get some items searched from a database.
The situation is:
User make a search, I put items found on this list and show it into a grid to the user.
Till here is ok.
The problem is when the user ask to print into a pdf the data he got showed in the grid.
When I back to the Action, my attribute "listItems" is null.
I've created a constructor into my Action and I saw that all the times user make a request, I create a new instance of the action, I mean:
Below part of code:
public class MyAction extends AbstractMyAction<MyObject, MyFilter, MyService>{
private List<MyObject> list;
//getter and setter...
//method that fill my list...
}
I need that this attribute "list" keep with his values after I send result back to the user and when the user request for another action, like asking to print the content into this list, for example.
Is there some how to make a "conversation scope" or something like this?
I need that my list keep alive when the user ask me to print.
Upvotes: 0
Views: 758
Reputation: 1576
There are a number of ways to solve this, including some of the above mentioned. For my own needs, and because I'm a code junkie who loves to write code but hates inelegance, overkill, or continual boiler-plate repeated over and over, I created a Struts2 Conversation plugin.
Depending on the scope of your application, time to learn technologies and deliver a product, etc., I find the plugin useful for my own needs and I have tried to make it very simple to use to quickly develop wizards in Struts2 (which I noticed from my own and my colleagues' experiences was sometimes a pain in the arse).
The Conversation Plugin is not intended as an alternative to the Spring or CDI plugins per se. It is not a dependency injection or inversion of control framework. It is geared specifically towards the scope of models that exist in action classes and are accessed in views. It also has some unique features like package-based conversation nesting, configuration by naming conventions, and an extension to the Struts2 Config Browser for viewing action's conversation details at run-time.
It takes very little time to learn to use and very little time to configure. And if the documentation not helpful enough, let me know and I will quickly elaborate on the documentation and beef it up on the site.
Also, if anyone thinks the plugin is a bad approach, I entreat you to share your insights!
Upvotes: 0
Reputation: 160311
Please don't do this; while it may be possible, it runs counter to the framework, and all testing/etc. is done with the normal "prototype" scope. Move outside the framework's norms and you're on your own.
The canonical approaches are outlined in this FAQ entry. In general implementing the Preparable
interface is the preferred mechanism. The attribute (the list) itself may be kept in session (and removed when no longer required), or your backing cache mechanism may be used to reduce any time penalties incurred by the service that fills the list from the DB.
Upvotes: 1
Reputation: 7344
You tagged this question with Spring. If this means Spring is creating your Struts2 actions then you can use a Spring scope="session"
in the corresponding bean. I believe the default is request
which would cause the behaviour you see.
Oh, I'd recommend enabling this carefully and not to abuse it since it can lead to overly complex and confusing actions.
You can read more about the Spring plugin in the official site or in other tutorials.
Upvotes: 0
Reputation: 23587
Strts2 Action work as a Data Transfer Object and that is why each request to action class lead to creation of new instance of Struts2 Action and i will not recommend to think about changing this as you are planning to change how the framework work and can lead to overall unreliable behavior of application.
A simple solution of this problem is to use Session and if you are using spring can use session scope
.
Another solution is persisting the List between different calls of the action and make it available.
Upvotes: 0