The road so far….

March 1, 2013

Tip ! : How to call static method from JSP

Filed under: java — Tags: , — Rahul Sharma @ 9:25 am

Every application has some user information which it displayed to the logged-in user. My application also has the same information, but at few places it needs to be converted to JSON format as well, besides being displayed. Now either I can pass this JSON information from my Controller or I can somehow reuse the existing information that is being  passed. I choose the second route, but then the next question comes to your mind is how to convert this  on server-side ? on client side etc. ? Since we already had a static method to do  such conversions so I needed to figure out a way how I can call the same static method which could do the conversion on JSP  ?

Answer : Spring EL, is the way to do this. Using this you can access any static method of a class or an Enum on the JSP.

<%@taglib prefix="s" uri="" %>

<s:eval expression="T(com.rahul.devlearnings.MyClass).callStaticMethod()" var="data1" />

<s:eval expression="T(com.rahul.devlearnings.MyEnum)" var="data2" />