JSTL call a method that's not a setter or a getter

24,916

Solution 1

The instance of the exam object must be within an EL scope such as pageScope. Assuming that the exam object reference is refered to in the pageScope as "exam" then the following EL will call the getHarfTo method:

   ${exam.harfTo}

NOTE: you cannot call a setter method.

To expand on the above (and just as an example) we can set the exam instance in the pageScope using scriptlets (note that scriptlets are not recommened this is just an example for clarification)

    <%
        com.example.Exam exam = new com.example.Exam();
        pageContext.setAttribute("exam", exam, PageContext.PAGE_SCOPE);
    %>

now we can access the exam object via EL: ${exam.harfTo}

The exam object can be added to other scopes such as the request scope and the session scope.

Scriptlets and EL

   <%
        java.util.ArrayList cities = new java.util.ArrayList();
        cities.add("NYC");
        cities.add("SFO");
   %>

Variables created in a scriptlet are not directly accessible to EL. Thus, the cities variable cannot be used in JSTL tags.

To do so, we first need to put it in some scope. For example:

    <%
        java.util.ArrayList cities = new java.util.ArrayList();
        cities.add("NYC");
        cities.add("SFO");
        pageContext.setAttribute("cities", cities, PageContext.PAGE_SCOPE);
    %>

This makes the object referred to by cities variable, available in the pageScope by the name of "cities".

    <select name="Cities">
           <c:forEach var="city" items ="${cities}">
                  <option> ${city}</option>
           </c:forEach>
    </select>

The cities variable can now be accessed. You cannot have a scriptlet as the property of an EL expression.

    <select name="Cities">
           <%for(int i = 0; i < cities.size(); i++) {%>
           <option>${cities[<%=i%>]}</option>
           <%}%>
    </select>

or you can use Standard actions if the exam class conforms to bean conventions:

    <jsp:useBean id="exam" class="com.example.Exam" scope="pageScope"/>
    The value return by the method called harfTo: <jsp:getProperty name="exam" property="harfTo"/>

Dont forget to import the JSTL tag lib:

   <%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix ="c" %>

EDIT:

If you have a collection exams which contains exam objects then you can use JSTL to iterate over then collection like so:

 <c:forEach var="exam" items ="${exams}">
         ${exam.harfTo}
 </c:forEach>

Solution 2

You need a TLD to do what you intend.

This link has a really elaborate example .

HTH

Solution 3

To access the method, you just strip off the get part.

${exam.harfTo}

The same works if you want to call a class's setter method.

Share:
24,916
Eslam Hamdy
Author by

Eslam Hamdy

Senior engineer with +9 years experience in java ecosystem, have one plus year experience in crafting and managing agile teams, has a passion for nature, mountain biking, swimming, traveling, tensegrity and coding.

Updated on July 05, 2022

Comments

  • Eslam Hamdy
    Eslam Hamdy almost 2 years

    i have these method in a class:

     public HarfDate getHarfTo() {
            return to;
          }
    

    i just wonder how to call it in JSTL as i tried to call it like that:

    ${exam.getHarfTo()}
    

    but it doesn't work!!

    Note: exam is an instance of the class enclosing these method

    • to is not a static member inside the class if the solution is to make the method static