You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@struts.apache.org by Conrad CRAMPTON PSE 52704 <co...@kent.pnn.police.uk> on 2005/01/19 14:02:20 UTC

Pre populate form missing values from nested object

Hi,
The list archive search doesn't appear to work to forgive me if this is
a simple and already answered topic.
 
I have an actionform that has an object as a property - Premises
premises, and a jsp that uses <html:text name="Premises"
property="premises.name" />.
When I use createPremises.jsp all is ok and the values for premises get
through to my action no problem.
 
The problem I have though is when I want to edit premises. The action
(extending Dispatchaction) viz...
 
public ActionForward edit(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response) throws
IOException, ServletException {
  PremisesForm premForm = (PremisesForm) form;
  String returnMapping = "edit";
  Long id = null;
  if (request.getParameter("id") != null)
   id = new Long(request.getParameter("id"));
  if (id == null)
   id = (Long) request.getAttribute("id");
  try {
   PremisesDAO pdao = new PremisesDAO();
   Premises prem = pdao.getPremises(id);
   premForm.setPremises(prem);
   premForm.setOwner(prem.getOwner());
   request.setAttribute("Premises", premForm);
  } catch (DataAccessException e) {
   e.printStackTrace();
   returnMapping = "error";
   request.setAttribute("error", e.getMessage());
  }
  return mapping.findForward(returnMapping);
 }

which is fine and sets the PremisesForm premises attribute correctly.
However when this gets submitted ultimately from the jsp, the properties
of premises that aren't html:text elements in my jsp aren't being
retained. e.g. id (primary key of premises from db, and other things
like collections of other pojo's in premises.
 
So the question is this - how do I retain values of nested properties
that aren't required to be (or cannot be) represented in the jsp? It
appears that the value of premises is always null and not using what is
in the request scope when the jsp is submitted.
 
I am sure this is simple answer but I just can't get it!
 
TIA
Conrad
 
 

Conrad Crampton
Software Solutions Manager
Force Headquarters
11 Edinburgh Square
Sutton Road
Maidstone
ME15 9BZ
01622 653283 (ext)
19-3283 (internal)
07814 011752 (mobile)

Re: Pre populate form missing values from nested object

Posted by Hubert Rabago <hr...@gmail.com>.
> So the question is this - how do I retain values of nested properties that
> aren't required to be (or cannot be) represented in the jsp? 

The only values that you'll get when the form is submitted are the
values that the form has.
If you have values that you want to submit with the form, but don't
want them displayed to the user, look at hidden fields <html:hidden
/>.


> It appears that
> the value of premises is always null and not using what is in the request
> scope when the jsp is submitted. 

Roughly, a "request" begins when the browser asks for information from
the server, and ends when the server has supplied that information. 
After you've created your Premises object and displayed it on your
JSP's form, the request is done.  When the user submits the form, that
counts as another request, a new, empty, fresh one, with nothing yet
in its scope, except for the data that comes with the form being
submitted.

If you want your form to persist between requests, put your form in
session scope.  Take a look at
http://java.sun.com/developer/EJTechTips/2003/tt1209.html#2 for a
better explanation of the differences between scopes.


Here's another link that might help you manage your form data,
particularly collections that you may place in <select> controls:
http://www.reumann.net/struts/articles/request_lists.jsp

hth,
Hubert


On Wed, 19 Jan 2005 13:02:20 +0000, Conrad CRAMPTON PSE 52704
<co...@kent.pnn.police.uk> wrote:
>  
> Hi, 
> The list archive search doesn't appear to work to forgive me if this is a
> simple and already answered topic. 
>   
> I have an actionform that has an object as a property - Premises premises,
> and a jsp that uses <html:text name="Premises" property="premises.name" />. 
> When I use createPremises.jsp all is ok and the values for premises get
> through to my action no problem. 
>   
> The problem I have though is when I want to edit premises. The action
> (extending Dispatchaction) viz... 
>   
> public ActionForward edit(ActionMapping mapping, ActionForm form,
> HttpServletRequest request, HttpServletResponse response) throws
> IOException, ServletException {
>   PremisesForm premForm = (PremisesForm) form;
>   String returnMapping = "edit";
>   Long id = null;
>   if (request.getParameter("id") != null)
>    id = new Long(request.getParameter("id"));
>   if (id == null)
>    id = (Long) request.getAttribute("id");
>   try {
>    PremisesDAO pdao = new PremisesDAO();
>    Premises prem = pdao.getPremises(id);
>    premForm.setPremises(prem);
>    premForm.setOwner(prem.getOwner());
>    request.setAttribute("Premises", premForm);
>   } catch (DataAccessException e) {
>    e.printStackTrace();
>    returnMapping = "error";
>    request.setAttribute("error", e.getMessage());
>   }
>   return mapping.findForward(returnMapping);
>  }
>  
> which is fine and sets the PremisesForm premises attribute correctly.
> However when this gets submitted ultimately from the jsp, the properties of
> premises that aren't html:text elements in my jsp aren't being retained.
> e.g. id (primary key of premises from db, and other things like collections
> of other pojo's in premises. 
>   
> So the question is this - how do I retain values of nested properties that
> aren't required to be (or cannot be) represented in the jsp? It appears that
> the value of premises is always null and not using what is in the request
> scope when the jsp is submitted. 
>   
> I am sure this is simple answer but I just can't get it! 
>   
> TIA 
> Conrad 
>

---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
For additional commands, e-mail: user-help@struts.apache.org