RSS feed
All(28) Eclipse(4) Java(5) JDBC(5) JSP(3) Maven(7) Personal (1) Spring(3) Tomcat(5)
<< A little about Rob Sinner | Home | Simplified Maven Setup Step 1 >>

Helpful JSTL Examples

These are JSTL JSP Examples I use all the time and I use this as a reference

How to get size/length of collection   use fn:length like so

<c:if test="${fn:length(results) == 1}">

You have one result

</c:if>

 

Links

<html:link page="/tni/telnoLookup.do" >

Telephone Number Inventory

</html:link>

 

Similar to Struts <html:link page=”” />  using c:url                                                                     

 

<c:url var="detailUrl" value="/${module}/lookupOptions.do">

<c:param name="dispatch" value="phdetailBySubno"/>

<c:param name="subno" value="${resultsVal.subno}"/>

<c:param name="main" value="${resultsVal.main}" />

</c:url>

 

<a href="<c:out value="${detailUrl}" />">

<c:out value="${name}"/>

</a>

                                                                       

                                                                                                                       

Format a telephone number

 

If the telno is still an unformatted string object as in the case of the result list of dtos

 

<c:out value="${fn:substring(resultsVal.telno, 0, 3)}-${fn:substring(resultsVal.telno, 3, 6)}-${fn:substring(resultsVal.telno, 6, 10)}" />

                       

If loading from form bean use Utility.maskTn()

 

<c:out value="${formBean.telno}"  />

                                         

 

Format Date

<bean:write name="resultsVal" property="efdte1" formatKey="display.date.format"/> 

 

Or using JSTL

<fmt:formatDate value="${resultsVal.efdte1}" pattern="MM/dd/yyyy" />

 

Both produce format

10/10/2002

 

Format Time

 

If the time is still a date object as in the case of the result list of dtos

<bean:write name="resultsVal" property="time" formatKey="display.time.format"/>

 

 

Trying to go from one string in time formatted to another

 

<fmt:parseDate value="${formBean.enttme}" var="enttme" pattern="hh:mm:ss a" />

<fmt:formatDate type="time" pattern="HH:mm:ss" value="${enttme}"  />

 

If already parsed date into a String by Utility.timeToString, as in the case of a formBean, its already in the correct format

<c:out value="${formBean.enttme}"  />

 

 

Dropdown                                       

<html:select property="rsvflg" styleId="rsvflg" size="1" styleClass="stdtext" >

<html:option value=""></html:option>

<html:option value="R">R - Reserved</html:option>

</html:select>

 

Or with a collection or array of beans

 

<html:select property="frmcic" styleId="frmcic" size="1" styleClass="stdtext" onchange="tabNext(this);">

<html:option value=""></html:option>

<html:optionsCollection property="cicmap" value="key" label="value" />

</html:select>

 

Dropdown with multiple options (from ap/SummaryCheckReport)

Note the companyArray is a string array

<html:select name="apForm" property="companyArray" size="5"

styleClass="stdtext" styleId="compBox" multiple="true" title="this is a title">

<c:if test="${ ! empty apForm.companyList}">

<html:optionsCollection name="apForm" property="companyList"

value="company" label="name" />

</c:if>

</html:select> 

Dropdown with more complex label than can be done with optionsCollection

<div class="input" style="">

Fuel Location Type<br />

<html:select name="fuelForm" property="fuelLocTypeRecId" styleId="fuelLocTypeRecId" styleClass="stdtext" title="FUEL_LOC_TYPE_REC_ID">

       <%--<html:optionsCollection name="fuelForm" property="fuelLocTypeList" value="recId" label="descr" />--%>

       <c:forEach items="${fuelForm.fuelLocTypeList}" var="item" varStatus="status">

              <html:option value="${item.recId}">${item.idx}-${item.descr}</html:option>

       </c:forEach>

</html:select>

</div>

 

Or with an array of String[] only and not an array of beans                                                     

<html:select name="invForm" property="inPartNd" size="1" styleClass="stdtextLeft75" >

      <html:options name="invForm" property="partFamilyArray"/>

</html:select>   

To print a stack trace out to your JSP page, you need to wrap the implicit object 'out' in a printWriter and pass the object to the printStackTrace method in Exception. ie:

<% // JSP scriptlet

try{   // An Exception is throw

%>

Your JSP Body

<%

}catch(Exception e){
   e.printStacktrace(new java.io.PrintWriter(out));
}

%>

Access request parameter

<c:out value=”param.nameOfParameter” />

Access request attribute

<c:out value="${requestScope['javax.servlet.forward.servlet_path']}"/>

 

Example

<c:set var="requestUri" value="${requestScope['javax.servlet.forward.servlet_path']}?includeJsp=${invForm.includeJsp}"/>

You want to access something in the HttpServletRequest implicit request object that is not a parameter or an attribute but has a getter method

<c:out value = "${pageContext.request.queryString}" />

<c:out value = "${pageContext.request.requestURI}" />

 

Iterate Over List(Collection)

<c:forEach var="resultsVal" items="${results}" varStatus="status">

 

<tr class="${((status.count % 2) == 0) ? 'resultseven' : 'resultsodd'}">

 

<td nowrap='true' width='1%' height='1%'>

<a href='#' class='results'>

<c:out value="${resultsVal.property1}"/>

</a>

</td>

 

<td width='100%' height='1%'>

<c:out value="${resultsVal.property2}"/>

</td>

</tr>

 

</c:forEach>

Iterate Over Hashmap

Say the hashmap is stored in requestScope

 

<c:forEach var="resultsVal" items="${results}" varStatus="status">

 

<tr class="${((status.count % 2) == 0) ? 'resultseven' : 'resultsodd'}">

 

<td nowrap='true' width='1%' height='1%'>

<a href='#' class='results'>

<c:out value="${resultsVal.key}"/>

</a>

</td>

 

<td width='100%' height='1%'>

<c:out value="${resultsVal.value}"/>

</td>

</tr>

</c:forEach>

 

Struts Form Variable with LinkedHashMap

 

<tr>

<td class="labelBlack" height="1%" width="100px">Removal code</td>

<td height="1%">

<html:select property="reportRemovalCode" styleClass="stdtext">

<html:option value="ALL">ALL</html:option>

<html:optionsCollection property="mxrotremMap" value="key" label="value"/>

</html:select>

</td>

</tr>

 

Date Input

Must include calendar.jsp in pageheader.jsp

<jsp:include page="/include/calendar.jsp" />

 

 

<html:text property="dispatchCmtdte" styleId="dispatchCmtdte" styleClass="stdtext10char" maxlength="10" onblur="needToConfirm=false;ckDate(this);" title="Commit Date"/>

&nbsp;

<html:img page="/images/btn_calendar.png" alt="Date Selector"

styleId="AR_anchorDispatchCmtdte" title="Date Selector"/>

 

<script type="text/javascript">

Calendar.setup({

inputField     :    "dispatchCmtdte",     // id of the input field

button         :    "AR_anchorDispatchCmtdte"  // trigger for the calendar (button ID)

});

</script>

Time Input (in military format from 0000 – 2359, similar to legacy) right now using the ckTime function from the php library and this is the format its expecting if we need to change this let’s discuss

<html:text property="dispatchCmttme" styleId="dispatchCmttme" styleClass="stdtext" onblur="ckTime(this,'24');" title="Commit Time"/>

Printout request values and such in a jsp

<c:import url="/debug.jsp" />

 

debug.jsp

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

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

 

<%@ page import="com.cms.common.action.RequestUtil" %>

 

<c:if test="${fn:contains(pageContext.request.serverName, 'localhost')}">

<a class="hdrText" style="font-size: 12px;" onclick="toggle('debug');"

href="#" title="View Debug Information">

View Debug Information -- only visible on localhost

</a>

<div id="debug" class="labelBlack" style="width:95%;display:none;">

<%=RequestUtil.getSummary(request)%>

</div>

</c:if>   

 

Include a file

 

--included inline

<%@ include file="/transactions/displayTagTransactionDetailLink.jsp" %>

 

<c:import url="/transactions/displayTagTransactionDetailLink.jsp" />

 

--as its own  servlet

<jsp:include page="/transactions/loan.jsp" />

 

Setting up selected menus based on Request Url (Servlet Path)

 

<table border="0" width='100%' height='1%' style="width:150px;margin: 0px 0px 0px 0px;" cellpadding='0px' cellspacing='0px'>

<tr>

            <c:choose>

                  <c:when test="${fn:contains(requestScope['javax.servlet.forward.servlet_path'], 'createCountSheets')}">

                        <c:set var="createCountSheetsClass" value="menuSelected" />

                  </c:when>

                  <c:otherwise>

                        <c:set var="createCountSheetsClass" value="menuNotSelected" />

                  </c:otherwise>

            </c:choose>

                 

            <td class="${createCountSheetsClass}" id='createCountSheetsMenu' style="width:150px;" onclick="window.location='createCountSheets.do';" >

            Create Count Sheets

            </td>

      </tr>

</table>

Print out an  exception stack trace in a jsp –good if you aren’t seeing a stack trace for some reason from your jsp body
Tags :



Add a comment Send a TrackBack