Java JSTL 调用不是 setter 或 getter 的方法

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/20537648/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 02:36:56  来源:igfitidea点击:

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

javajspjstljsp-tags

提问by Java Player

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:

我只是想知道如何在 JSTL 中调用它,因为我试图这样调用它:

${exam.getHarfTo()}

but it doesn't work!!

但它不起作用!!

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

注意:exam 是包含这些方法的类的实例

  • to is not a static member inside the class if the solution is to make the method static
  • 如果解决方案是使方法静态,则 to 不是类内的静态成员

回答by hglasgow

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

要访问该方法,您只需去掉 get 部分。

${exam.harfTo}

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

如果您想调用类的 setter 方法,则同样有效。

回答by seeker

You need a TLD to do what you intend.

您需要一个 TLD 来执行您的意图。

Thislink has a really elaborate example .

这个链接有一个非常详细的例子。

HTH

HTH

回答by Alex Theedom

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

exam对象的实例必须在 EL 范围内,例如pageScope. 假设exam对象引用在 pageScope 中被称为“考试”,那么以下 EL 将调用该getHarfTo方法:

   ${exam.harfTo}

NOTE: you cannot call a setter method.

注意:您不能调用 setter 方法。

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

为了扩展上述内容(仅作为示例),我们可以exampageScopeusing scriptlet 中设置实例(请注意,不推荐使用 scriptlet,这只是一个用于说明的示例)

    <%
        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}

现在我们可以通过 EL 访问考试对象:${exam.harfTo}

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

可以将考试对象添加到其他范围,例如请求范围和会话范围。

Scriptlets and EL

小脚本和 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.

EL 不能直接访问在 scriptlet 中创建的变量。因此,城市变量不能在 JSTL 标签中使用。

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 pageScopeby the name of "cities".

这使得由城市变量引用的对象,在pageScope“城市”的名称中可用。

    <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.

现在可以访问城市变量。您不能将 scriptlet 作为 EL 表达式的属性。

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

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

或者,如果考试类符合 bean 约定,您可以使用标准操作

    <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:

不要忘记导入 JSTL 标签库:

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

EDIT:

编辑:

If you have a collection examswhich contains examobjects then you can use JSTL to iterate over then collection like so:

如果您有一个exams包含exam对象的集合,那么您可以使用 JSTL 来迭代集合,如下所示:

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