Friday, August 6, 2010

Conditional Statements <c:if > , <c:choose > , <c:when > , <c:otherwise > in JSTL for a JSP Page

Any non-JSP code in a JSP file is called template text and is
automatically written to the output stream. The JSTL tags <c:if> and <c:choose> make it possible to dynamically generate template text depending on a condition.

<c:if >
The <c:if> tag generates its body if the expression in the test attribute evaluates to the boolean value true or the string value"true">:

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

<%-- Simple if conditions --%>
<c:if test='${param.p == "someValue"}'>
    Generate this template text if p equals someValue
</c:if>

<c:if test='${param.p}'>

    Generate this template text if p equals "true"
</c:if>

<c:choose > , <c:when > and, <c:otherwise >
This tags are used to write code in the form of if/else , if / elseif / else ...
Check the bellow example for how we can achieve if/else code using choose / when / otherwise in JSTL
<%-- A simple if/else condition --%>
<c:choose>
    <c:when test='${param.p == "someValue"}'>
        Generate this template text if p equals someValue
    </c:when>
    <c:otherwise>
        Otherwise generate this template text
    </c:otherwise>

</c:choose>


If you want to use nested if / elseif / else ... condition then check the following sample code

<c:choose>
    <c:when test='${param.p == "0"}'>
        Generate this template text if p equals 0
    </c:when>
    <c:when test='${param.p == "1"}'>
        Generate this template text if p equals 1
    </c:when>

    <c:otherwise>
       Generate this template text if p equals anything else
    </c:otherwise>
</c:choose>


The <c:out> can also be used to conditionally generate template text. If the value attribute is null, a default value (if specified) is generated. The default value can be specified using the default attribute or can be specified in the body content. This example demonstrates both methods:
<%-- Default value in an attribute --%>
<%-- Default value in the body content --%>
<c:out value='${param.p}'>
    Generate this if p is null
</c:out>

JSTL Guide , Tutorial , Startup or First Program

There are two versions of the JSTL : one that enables the JSTL expression language support and one that doesn't. JSTL expression language support replaces the expression JSP tag with a more convenient syntax. For example, the JSP expression:
<c_rt:if test='<%= request.getParameter("p") != null %>'>
    <%= request.getParameter("p") %>
</c_rt:if>

can be written as
<c:if test="${param.p != null}">
    <c:out value="${param.p}" />
</c:if>

with expression language support enabled. More detailed information about the expression language is available at http://www.jcp.org/aboutJava/communityprocess/first/jsr052/index.html.

As mentioned in Using the Java Standard Tag Library (JSTL) in a JSPPage, there are four tag libraries that make up the JSTL. There are two versions of each of these libraries. That example declares the use of the four tag libraries that do not support the expression language. Here is an example that declares the use of the four tag libraries that do support the expression language:
<%-- Core with EL --%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>

<%-- I18N Formatting with EL --%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/fmt" prefix="fmt" %>

<%-- SQL with EL --%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/sql" prefix="sql" %>

<%-- XML with EL --%>

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

<%-- JSTL functions with EL --%>

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

JSTL Sample Code / Examples / Tutorials for c:out and c:set

In this article we will see how JSTL TAG c:set and c:out can be used inside JSP page.

When a JSP page needs to store data as a variable for its processing, it must specify a location, called the scope. See how JSP Page store data in variables in the four available scopes.

Data is saved using a mechanism called scoped variables. A scoped variable has a name, which is of type String and a value, which is of type Object. For non-page scoped variables, it is recommended that the name use the reverse domain name convention (e.g., prefixed with com_myprefix) to minimize unexpected collisions when integrating with third party modules.


When using the JSTL's expression language, the variables in each
scope are made available in the implicit objects pageScope, requestScope, sessionScope, and applicationScope


This example saves and retrieves values in scoped variables in each of the four scopes:


<%-- Declare the core library --%>
<%@ taglib uri="/WEB-INF/tld/c.tld" prefix="c" %>

<%-- Save data in scoped variables --%>

<c:set var="name1" value="testvalue1" scope="page" />
<c:set var="com_myprefix_name2" value="tesvalue2" scope="request" />
<c:set var="com_myprefix_name3" value="tesvalue3" scope="session" />
<c:set var="com_myprefix_name4" value="tesvalue4" scope="application" />

<%-- Show the saved values --%>
<c:out value='${pageScope.name1}' />
<c:out value='${requestScope.com_myprefix_name2}' />
<c:out value='${sessionScope.com_myprefix_name3}' />

<c:out value='${applicationScope.com_myprefix_name4}' />

When retrieving a saved value, it is possible to omit the scope. If the scope is omitted, the variable name is automatically searched for in each of the scopes, in the order pageScope, requestScope, sessionScope, and applicationScope.



<%-- Show the saved values without a specific scope --%>

<c:out value='${name1}' />
<c:out value='${com_myprefix_name2}' />
<c:out value='${com_myprefix_name3}' />
<c:out value='${com_myprefix_name4}' />

It is also possible to specify the value to save using the contents
of the body, rather than through the value attribute:


<%-- Save data using body content --%>
<c:set var="name1" scope="page">
tesvalue 1 in body
</c:set>
<c:set var="com_myprefix_name2" scope="request" >
tesvalue 2 in body
</c:set>
<c:set var="com_myprefix_name3" scope="session" >
tesvalue 3 in body

</c:set>
<c:set var="com_myprefix_name4" scope="application">
tesvalue 4 in body
</c:set>


When specifying the value using body contents, the body contents is first trimmed of leading and trailing white space before it is saved. For example,

<c:set var="name1" scope="page">
line 1
line 2

</c:set>
would be saved as:

"line 1\n    line 2"

Tuesday, August 3, 2010

Welcome

Welcome to JSP TAG Libraries Help and Tutorials. Wait till I post more pages over here.