0

I want to understrand what happens when i use JSTL to access maps,in Hidden features of JSP/Servlet in @blausC's answer, he explained what happend, but when i try to use the following code

     <c:set var="resultMap" value="validationResults" scope="request"></c:set>
     <c:if test="${resultMap['userName'] != null}">
          ${resultMap['userName'].details}
    </c:if>

a confused exception happend

Caused by: javax.el.PropertyNotFoundException: Property 'userName' not found on type java.lang.String

The key of the map should be string, so whay is this exception, i tried the examples in the above question and the same exception,Can some one tell me where I have misunderstanding?

Edit: I populate the map in the servlet and send it to jsp

 Map<String, ValidationResult> result = new HashMap<String, ValidationResult>();
 aValidationResult = new ValidationResult();
 check whether the field is valid or not if not fill the map
 result.put("userName", aValidationResult);
 result.put("group", aValidationResult);

if map is not empty, return the map to jsp

request.setAttribute("validationResults", result);

the map is filled when i make server side validation ,

Thanx in advance.

Community
  • 1
  • 1
palAlaa
  • 9,500
  • 33
  • 107
  • 166

2 Answers2

3

resultMap is a String because of this line

 <c:set var="resultMap" value="validationResults" scope="request"></c:set>

You need to use EL to assign the value

 <c:set var="resultMap" value="${validationResults}" scope="request"></c:set> 

Edit: The following is working code

 <c:set var="validationResults" value="<%= new java.util.HashMap() %>" />
 <c:set target="${validationResults}" property="username" value="Hello World" />
 <c:set var="resultMap" value="${validationResults}" />
 <c:out value="${resultMap['username']}"></c:out>
gbakernet
  • 326
  • 2
  • 5
  • it doesn't work when i make the value like this value="${validationResults}" ,it works ad @Enriquw answered,but i didn't understand why? – palAlaa Dec 17 '10 at 04:31
1

This is caused because String class does not have a method called getUserName()

Enrique
  • 9,920
  • 7
  • 47
  • 59