Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Populating html text boxes dynamically

Status
Not open for further replies.

murtazac

Programmer
Mar 25, 2004
4
US
Hi,
i am trying to populate a html textbox in a JSP page dynamically, from a bean,but i keep getting an error message below is the code and the error message:

<td align="left" class="formTable"><html:text property="last" value="<bean:write name="user" property="lastname"/>" /><html:errors property="last"/></td>

teh error message i am getting is

[ServletException in:/tiles/body_editUser.jsp] /tiles/body_editUser.jsp(34,115) equal symbol expected'

plz help!!!

thanks,
Murtaza
 
Don't use a bean write, try

<html:text name="user" property="lastname"/>

A bean:write in that context is not valid an unneccesary, you can access the property directly in the tag..

Thats how I see it but I am fairly new to struts..
 
You have a bean:write tag nested inside a html:text. Tag lib does not support nested tag.

Let say you have the form-beans defined in struts-config as

<form-bean name="userFormBean" type="MyProject.UserForm">

And assuming you have the setting and getter method for last property in UserForm class.

You could:
1. set the last property in the formBean:
Code:
<html:form .....>
<!- after thml form tag, i.e. the form bean is created -->
<%
UserForm form = (UserForm) request.getAttribute("userFormBean");
form.setLast(((User)request.getAttribute("user")).getLastname());
%>
<!-- value already set to the 
<html:text property="last"/>"
2. use JSP expression:
Code:
<html:text property="last" value="<%= ((User)request.getAttribute("user")).getLastname()%>" />
 
Since "user" is already in scope (for the bean:write) can't he just access that directly by name, calling the property for it, in the html:text tag? Thats what I do and it works pretty well and keeps jsp out of the struts page which I prefer but is by no means a 'good' or 'bad' thing.
 
I just wonder how this work.

In the form, the property name is "last"

<html:text property="last">

In the User bean, the property name is lastname

<bean:write name="user" property="lastname"/>

So how is struts know that:

<html:text name="user" property="lastname"/>

is refering to property "last"
 
I guess I don't understand your design paradigm.

I usually map my forms 1 to 1 with the form bean. So if the bean property is 'lastname' the form field name is 'lastname' and then the actionform and directly impact the form fields.

Either your designing with a different concept or your 'user' has nothing to do with the 'form'.

JSP is probably your best bet, sounds like your doing something complex that I am not seeing.
 
siberian, I like you solution. But for it to work, the property name for the form field and corresponding bean must be the same, i.e. they both must named "lastname" or "last". Am I right?
 
siberian, I didn't design it, it just came to my attention that in murtazac's code, they are not one to one mapped. That's is why I'm wondering "<html:text name="user" property="lastname"/>" will work withing change the User bean class.
 
Not really, you can add a getter method to your actionform class that returns the proper value. Then you can have different names.

However, if its the same object I am not sure why you would have different names involved. If its lastname in your user object and the form field is intended to reference that object why not keep the name the same and make life simpler.

So, short answer, just add a getter method to your actionform. Long answer, I would syncronize my form fields with my action form and then back into my object so it all flows transparently.

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top