开发者

How to write jstl instead of scriptlets?

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

 %>


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.


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

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜