How do I call a java method in JSTL?
This may be a recurring question.
I just want to call a method that is not a getter or setter for example. makeCall (someObj, "stringvalue") of the xyz class.
Java class
Class XYZ{
public String makeCall("someValue1","stringValue2"){
//some logic here
}
}
JSTL
<jsp:userBean id="xyz" class="com.XYZ"/>
${xyz.makeCall("hello","Friend")}
+3
Sachin J
source
to share
3 answers
For this we need to create our own tag. (in .tld file)
and need to write one java class for this tag.
After that, you can call a method inside that native class and set the result to pageCotext to retrieve it to jsp.
+1
Sachin J
source
to share
Just create an object of the class with <jsp:useBean>
and call the method using the JavaServer Pages Standard Tag Library or Expression Language , which is easier to use and less error prone.
example code:
<jsp:useBean id="test" class="com.x.y.z.XYZ"/>
${test.methodXYZ(object,"myString")}
Read more on Implicit Objects that can help you.
+2
Braj
source
to share
Try the following:
<c:out value="${XYZbean.makeCall(someObjBean, 'value')}" />
+1
Balduz
source
to share