如何编写 jstl 而不是 scriptlet?

发布于 2024-11-03 17:30:16 字数 508 浏览 0 评论 0原文

<%
    UserDetailsVO objUserDetailsVO = null;
   ArrayList arlUserDetailsVO = (ArrayList)request.getAttribute("LSTUSERSDETAILS");
   String nonBifFlag = "";
   if(arlUserDetailsVO !=null){
   Iterator it = arlUserDetailsVO.iterator();
   String urlProfile="";
   while(it.hasNext()){
       objUserDetailsVO = (UserDetailsVO)it.next();
        urlProfile = "UserProfile.htm?userID="+objUserDetailsVO.getLogin_Ident()+"&internalID=111"+objUserDetailsVO.getInternalId();

 %>
<%
    UserDetailsVO objUserDetailsVO = null;
   ArrayList arlUserDetailsVO = (ArrayList)request.getAttribute("LSTUSERSDETAILS");
   String nonBifFlag = "";
   if(arlUserDetailsVO !=null){
   Iterator it = arlUserDetailsVO.iterator();
   String urlProfile="";
   while(it.hasNext()){
       objUserDetailsVO = (UserDetailsVO)it.next();
        urlProfile = "UserProfile.htm?userID="+objUserDetailsVO.getLogin_Ident()+"&internalID=111"+objUserDetailsVO.getInternalId();

 %>

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

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

发布评论

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

评论(2

我不在是我 2024-11-10 17:30:16

确保您拥有 JSTL 位于您的类路径中(即您的 Web 项目的 WEB-INF/lib 文件夹)。您将需要在 jsp 页面的顶部:

<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>

代码的字面翻译如下所示:

<c:set var="objUserDetailsVO" value="${null}"/>
<c:set var="arlUserDetailsVO" value="${requestScope['LSTUSERSDETAILS']}"/>
<c:set var="nonBifFlag" value=""/>
<c:if test="${not empty arlUserDetailsVO}">
    <c:set var="urlProfile" value="${null}"/>
    <c:forEach var="objUserDetailsVO" items="${arlUserDetailsVO}">
        <c:url var="urlProfile" value="UserProfile.htm">
            <c:param name="userID" value="${objUserDetailsVO.login_Ident}"/>
            <c:param name="internalID" value="111${objUserDetailsVO.internalId}"/>
        </c:url>
    </c:forEach>
</c:if>

尽管如此,考虑到您实际上并不需要将页面/请求属性设置为 null 因为它们是已经 null,您可能可以将其简化为:

<c:set var="arlUserDetailsVO" value="${requestScope['LSTUSERSDETAILS']}"/>
<c:if test="${not empty arlUserDetailsVO}">
    <c:forEach var="objUserDetailsVO" items="${arlUserDetailsVO}">
        <c:url var="urlProfile" value="UserProfile.htm">
            <c:param name="userID" value="${objUserDetailsVO.login_Ident}"/>
            <c:param name="internalID" value="111${objUserDetailsVO.internalId}"/>
        </c:url>
    </c:forEach>
</c:if>

请注意,通过使用 构造您的 url,您的 url 参数现在将被正确编码,因为它们没有被编码在您的 java 代码片段中。

Make sure you have jstl.jar and standard.jar from the JSTL are in your classpath (ie. your web project's WEB-INF/lib folder). A the top of your jsp page you will need:

<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>

A literal translation of your code would look like this:

<c:set var="objUserDetailsVO" value="${null}"/>
<c:set var="arlUserDetailsVO" value="${requestScope['LSTUSERSDETAILS']}"/>
<c:set var="nonBifFlag" value=""/>
<c:if test="${not empty arlUserDetailsVO}">
    <c:set var="urlProfile" value="${null}"/>
    <c:forEach var="objUserDetailsVO" items="${arlUserDetailsVO}">
        <c:url var="urlProfile" value="UserProfile.htm">
            <c:param name="userID" value="${objUserDetailsVO.login_Ident}"/>
            <c:param name="internalID" value="111${objUserDetailsVO.internalId}"/>
        </c:url>
    </c:forEach>
</c:if>

Although, considering tha you don't really need to set page/request attributes to null since they are already null, you could probably pare it down to this:

<c:set var="arlUserDetailsVO" value="${requestScope['LSTUSERSDETAILS']}"/>
<c:if test="${not empty arlUserDetailsVO}">
    <c:forEach var="objUserDetailsVO" items="${arlUserDetailsVO}">
        <c:url var="urlProfile" value="UserProfile.htm">
            <c:param name="userID" value="${objUserDetailsVO.login_Ident}"/>
            <c:param name="internalID" value="111${objUserDetailsVO.internalId}"/>
        </c:url>
    </c:forEach>
</c:if>

Note that by using <c:url> to construct your url, your url params will now be correctly encoded, where as they are not being encoded in your java code snippet.

巷子口的你 2024-11-10 17:30:16

尝试核心标签以及请求的绑定等,例如 ...

这是一个简短教程(或者如果可以的话阅读德语:德语教程/参考)。

Try the core tags as well as bindings for the request etc., e.g. <c:forEach>, <c:out>, <c:if> ...

Here's a short tutorial (or if you can read German: a German tutorial/reference).

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