Re: question (related to now I know why I was initializing Map in constructor)...

Vic Cekvenich <[email protected]> Sat, 07 Jun 2003 09:01:21 -0400
Newsgroups gmane.comp.java.mvc.devel
Organization baseBeans Engineering
Message-ID <[email protected]>

Rick Reumann wrote:

>On Fri, Jun 06,'03 (09:28 PM GMT-0400), Rick wrote: 
> 
>  
>
>>- I say 'should' because of course I haven't tested it yet. Hope it
>>works:)
>>    
>>
>
>Well of course I spoke too soon. I should have realized the plan I had
>of course wouldn't work. I'm really stumped by the best way to handle
>this Vic, and I'm sure it will come up in the future....
>
>The problem is when you have a FormBean tied to an Action, 
>

(of course in my head I think of html form tag and then... the formBean 
is tied to the form/page, and not the action, but...)

>the first
>thing Struts does is look for that FormBean in scope and if not in scope
>it creates the FormBean and then tries to populate the bean from any
>request parameters. 
>
OK, but most of the time .... there are none "parametars" / propeties 
since we in esence do a redirect.

>Of course this all happens with the default stuff
>(RequestProcessor etc).  So the problem is if you call an Action and
>pass in any properties BEFORE this FormBean is ever created at least one
>time and put into session scope, you will end up with null pointer
>exceptions since _current Map will be null. 
>
This happens on a submit back to the action, not other wise, methinks.

>This is sort of a big
>problem since you aren't always going to create a blank form with the
>"New" dispatch before you do anything. 
>  
>

I think of it as .... in the action, a developer has a chance to 
pre-populate. (on a submit, Struts populates form (page) values before 
the action, but otherwise we redirect)

>None of the solutions I can think of I like that much. Currently I'm
>doing this (which I really don't like)...
>
>First added these methods to BaseBean...
>
>public Map getCurrentMap() {
>        return _current;
>    }
>
This has to be in there already, don't tell me I don't have this and use 
propery? Please let know.

>    public void initializeCurrentMap() {
>        _rlist.add(new HashMap());
>        _size = _rlist.size();
>        _current = (HashMap)_rlist.get(size() - 1);
>    }
>
>
>Then in one of my Display disptach actions...
>
>
>  
>
By the time you code anything, Struts did it's thing.

>//make sure you have a ProjectDetailBean in session scope 
>//intialized with a new _current Map
>

>if ( ae.getSessionBean("projectDetailBean") == null ) {
>	ProjectDetailBean projectDetailBean = new
>	ProjectDetailBean();           
>	projectDetailBean.initializeCurrentMap();            		
>	ae.setSesBean("projectDetailBean", projectDetailBean );          }
>else {
>	ProjectDetailBean projectDetailBean =
>	(ProjectDetailBean)ae.getReq().getSession().getAttribute(
>			"projectDetail Bean");            
>	if (projectDetailBean.getCurrentMap()== null ) {               
>		projectDetailBean.initializeCurrentMap();               
> 	}
>}
>  
>
?? I do not do any of this in bP and it does all the CRUD, etc. ??
But it's not to bad the code.

>I really don't like the above very much since it now makes this Action
>to have to be TOTALLY aware of where it's going to.
>
You are thining good, OO and modular, actions should NOT know what is 
going on in bean.

>
>Some other ideas I thought of....
>
>1) Go ahead and just initialize a FormBeans with a new _current Map when
>the user logs in and put them all in Session scope. 
>

For all the possible beans and put in scope, nope.

>
>2) The best solution is probably to subclass the RequestProcessor and
>modify the processActionForm class. Have it check to see if the FormBean
>is of type BaseBean and if so make sure that _current is not null, and
>if it is make sure the _rlist is initialized with a new _current
>HashMap.
>  
>
Only if the RequestPorcessor has some data we care about, which would 
for a submit not for a redirect.

>This number 2 solution might be the best option. Another good reason
>for option 2 above is it will allow you to then use beans with Request
>scope. Currently the way bP is set up the user really will have to
>almost always be using Session scope for all beans. If they don't
>Session scope you can quickly run into problems since _current could be
>null.
>
>
>Either way something needs to be done since it is common to go directly
>to an action from a forward link with passed in parameters.
>
I do this here similar in MRCmsAdmin.jsp
    <c:url value="contentAdminPg" var ="url">
            <c:param name="ID" value="${row.id}"/>
            <c:param name="Dispatch" value="Edit"/>
    </c:url>
    <a href ='<c:out value="${url}"/>' >
         <c:out value="${row.id}"/>
     </a>

And in userList.jsp
  <display:column property="handle"      width="40%"        
title="Handle"    styleClass="text"
          href="/do/userAdminPg?Dispatch=Edit"                 
paramId="ID" paramProperty="id" />

Also most my List Action forward to the "zoom" action, within the list 
action mappings (chaining?)

And found no issues here or other project I did. I guess I still don't 
get where you are stuck this time. I must say I admire your presistance, 
you will go far in Sofware.




> If there is
>a bean associated with the Action (which there almost always would be)
>
A bean should ONLY be maped to a action mapping if it is for R/W, 
updates, inserts.
If the page is display only, than there should NOT be a bean in action 
maping, and most of the pages just display data. So I say most of the 
time there is no form bean assiociated with the action.
If it's display only, then in action you say bean = new bean and put it 
in request scope (not session for read only. For R/W it should be ins 
ession becuase validation errors, etc.)

>and this bean wasn't set up some time along the way in Session scope,
>then _current will be null. 
>

See above.

>  
>
>
>  
>

-- 
Vic Cekvenich,
Struts Instructor,
1-800-917-JAVA

Advanced <a href ="baseBeans.com">Struts Training</a> and project recovery in North East.
Open Source <a href ="baseBeans.com">Content Management</a>  basicPortal sofware
Best practice<a href ="baseBeans.com">Struts Support</a> v.1.1 helper ScafflodingXPress