Web tier: servlets, JSP, Web frameworks: How can I nest Javabeans or is there a better way?

  1. I am building an application that allows for multiple (unknown number of) addresses, so I thought about making an "address" bean that I would call from my other bean. The problem is I don't know the best way to do this. Specific questions I have are as follows:

    1) How do I access the setter/getter methods from JSP (this will be displayed via browser)

    2) Is this the best direction to go? (e.g. Person bean includes zero to many Address beans) This will apply for the ActionForm as well.

    I've never tried to implement something like this so any help will be appreciated. I've added the Addressbean in the Person bean and can access it from other Java classes. The problem I'm having is accessing it via the JSP file in the View. Also, how do I implement multiple addresses for the same person? Using a List or something?

    Thanks in advance,

    Threaded Messages (4)

  2. use list[ Go to top ]

    In jsp file you can access the address list in a PersonBean. Several lines of code can meet you goal. Remember that java code can be embeded in Jsp.

  3. Thank you for your reply. However, everything I've read highly recommends avoiding Java code in JSPs as much as possible. Is this one of those cases where it's not possible? I'm sure I'm not the only one that has something like this. What is normally done in a case like this?

    Scenerio is--I have a person that can have multiple addresses (physical, mailing, etc). I've developed a "Person" bean object that will include a "List" of addresses. To view this, I'll have to include Java code in the JSP (in a Struts framework).

    Is this the best way to solve my problem?

    Thanks in advance,
  4. Yes, but...[ Go to top ]

    This is a much debated topic...

    A lot of people would tell you to use Struts. Struts allows you to only use special tags which would access the bean and its properties. However, since you seem somewhat new to JSP's, I would not reccomend Struts to you, as it can be very complex for beginners.

    When someone says "As little Java code in JSPs as possible" they usually mean one of 2 things: 1) use something like Struts to remove all Java code, or 2) that you should put most of the code into bean classes (which you are doing) and then use only a few lines of code to get what you want from the bean. For your situation, I would reccomend option 2.

    Your code would look something like this:

    List addressList = person.getAddressList();
    for( all items in addressList)
        Address address = addressList.nextItem();

    Or something like that, you get the idea. See how this is only a few lines. You could have put ALL of your logic into the JSP, but that would be ugly. I hope this helps.
  5. Thank you for your help.[ Go to top ]

    I ended up working through what you referred to as option 1 yesterday. Here's what I ended up with (it works, but any feedback if this is the right way to do it would be appreciated):

    ---JavaBean (PersonObject)--------------------------------

    public ArrayList addresses;

    public ArrayList getAddresses(){
    return addresses;
    public void setAddresses(ArrayList x){
    this.addresses = x;

    ---JavaBean (AddressObject)--------------------------------------

    public String getHtmladdr(){
    String x = "";
    if(address != null) {x = x + address + "<br>";}
    if(address != null) {x = x + address2 + "<br>";}
    if(pobox != null) {x = x + "P.O. " + pobox + "<br>";}
    if(city != null) {x = x + city + ", ";}
    if(state != null) {x = x + state + " ";}
    if(zip != null) {x = x + zip;}

    return x;

    ---JSP File---------------------------------------------------

    <logic:iterate id="po" name="brokerDetailForm" scope="session" property="broker.addresses">
    <span class="broker_fieldtitle">
    <bean:write name="po" property="type"/>
    <bean:write name="po" property="htmladdr" filter="false" /><br>