繁体   English   中英

使用getter和setter的Java JSP

[英]Java JSP using getters and setters

我正在构建Java应用程序,并且我有.jsp文件,其中有以下代码:

        <c:forEach items="${months}" var="month">
                    <c:choose>
                        <c:when test="${month.getValue() == currentMonth}">
                            <option value="${month.getValue()}" selected>${month.getValue()}</option>
                        </c:when>
                        <c:otherwise>
                            <option value="${month.getValue()}">${month.getValue()}</option>
                        </c:otherwise>
                    </c:choose>
                </c:forEach>

像这样将月份从控制器添加到jsp的位置:

model.addAttribute("months", getMonths());

这是一种被称为的方法:

        private Map<Integer, String> getMonths() {
    Map<Integer, String> months = new LinkedHashMap<Integer, String>();
    months.put(1, "January");
    months.put(2, "February");
    months.put(3, "March");
    months.put(4, "April");
    months.put(5, "May");
    months.put(6, "June");
    months.put(7, "July");
    months.put(8, "August");
    months.put(9, "September");
    months.put(10, "October");
    months.put(11, "November");
    months.put(12, "December");
    return months;
}

我有一个问题,当我在服务器上运行它时,我使用的是Tomcat 6.0.2,出现以下错误:

org.apache.jasper.JasperException: /WEB-INF/views/attendance.jsp(241,7) The function getValue must be used with a prefix when a default namespace is not specified

您能告诉我如何修复它,使其正常工作吗?

编辑

对此还有一个问题。 在.jsp中,我有这行:

<td>${currentUser.setValue(d.key) } ${d.value }</td>    

而且我收到类似的错误:

The function setValue must be used with a prefix when a default namespace is not specified. 

方法setValue来自我的User.java类。 是否可以通过某种方式直接从.jsp文件设置值?

<c:forEach items="${months}" var="month">

可变month是地图条目。 如果您想使用它,请使用:

${month.key} // will returns: 1
${month.value} // will returns: January

您的JSTL应该如下所示:

<c:forEach items="${months}" var="month">
  <c:choose>
    <c:when test="${month.value == currentMonth}">
        <option value="${month.value}" selected>${month.value}</option>
    </c:when>
    <c:otherwise>
        <option value="${month.value}">${month.value}</option>
    </c:otherwise>
  </c:choose>
</c:forEach>

在迭代和访问地图时需要此

<c:forEach var="month" items="${months}">
     ${month.value}
</c:forEach>

当您用于遍历Map时,迭代中的每个项目都是Map.Entry的实例。

<c:forEach items="${months}" var="month"">
  Key is ${month.key}
  Value is ${month.value}
</c:forEach>

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM