Marcelo Flores A. wrote:
> I need help...
> 
> I'm have data of javabeans than have other javabeans with attribute.  
> 
> class MyClass1 
> {
>       String name;
>       MyClass2 myClass2;   
> }
> 
> class MyClass2
> {
>       String name2;
> }
> 
> how I have across sintax tag the name2' s value  only with object MyClass1
> 
> <jsp:getProperty name="MyClass1"  property="name2">  throw a error.
> 
> I don't know how do it----

I know you've already received a number of answers, but since each one
just gives you a piece of the solution, let me give it a try as well.

First, <jsp:getProperty> and JSTL can not access public fields in a
class, only bean getter methods. So no matter if you want to use JSTL
or not, you need to expose all data you want to access through getter
methods. Depending on what makes sense for your application, you can
expose "name2" directly in MyClass1:

   1)

   class MyClass1 {
       String name;
       MyClass2 myClass2;
       ...
       public String getName() {return name;}
       public String getName2() {return myClass2.name;} // public field
   }

or expose MyClass2 in MyClass1, and then expose "name2" in MyClass 2:

   2)

   class MyClass1 {
       String name;
       MyClass2 myClass2;
       ...
       public String getName() {return name;}
       public String getMyClass2() {return myClass2}
   }

  class MyClass2 {
       String name2;
       ...
       public String getName2() {return name2;}
  }

Without JSTL, you can now get "name2" like this:

   1)

   <jsp:useBean id="myClass1" class="com.foo.MyClass1" />
   <jsp:getProperty name="myClass1" property="name2" />

   2)

   <jsp:useBean id="myClass1" class="com.foo.MyClass1" />
   <%
     // Must use scripting to get MyClass2 first
     MyClass2 myClass2 = myClass1.getMyClass2();
     // Must save in a JSP scope for getProperty to find it
     pageContext.setAttribute("myClass2", myClass2);
   %>
   <jsp:getProperty name="myClass2" property="name2" />

With JSTL, case 2) it's much easier:

   1)

   <jsp:useBean id="myClass1" class="com.foo.MyClass1" />
   <c:out value="${myClass1.name2}" />

   2)

   <jsp:useBean id="myClass1" class="com.foo.MyClass1" />
   <c:out value="${myClass1.myClass2.name2}" />

For details about using JSTL, see <http://java.sun.com/products/jsp/jstl/>

I hope this helps,
Hans
-- 
Hans Bergsten           [EMAIL PROTECTED]
Gefion Software         http://www.gefionsoftware.com
JavaServer Pages        http://TheJSPBook.com


--
To unsubscribe, e-mail:   <mailto:[EMAIL PROTECTED]>
For additional commands, e-mail: <mailto:[EMAIL PROTECTED]>

Reply via email to