how to use <bean:write > tag in strut 1.2?
Asked Answered
F

3

7

How to Use <bean:write> tag in Struts 1.2.

In name attribute, what value have to be used? Is bean name your property name?

Fiddle answered 14/7, 2011 at 12:18 Comment(0)
W
33

Javadoc for <bean:write>:

Specifies the attribute name of the bean whose property is accessed to retrieve the value specified by property (if specified). If property is not specified, the value of this bean itself will be rendered.

In essence, if you have a JavaBean (with getters and setters),

Person person = new Person;
request.setAttribute("person", person);

by setting <bean:write name="person" property="age" />, you're telling Struts to first find person object first from PageContext scope. If not found, then request, then session, then application scope.

The property="age" attribute (from <bean:write /> tag), will then call the getter method getAge() from the Person object (irrespective of whether there's an instance variable called age on the bean).

Hope this helps.

Whitsun answered 14/7, 2011 at 12:29 Comment(1)
now, in case if i want to use "age" property again, how will i retrieve the value? Is that possible? My scenario is, on click of button b1, i fetch certain value, let us say "age" and populate it on the jsp page using <bean:write name="person" property="age"/> If I press another button b2, I am not able to get the value from this age property. form.getAge() returns null where 'form' is of type Person.Leftwich
P
4

In order to display person.getAge() you would use

<bean:write name="person" property="age" />
Photosensitive answered 14/7, 2011 at 12:22 Comment(0)
A
0

The "name" attribute should specify the name of the bean. For example, if you're attempting to output a property from an ActionForm, the name attribute should be set to the name of the ActionForm, and the property attribute should be set to the property of the ActionForm you want to write. So in this case you might do:

<bean:write name="productInfo" property="summary" />

If you declare a non-ActionForm bean using a tag for example, then the name attribute would be set to the name of that defined bean:

<bean:define id="displayText" value="Text to Display" />
<bean:write name="displayText" />

Note that the property attribute is missing in this case, in which case the tostring value of the bean itself will be displayed.

Admittance answered 14/7, 2011 at 12:35 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.