Announcement Announcement Module
Collapse
No announcement yet.
jstl Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • jstl

    Hi there
    I am not sure if this is the right place to ask this question, but I am using Spring + JSP with JSTL.

    Say I have an object I can access on my JSP, like this:
    <c:out value="${company.name}" />

    Which in effect calls the "getName()" method on my "company" object.

    How do I manually call a method on the object? For example, what if I want to call a method which takes parameters, like "company.getAddress( 1 )" - where I want to call a method which takes an int as a parameter, and returns a List in this case.

    If there is a better place for me to find this sort of information I would be grateful for a pointer.

    Thanks,
    Peter

  • #2
    AFAIK, the EL '.' operator can only retrieve JavaBean-named properties.

    There's an intro to EL here: http://java.sun.com/j2ee/1.4/docs/tu...JSPIntro7.html

    Comment


    • #3
      Re: jstl

      Originally posted by pak
      Which in effect calls the "getName()" method on my "company" object.

      How do I manually call a method on the object? For example, what if I want to call a method which takes parameters, like "company.getAddress( 1 )" - where I want to call a method which takes an int as a parameter, and returns a List in this case.
      Why don't You use <%=company.getAddress(1)%> ?

      Artur

      Comment


      • #4
        Indeed I can do that, but only if I obtain my "Company" object like this:

        <%
        Company company = (Company)session.getAttribute("dk.ciber.organisati on.web.CompanyController.form.company");
        %>

        Which strikes my as slightly ugly. But workable.

        So I can then call
        <%
        List addresses = company.getAddress(1);
        %>

        But then I can't use a jstl forEach on the "addresses" List, can I? I think I have to use a normal Iterator instead.

        Peter

        Comment


        • #5
          How do I manually call a method on the object? For example, what if I want to call a method which takes parameters, like "company.getAddress( 1 )" - where I want to call a method which takes an int as a parameter, and returns a List in this case.
          If you want to something like this, which you can't do in EL, wouldn't it be better to do the method call in your controller, and add the results to your model?

          Surely the whole point of mvc is to make your views simple and 'java-less', with the data they need pre-digested and put in the model for easy access?

          Of course, where possible you can also write your classes with EL in mind, and provide getXxx methods without parameters for the data you know you're going to need in your view.

          So I can then call
          <%
          List addresses = company.getAddress(1);
          %>

          But then I can't use a jstl forEach on the "addresses" List, can I? I think I have to use a normal Iterator instead.
          If you make your object a request attribute, i.e.
          Code:
          <% request.setAttribute&#40;"addresslist", addresses&#41;; %>
          then I think you should be able to refer to ${addresslist} in EL.

          Comment


          • #6
            Originally posted by pak
            Indeed I can do that, but only if I obtain my "Company" object like this:

            <%
            Company company = (Company)session.getAttribute("dk.ciber.organisati on.web.CompanyController.form.company");
            %>

            Which strikes my as slightly ugly. But workable.

            So I can then call
            <%
            List addresses = company.getAddress(1);
            %>

            But then I can't use a jstl forEach on the "addresses" List, can I? I think I have to use a normal Iterator instead.

            Peter
            If you've set up the model for the view via Spring, then it will actually be available in the pageContext, so you don't have to go to the session. This is not much shorter though. But if you later want to iterate over the value you extract using the JSTL tags, you can yourself put the extracted collection into the pageContext. So you could do it with one line of old-style jsp code.

            <%
            pageContext.setAttribute("addresses", ((<CAST HERE>)pageContext.getAttribute("command")).getComp any().getAddresses(1);
            %>

            Something like that...

            Comment


            • #7
              It tends to suck how JSTL works in this area but you have to return the *whole* collection to the JSTL tag and then let it pick out the one you specify.

              This means that if it takes any sort of processing power to get the item you want you're better off using scriptlets and avoiding JSTL.

              I guess this is trying to push people in the direction of properly preparing their models up front before trying to render it, i.e. not bluring model and view like cmgharris said.

              Java code

              Code:
              public interface Company &#123;
              
                 /** @return all of the addresses */
                Address&#91;&#93; getAddresses&#40;&#41;;
              &#125;

              JSP code

              Code:
              <%-- prints address1 property of address 1 --%>
              <c&#58;out value="$&#123;company.addresses&#91;0&#93;.address1&#125;"/>
              or

              Code:
              <%-- loops through all addresses --%>
              <table>
              <c&#58;forEach items="$&#123;company.addresses&#125;" var="item">
                <tr>
                   <td>
                     <c&#58;out value="$&#123;item.address1&#125;"/>
                   </td>
                    <td>
                      <c&#58;out value="$&#123;item.address2&#125;"/>
                    </td>
                 </tr>
              </c&#58;forEach>
              </table>

              Comment

              Working...
              X