0

I need to call a method from my action object inside the JSP, something like:

var currentStatus = ${getCurrentStatus()};

I cannot call an attribute, and I tried following this answer (How to call an action method using OGNL) and it didn't work.

  • Why can't you call an attribute? Getters are just functions--they can be anything you want (including a function). "It didn't work" isn't actionable--we're sorry it didn't work, but there's no way to understand *why* it didn't work without any details. You need to explain how you're calling it (a JSP? A JS file? etc.), what actually happens (error in JS console? E.g., if the `currentStatus` is a string, you've created illegal JS unless the status corresponds to a valid, declared JS identifier), and so on. – Dave Newton Mar 29 '18 at 12:58
  • It can't be an attribute as it needs to check dynamic, like going to the database. – Hola Soy Edu Feliz Navidad Mar 29 '18 at 13:19
  • As I said, the code inside a getter function is arbitrary: the JavaBean convention is a *naming convention* and says nothing about the code behind the name. (Noting that if it's a DB call it should be locally cached.) You can put *whatever code you want* in an attribute accessor, e.g., `getCurrentStatus`, accessible thru OGNL with `currentStatus`, could make a DB call, an HTTP call, an I18N property lookup, *it doesn't matter*. – Dave Newton Mar 29 '18 at 14:11
  • Now I see what you mean and it worked. If you want, place it as an answer and I'll set it as the right one. – Hola Soy Edu Feliz Navidad Mar 29 '18 at 15:40

1 Answers1

1

There are a variety of ways to call methods (on actions, on other objects, or static methods from classes) from OGNL.

In this case, however, I don't see any issue with using a normal accessor. Note that the JavaBean convention is almost (completely?) about naming. A getter named getCurrentStatus(), accessed simply in OGNL via currentStatus, can contain whatever code you want.

This could include the DB access you mention in your question, etc.

Dave Newton
  • 158,873
  • 26
  • 254
  • 302