|
JSTL - Accessing compound bean properties |
|
|
This sample shows you how to use <c:set> to set properties for compound beans, which are beans that contain references to other beans. In order to illustrate that approach, we'll create a compound bean Name which will
be refered as a property of the other component Person. The name bean is a compound bean because it
contains references to strings.
On initialization of person we set its name property to a bean object we create earlier. We use <c:set> action
with EL expressions to modify the first and the last names directly from a person reference:
A bean to be used as a property:
package test;
public class Name {
private String _first, _last;
public void setFirst(String first) { _first = first; }
public String getFirst() { return _first; }
public void setLast(String last) { _last = last; }
public String getLast() { return _last; }
}
|
A bean containing compound property:
package test;
public class Person {
private Name _name;
private String _email;
public void setName(Name name) { _name = name; }
public Name getName() { return _name; }
public void setEmail(String email) { _email = email; }
public String getEmail() { return _email; }
}
|
Page setting compound properties:
<%@ taglib uri='http://java.sun.com/jstl/core' prefix='c' %>
<jsp:useBean id='name' class='test.Name'>
<c:set target='${name}' property='first' value='John'/>
<c:set target='${name}' property='last' value='Tate'/>
</jsp:useBean>
Bean initialization and property setting:
<jsp:useBean id='subscriber' class='test.Person'>
<c:set target='${person}' property='name' value='${name}'/>
</jsp:useBean>
<html>
...
<c:out value='${person.name.first}'/>
<c:out value='${person.name.last}'/>
...
</html>
Related Tips
|
Page 1 of 0 ( 0 comments )
You can share your information about this topic using the form below!
Please do not post your questions with this form! Thanks.