开发者

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 quest开发者_JAVA技巧ion 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.


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>


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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜