palAlaa
palAlaa

Reputation: 9858

concepts about JSTL

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.

Upvotes: 0

Views: 1384

Answers (2)

gbakernet
gbakernet

Reputation: 336

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>

Upvotes: 3

Enrique
Enrique

Reputation: 10117

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

Upvotes: 1

Related Questions