More than one instance of the same form-bean-type in http-session required.
2 posts in topic
Flat View  Flat View
TOPIC ACTIONS:
 

Posted By:   Petter_Oqvist
Posted On:   Sunday, December 9, 2001 01:17 AM

I am using Struts to make a kind of browser that lets the user see and update different kinds of objects. User-dialogs that are a little bit more complicated can be divided in several steps and kept together by using http-session-scope to store some kind of bean-class that keeps track of the state. It seems to me that the most straight-forward way of using Struts implies that in the same http-session there can be only one instance of each form-bean type at a time. The names of the form-beans are specified in the file struts-config.xml In my case this is to much a restriction since I want each of my users (each having their own http-session) to be able to interact with several instances of the same    More>>


I am using Struts to make a kind of browser that lets the user see and update different kinds of objects.


User-dialogs that are a little bit more complicated can be divided in several steps and kept together by using http-session-scope to store some kind of bean-class that keeps track of the state.


It seems to me that the most straight-forward way of using Struts implies that in the same http-session there can be only one instance of each form-bean type at a time. The names of the form-beans are specified in the file struts-config.xml


In my case this is to much a restriction since I want each of my users (each having their own http-session) to be able to interact with several instances of the same form-bean-type at the same time without them interfering with each-other.


A simple solution would be to say that the scope should be "request" instead of "session" for the actions involved but this is not a viable solution for more complex dialogs (with several steps).


To be able to use the struts taglibs (e.g. struts-html, struts-form) it seams that the easiest way to get it to work is by having only one instance of each form-bean type at a time per http-session. I wonder if there is a good way of making it possible to solve this so that it is still possible to use the struts-taglibs. A reasonable approach would be to store each form-bean with a prefix so that each form-bean-instance would get a unique name within the http-session. This must be a dynamic parameter so it cannot be hard-coded in struts-config.xml but maybe it is possible in some other way?


Grateful for answers or suggestions.

   <<Less

Re: More than one instance of the same form-bean-type in http-session required.

Posted By:   Ted_Husted  
Posted On:   Monday, December 10, 2001 06:01 PM

Struts supports using a different form bean name for a
given ActionMapping through the attribute property.


You can also define the type for a form bean more than
once, but give it a different name.


There is also support support for bean prefixes and
suffixes, but I think the attribute property may be what
you are looking for.


HTH -Ted.

Re: More than one instance of the same form-bean-type in http-session required.

Posted By:   Petter_Oqvist  
Posted On:   Sunday, December 9, 2001 07:11 PM


If the ActionForm in itself acts as a holder of more ActionBeans and that each of these ActionBeans handles the data for a specific user interaction it will work. To keep track of which of the ActionBeans to choose in a particular ActionForm; a hidden index-variable in request-scope can be used. This way the Struts infrastructure can be used entirely if the extra work of indexing in the ActionForm to get hold of the right ActionBean is done on the JSP-page (and in the Action-class).


The code would look something like if this if we suppose that the index variable is called id:

URL from Action to JSP-page:


http://server/actionservlet/updateusergroup.jsp?id=123

updateusergroup.jsp


<% String id = URLHandler.extractId(request); %>


Update user group <%= updateUsergroupForm.getBean(id).getGroupName() %>
...

UpdateUsergroupForm.java


public class UpdateUsergroupForm extends ActionForm {

/** The form beans. */
Hashtable beansM = new Hashtable();

/** Fetch bean with specific id. */
public UpdateUsergroupBean getBean(String id) {
return (UpdateUsergroupBean)beansM.get(id);
}
...


UpdateUsergroupBean.java


public class UpdateUsergroupBean {

String name;
Vector members = new Vector();

public String getGroupName() {
return name;
}
...
About | Sitemap | Contact