开发者

how can I change special jsp scriptlet to jstl?

开发者 https://www.devze.com 2023-01-29 21:37 出处:网络
i want to change all scriptlets in my jsp pages to jstl, how can i change this code to jstl <%Map validationResults = (HashMap) request.getAttribute(\"validationResults\");%>

i want to change all scriptlets in my jsp pages to jstl, how can i change this code to jstl

    <%     Map validationResults = (HashMap) request.getAttribute("validationResults");%> 
    <% if (validationResults != null) {
           if (validationResults.containsKey("userName")) {  //how can i chage this line to jstl ?
    %>
    <%=((ValidationResult) (validationResults.get("userName"))).getDetails()%> //how can i chage this line to jstl too ?
  <%}%>
 <%}%>

MY JSTL

<c:set var="validationResults" value="validationResults" scope="request"></c:set>
   <c:if test="validationResults != null">   
  //how can i change the code of map here? 
   </c:if>

and another problem with ArrayList which contains list of Group object , in the loop i want to get each Group object and check a specific method inside Group object , how can I reach to these method through jstl??

I want to change this开发者_如何学运维 code

    <%List<Group> allGroupList = new ArrayList<Group>();
        allGroupList = (ArrayList) request.getAttribute("groups");%>

       <% for (int index = 0; index < allGroupList.size(); index++) {%>
       <%Group aGroup = (Group) allGroupList.get(index);%>
        <label ><%=aGroup.getGroupEName()%></label>
        <%if (aGroup.isIsUserGroup()) {%>
        <input type="checkbox" name="group" value="<%=aGroup.getGroupNo()%>" CHECKED />
         <%} else {%>
         <input type="checkbox" name="group" value="<%=aGroup.getGroupNo()%>"  />
       <%}%>
   <%}%>

here's my changed code:

<jsp:useBean id="GroupBean" class="ps.iugaza.onlineinfosys.entities.Group" type="ps.iugaza.onlineinfosys.entities.Group" scope="reqeust">
<c:set var="allGroupList" value="groups" scope="request"></c:set>
<c:forEach var="grp" items="${allGroupList}" varStatus="status">
        //?????? what should i do ?
 </c:forEach>


For The First Part

JSTL and EL only work with method that follows Java Bean convention. If you really wanna go this route, then you can loop around your map.

<c:forEach items="${requestScope.validationResults}" var="mapEntry" varStatus="index">
    <c:if test="${mapEntry.key == 'userName'}">
        <tr>
            <td>${mapEntry.value.details}</td>
        </tr>
    </c:if>
</c:forEach>

The other way can be just get userName from the map, and check for if its null or not, and then do whatever you like. This is indeed a better idea.

<c:if test="${requestScope.validationResults['userName'] != null}">
    <tr>
        <td>${requestScope.validationResults['userName'].details}</td>
    </tr>
</c:if>

For The Second

<c:forEach var="grp" items="${requestScope.groups}" varStatus="status">
    <label>${grp.groupEName}</label>
    <input type="checkbox" name="group" value="${grp.groupNo}" ${grp.isUserGroup ? 'checked' : ''} />
</c:forEach>


As for no 1), You would have to populate your request through an action/controller, and have a JSTL script that iterates through your map as follows:

Warning: Untested

<c:if test="${requestScope.validationResults != null}">
    <c:forEach var="entry" items="${requestScope.validationResults}">
        <c:if test="${entry.key == 'userName'}">
            Result: ${entry.value.details};
        </c:if>
    </c:forEach>
</c:if>

Adeel Ansari answered number 2 for you.

0

精彩评论

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