Hi All,

This is the correct approach; but you can simplify things by writing a base
action class that your 'ReadUserAction' class extends. The base class might
look something like the one shown below. In this the setForm method uses the
entries from struts-config (mapping) to put the form in the correct scope,
using the correct key. This way you can change your struts-config without
having to change your code.

Hope this helps,

Jon Ridgway

----------------

package uk.co.upco.common.struts;
/*
 * (c) Copyright 2001 UPCO.
 * All Rights Reserved.
 */

import java.lang.reflect.InvocationTargetException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import org.apache.struts.action.Action;
import org.apache.struts.action.ActionMapping;
import org.apache.commons.beanutils.PropertyUtils;


/**
 * @version     1.0
 * @author              J Ridgway
 */
public class BaseAction extends Action
{
        protected void copyProperties (Object destination, Object source)
                throws ServletException
        {
                try
                {
                        PropertyUtils.copyProperties (destination, source);
                }
                catch (InvocationTargetException e) 
                {
                Throwable t = e.getTargetException();
                if (t == null) 
                {
                t = e;
                }
            throw new ServletException("populate", t);
        } 
        catch (Throwable t) 
        {
                throw new ServletException("populate", t);
        }
        }
        
        protected void removeForm (ActionMapping mapping, 
                                HttpServletRequest request)
        {
                if ("session".equals(mapping.getScope()))
                {
                        HttpSession session = request.getSession ();
                        session.removeAttribute(mapping.getName());
                }
                else
                {
                        request.removeAttribute(mapping.getName());
                }
        }
        
        protected void setForm (ActionMapping mapping, 
                                ActionForm form,
                                HttpServletRequest request)
        {
                
                if (mapping.getName() != null) 
                {
            if ("request".equals(mapping.getScope()))
            {
                request.setAttribute(mapping.getName(), form);
            }
            else
            {
                HttpSession session = request.getSession();
                session.setAttribute(mapping.getName(), form);
            }
        }
        }
        
}

-----Original Message-----
From: Thierry Ruiz [mailto:[EMAIL PROTECTED]] 
Sent: 02 May 2002 11:20
To: Struts Users Mailing List
Subject: Re: Initializing formBeans

At 09:25 AM 5/2/02 +0200, you wrote:
>Hi !
>
>I'm a newbie with struts and I cannot find the right way to do a simple
>thing like this :
>
>I would like to create an HTML form to modify the user information I
>recorded in my database.
>
>I didn't find how to initialize my form on a user-basis (the userId is
>stored in the session).
>
>Thanks
>
>Arnaud
>
>-------------------------------------------
>Arnaud GADBY
>
>EILEO
>9ter rue Carnot
>94270 Le Kremlin Bicetre
>FRANCE

Hi Arnaud,

I guess that you're searching a way to initialise the form with the 
informations
of the existing user. I've been faced with something similar. Here is what 
I did :

The thing is that I haven't found a built-in way to pass the session 
(containing the user id)
to the form implementation, and the ActionForm.reset() method  that could 
have been
used to populate the form with user data is not called (If somedy knows 
why...).

So I created a specific Action in that goal. In the doPerfom method I 
create a new instance of the form
bean and populate it with the user informations (that is in your session I 
guess).
The key is that this new form instance should be set in your session with 
the same attibute name
defined in struts-config <form-bean>


This Action is called by an 'edit user' link or button and forwards to the 
user form page.


Hope this help.

Thierry Ruiz
SchlumbergerSema.



--
To unsubscribe, e-mail:
<mailto:[EMAIL PROTECTED]>
For additional commands, e-mail:
<mailto:[EMAIL PROTECTED]>

The contents of this email are intended only for the named addressees and
may contain confidential and/or privileged material. If received in error
please contact UPCO on +44 (0) 113 201 0600 and then delete the entire
e-mail from your system. Unauthorised review, distribution, disclosure or
other use of this information could constitute a breach of confidence. Your
co-operation in this matter is greatly appreciated. 

--
To unsubscribe, e-mail:   <mailto:[EMAIL PROTECTED]>
For additional commands, e-mail: <mailto:[EMAIL PROTECTED]>

Reply via email to