使用getter和setter的Java JSP

时间:2015-11-17 20:03:53

标签: java jsp

我正在构建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文件设置值吗?

3 个答案:

答案 0 :(得分:2)

class CenteredThumbSlider : UISlider {
  override func thumbRectForBounds(bounds: CGRect, trackRect rect: CGRect, value: Float) -> CGRect
  {
    let unadjustedThumbrect = super.thumbRectForBounds(bounds, trackRect: rect, value: value)
    let thumbOffsetToApplyOnEachSide:CGFloat = unadjustedThumbrect.size.width / 2.0
    let minOffsetToAdd = -thumbOffsetToApplyOnEachSide
    let maxOffsetToAdd = thumbOffsetToApplyOnEachSide
    let offsetForValue = minOffsetToAdd + (maxOffsetToAdd - minOffsetToAdd) * CGFloat(value / (self.maximumValue - self.minimumValue))
    var origin = unadjustedThumbrect.origin
    origin.x += offsetForValue
    return CGRect(origin: origin, size: unadjustedThumbrect.size)
  }
}

变量<c:forEach items="${months}" var="month"> 是一个地图条目。如果你想使用它,请使用:

month

您的JSTL应如下所示:

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

答案 1 :(得分:1)

您想要这样,因为您正在迭代并访问Map

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

答案 2 :(得分:1)

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

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