Lenusska1414
Lenusska1414

Reputation: 171

Java JSP using getters and setters

I'm building java application and I have .jsp file, where I have this code:

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

Where the months is added to jsp from controller like this:

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

Here is a method from which is it called:

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

I have a problem,when I run it on server, I'm using Tomcat 6.0.2 and I'm getting a following error:

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

Could you tell me how to fix it, so it works properly?

EDIT

One more question to this. in .jsp I have this line:

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

and I'm getting similar error:

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

Method setValue is from my User.java class . Is it possible somehow to set value directly from .jsp file?

Upvotes: 1

Views: 3525

Answers (3)

Sas
Sas

Reputation: 2503

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

Variable month is a map entry. if you wanna use it, use:

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

Your JSTL should look like:

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

Upvotes: 2

Abdelhak
Abdelhak

Reputation: 8387

When you use to iterate over a Map, each item in the iteration is an instance of Map.Entry.

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

Upvotes: 1

Jigar Joshi
Jigar Joshi

Reputation: 240928

You want this, as you are iterating and accessing Map

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

Upvotes: 1

Related Questions