Search code examples
javajspweb-applicationsjstlscriptlet

JSTL equivalents to jsp tags


I have a search application.

The result of my search is all set as properties in transfer object.

In my processor java class, I am putting the TO in context as:

ctx.putUserData("searchDetailsTO", searchDetailsTO);

Along with above object, several strings are also set as below:

    ctx.putUserData("productName", productName);
    ctx.putUserData("productNameCriteria", productNameCriteria);
    ctx.putUserData("currency", currency);

In my jsp, I am accessing the TO as:

<jsp:useBean id="searchDetailsTO" scope="session" type="com.domain.SearchDetailsTO" />

and accessing the strings as:

<%
String  productName =(String)session.getAttribute("productName");
String  productNameCriteria =(String)session.getAttribute("productNameCriteria");
String  currency =(String)session.getAttribute("currency");
int searchResultsSize = searchDetailsTO.getTotalResults();
SomeTO someTO = new SomeTO();
%>

And later on in the jsp, using above TO and Strings as:

<%
if (productNameCriteria .equals("NAME_BEGINS")) { 
%> 
  <input type="radio" name="productNameCriteria" id="productNameCriteria" value="NAME_BEGINS" CHECKED/><b>Begins With</b>
  <input type="radio" name="productNameCriteria" id="productNameCriteria" value="NAME_CONTAINS" /><b>Contains</b>
  <input type="radio" name="productNameCriteria" id="productNameCriteria" value="NAME_IS" /><b>Exact Match</b>
<% 
} else if (productNameCriteria .equals("NAME_CONTAINS")) { 
%> 
  <input type="radio" name="productNameCriteria" id="productNameCriteria" value="NAME_BEGINS"/><b>Begins With</b>
  <input type="radio" name="productNameCriteria" id="productNameCriteria" value="NAME_CONTAINS" CHECKED/><b>Contains</b>
  <input type="radio" name="productNameCriteria" id="productNameCriteria" value="NAME_IS" /><b>Exact Match</b>                      
<% 
}
else { 
%>  
  <input type="radio" name="productNameCriteria" id="productNameCriteria" value="NAME_BEGINS"/><b>Begins With</b>
  <input type="radio" name="productNameCriteria" id="productNameCriteria" value="NAME_CONTAINS"/><b>Contains</b>
  <input type="radio" name="productNameCriteria" id="productNameCriteria" value="NAME_IS" CHECKED/><b>Exact Match</b>
<% 
}
%>  

And

<%
if (searchResultsSize > 0) {
for (int k = 0; k < searchDetailsTO.getResult().size(); k++) {
  someTO = (someTO) searchDetailsTO.getResult().get(k);
%>
<Table>
<TR>
<TD class="gridrow<%=k%2%>" align="center" style="style" width="120">
     <%=someTO.getSomething()%>
</TD>   
<TD class="gridrow<%=k%2%>" align="left" style="style" width="372">
  <A href="#"onclick="loadDetails('<%=someTO.getSomeId()%>', '<%=someTO.getSomethingMore()%>')"; ><%=someTO.getSomethingElse()%></A>
</TD>  
</TR>
</Table>
<%
} }
%>

etc. etc.

My javascript function also uses value from TO. Something like:

function someFunc(){
       if(option=='goto' && !(pageNum>0 && pageNum<=<%=searchDetailsTO.getTotalPages()%>))
       {
  alert("Please enter valid Page# (Range 1 to <%=searchDetailsTO.getTotalPages()%>)");
       }

I want to switch over to using JSTL and EL and avoid using scriptlets as above. However, EL and JSTL is kind of greek to me.

(1) What will be the jstl equivalents to above code blocks? Do I need to do some major changes to my backend to achieve the same using JSTL/EL ??

(2) I have some javascript functions in the jsp. Will they be affected by JSTL? For e.g. loadDetails function in above code and someFunc function above which uses the TO in it.

(3) How to call TO's flag functions in jstl/el

<%
if(searchDetailsTO.isPrevious() || searchDetailsTO.isNext()) {      
%>  

using

c:if test="${(searchDetailsTO.isPrevious()) or (searchDetailsTO.isNext())}"   

is not working

I tried using

<c:when> 

on the if else code for radio buttons above, however, I started getting 3 rows of 3 buttons (9 buttons).

Thanks for reading!!


Solution

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

    • For if..else use c:choose and c:when / c:otherwise.
    • For for use c:forEach.
    • To declare variable use c:set
    • To get data from session use ${pageContext.session.varName}. (replace varName with name of session data you want to get)
    • To get size in loop use fn:length(dataList). (to use this you need to import functions taglib <%@taglib uri="http://java.sun.com/jsp/jstl/functions" prefix="fn" %>)

    To use jstl you need to add 2 jars in your project that are jstl.jar and standard.jar


    JSTL will not affect java script behavior.