1

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?

Lenusska1414
  • 171
  • 4
  • 15
  • I'm trying to learn about jsf, so I'm just curious about your question, especially why the error message complains about namespaces. Perhaps it would be helpful if you could tell us what code snippet is at /WEB-INF/views/attendance.jsp(241,7). – Gyro Gearloose Nov 17 '15 at 20:17
  • It was this line : – Lenusska1414 Nov 17 '15 at 20:21
  • I don't really understand what's going on. ${months} should return am map, as a sequence of Map.Entry. Each ${month} should be a Map.Entry, a (key, value) pair from which you should be able to extract the value by ${month.value} (as the 3 answers given tell you). What I really don't understand is why your ${month.getValue()} does not work, and why the message about namespaces pops up. – Gyro Gearloose Nov 17 '15 at 20:32
  • just for completeness, could you please tell us the jsf-version you are using? – Gyro Gearloose Nov 17 '15 at 21:19

3 Answers3

2
<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>
Sas
  • 2,473
  • 6
  • 30
  • 47
  • Please help me, why, so, does ${month.getValue()} not work? – Gyro Gearloose Nov 17 '15 at 20:35
  • month is a map entry. Map doesn't have a method called getValue(). If you want to get the value of the map, the use `${month.value}` and this will return you, for example, the month "january" . – Sas Nov 17 '15 at 20:39
  • Map.Entry well does have a method getValue() (please check), for otherwise the ${month.value}, which calls the (bean-)method getValue(), wouldn't work. Or do I really have a sound misunderstanding of the concepts? – Gyro Gearloose Nov 17 '15 at 20:41
  • Map has a menthod `get` and accept key as a parameter, not getValue. See the [java doc](http://docs.oracle.com/javase/7/docs/api/java/util/HashMap.html) – Sas Nov 17 '15 at 20:44
  • Well ${months} is of type Map, but ${month} (without final "s") is of type Map.Entry and does have getValue() method. – Gyro Gearloose Nov 17 '15 at 20:47
  • Hmm my bad. Calling "${month.value}" is equivalent as ${${month.getValue()}. I would assume he was using older version of EL or servlet. Take a look [here](http://stackoverflow.com/questions/18961316/the-function-getmessagedata-must-be-used-with-a-prefix-when-a-default-namespace) – Sas Nov 17 '15 at 20:56
  • Yes, that could be the reason. I'm a novice on jsf and I started with 2.2.8 but heard rumors that EL-function calls were not possible in versions 2.1.x and earlier. Another thing: do you have any idea why the error message complained about namespaces? – Gyro Gearloose Nov 17 '15 at 21:02
  • Ouch, your link also hits my latest question. Hm, BallusC is sure an expert, but so great a specialist that he sees duplicates where normal users need at least an hour to see why and how those are duplicates. – Gyro Gearloose Nov 17 '15 at 21:05
  • 1
    Just for completeness, a snippet from a comment from @Palpatim , giving the crucial hint: "The error message suggests that the EL is interpreting test() as a function, which expects a namespace, like using the JSTL function fn:replace()" (taken from http://stackoverflow.com/questions/13017348/org-apache-jasper-jasperexception-the-function-test-must-be-used-with-a-prefix ) – Gyro Gearloose Nov 17 '15 at 21:16
1

You want this, as you are iterating and accessing Map

<c:forEach var="month" items="${months}">
     ${month.value}
</c:forEach>
jmj
  • 237,923
  • 42
  • 401
  • 438
1

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>
Abdelhak
  • 8,299
  • 4
  • 22
  • 36