如何将特殊的 jsp scriptlet 更改为 jstl?

发布于 2024-10-07 09:49:45 字数 1943 浏览 2 评论 0原文

我想将 jsp 页面中的所有 scriptlet 更改为 jstl, 我如何将此代码更改为 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 ?
  <%}%>
 <%}%>

我的 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>

以及包含 Group 对象列表的 ArrayList 的另一个问题,在循环中我想获取每个 Group 对象并检查 Group 对象内的特定方法,我如何通过 jstl 访问这些方法??

我想更改此代码,

    <%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()%>"  />
       <%}%>
   <%}%>

这是我更改后的代码:

<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>

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>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

2024-10-14 09:49:45

第一部分

JSTL 和 EL 只适用于遵循 Java Bean 约定的方法。如果您真的想走这条路线,那么您可以绕着您的地图转一圈。

<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>

另一种方法可以是从地图中获取userName,并检查其是否为null,然后执行您喜欢的操作。这确实是一个更好的主意。

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

第二次

<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>

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>
与他有关 2024-10-14 09:49:45

至于否 1),您必须通过操作/控制器填充您的请求,并拥有一个循环访问您的地图的 JSTL 脚本,如下所示:

警告: 未经测试

<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 为您回答了第二个问题。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文