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

  • JSTL Help

    Is it possible to call a non-static method of a class in jstl?

    Ex. Given the code below, how would I call getPayableAmount()?
    Code:
    public class BillingStatement extends DBObject {
    public BigDecimal getPayableAmount() { BigDecimal amt; if (overwrittenPayableAmt.compareTo(OVERWRITEPAYMENT_NOTUSED) == 0) { amt = getComputedAmount(); } else { amt = overwrittenPayableAmt; } return amt; }
    }
    I know I cannot use ${bs.getPayableAmount()}. The payable amount is computed on the fly depending on the data. I also know that a workaround would be to create a class variable that stores the payable amount determined before the view is called. However having hundreds of functions having the same nature, it is a bit cumbersome to do it that way. Is there another work around it. I am also avoiding the use of scriplets and hope to use all-JSTL

    I am using jstl 1.2 and JSP 2.x

    Thank you very much for any help you can provide.

  • #2
    if you are using spring mvc then possible.
    you need to add the class into map.
    give more details that which mvc you are using.

    Comment


    • #3
      Yes I am using MVC Spring 3.0.

      Comment


      • #4
        Given your method respects the JavaBean conventions (getXXX), why can't you just use ${bs.payableAmount} ?

        Comment


        • #5
          getPayableAmount() does not return a class variable but is a method that uses several of the class variables to determine the payable amount

          bs.getXXX only works for getters and setters of class vars having the same name not the other methods as far as I know. I also tried it hoping that it might work... however, as expected, it did not work.

          Comment


          • #6
            There's no need for a getter method to have a corresponding field, it could be a 'virtual' field computed from several values.
            If your method signature is getXxx then you should be able to use ${bs.xxx}.

            In your case, you have to use ${bs.payableAmount} and not ${bs.getPayableAmount} (the 'get' disappear).

            Comment


            • #7
              Oh..... it works now! Thanks. This shows I am quite a newbie in jstl and spring...

              Another question though, how about methods with parameters?

              Code:
              public BigDecimal getBalance(boolean verifiedOnly) {
                      return getPayableAmount().subtract(getTotalPaidAmount(verifiedOnly));
              }
              ${bs.balance(true)} does not seem to work.

              Thanks a lot for all the help! Been learning a lot about spring and jstl lately.

              Comment


              • #8
                ${bs.balance(true)} does not seem to work.
                You should study the Expression Language, especially how and when it can be used. You should also take a look at the JavaBeans specification. In short, EL lets you navigate an object properties by calling standard JavaBeans getter methods. Per specification, getters have no attributes, so you can't do that. If you need to pass a parameter back to the server it means you need to somehow change or refresh the model so you should go the standard mvc way (i.e. trigger navigation by calling a controller -> populate the model from the business layer -> reload the page).

                Comment


                • #9
                  After searching the net a lot, I found thgat I can pass parameters using the ${bs.getBalance(true)} in the newest JSP version using the tomcat 7.

                  Thanks for all your help

                  Comment

                  Working...
                  X