File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Struts and the fly likes Populating value of hidden field.... Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCA/OCP Java SE 7 Programmer I & II Study Guide this week in the OCPJP forum!
JavaRanch » Java Forums » Frameworks » Struts
Bookmark "Populating value of hidden field...." Watch "Populating value of hidden field...." New topic
Author

Populating value of hidden field....

Dave Hewitson
Greenhorn

Joined: Jun 01, 2001
Posts: 27
I have a bean defined in my jsp like this...
<bean efine id="emp" name="empList" property="currentEmployee" type="bean.EmployeeCargoBean"/>
I am showing the user the attributes of the above bean for display only, and want to also "pass on" a value from this bean into the form so the next action in the sequence can do something with it.
So I also have a hidden field, like this...
<html:hidden property="employeeNum"></html:hidden>
How can I "load" this field with a value from the previously defined bean?
Sounds simple (and probably is !) but I can't work out how to do it!
Can anyone help?
TIA
Dave
Junilu Lacar
Bartender

Joined: Feb 26, 2001
Posts: 4777
    
    8

try
<html:hidden property="employeeNum" value="<%= emp.getSomeProperty() %>" />

Note that you could simply have Struts take care of populating this field for you. All you need to do is to set the property value in the appropriate form before the JSP that uses the form is displayed.
[ February 06, 2003: Message edited by: Junilu Lacar ]

Junilu - [How to Ask Questions] [How to Answer Questions]
Dave Hewitson
Greenhorn

Joined: Jun 01, 2001
Posts: 27
Hi Junila - thanks
I understand the first part of your reply - though I was trying to avoid using any scriptlets in the jsp.
I'm not sure what you mean about loading the form before the jsp is displayed. I presume you mean in the action previous to this?? But how can I access that form because until struts builds the page it won't be in the session will it?
Have I got the wrong end of the stick here?
Dave
Junilu Lacar
Bartender

Joined: Feb 26, 2001
Posts: 4777
    
    8

It's Junilu, thank you.
I assume you are following the recommendation to link only to actions. Doing this makes life a lot simpler and allows you to avoid scripting a great deal. Here's what I'm talking about:
first.jsp: contains a form, a hidden field - say employeeNum - without the value set, and a submit button. The form's action="first.do". first.do is associated with FirstAction which is associated with FirstForm. FirstForm would have getEmployeeNum() and setEmployeeNum(). I usually add a submit field in my forms to receive the value of the submit button. In the reset() method, I set the submit field to null.
Configure FirstAction with two forwards: say "display" and "process". The "display" forward will have the first.jsp as a target. The "process" forward will have "second.do" as its target.
FirstAction.perform() or execute() checks if the form (passed as a parameter) was submitted (submit != null). If not submitted, then retrieve the value for the employeeNum (from database or some bean already existing) and call the firstForm.setEmployeeNum(). Then return the "display" forward. This will process first.jsp and Struts will automatically set the value of the hidden employeeNum field. If the form was submitted, the employeeNum value is already in the request as a parameter so all you need to do is return the "process" forward, which will hand off control to the Action associated with "second.do", say SecondAction.
In the SecondAction.perform()/execute(), you can simply use request.getParameter() to get the employeeNum value and use it to prepopulate the second form, say SecondForm. If the employeeNum is also a field in SecondForm and that is all you need to be prepopulated, then don't have to do anything: Struts will automatically populate employeeNum with the value in the request parameter. You just return a "success" forward whose target is "second.jsp" which has a form whose action="second.do".
Clear as mud?
---
seem to be having some trouble posting messages today so sorry if I've confused you with my reply that I have since deleted
[ February 06, 2003: Message edited by: Junilu Lacar ]
Dave Hewitson
Greenhorn

Joined: Jun 01, 2001
Posts: 27
Hi Junilu (got it right that time!)
I think I understand your suggestion.
At the moment my forwards are to jsp pages, but I like the idea of forwarding to an action because it allows me to do some screen pre-processing that I couldn't easily do at present.
So you're saying that by using the button state stored on the actionform I can tell whether the form was submitted, or whether the page is being shown as a result of a call from another page.
So, because I'm going to another action, all the stuff the request is still there and I can pre-populate the actionform for the second screen.
Yes - that sounds like it'll work.
Many thanks
Dave.
Junilu Lacar
Bartender

Joined: Feb 26, 2001
Posts: 4777
    
    8

Just to make my life simpler, I'm using an ActionForm subclass as my base form that has the following:
Dave Hewitson
Greenhorn

Joined: Jun 01, 2001
Posts: 27
Is it possible to do that and use LookupDispatchAction?
I can't work out how to control doing the screen preprocessing, ie when normally the button would be null - because this is taken care of with the mappings in the action.
Dave
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Populating value of hidden field....