Saturday, August 10, 2013

JSTL Tutorial


What is JSTL and when we need it ?
--------------------------------------

First we will see When we need it 
-----------------------------------

As per best practices,it is not good to write scriptlets... and expression tags.. 
in jsp page because.. HTML authoring guys doesn't know java code... it
will be difficult for them to maintain jsp pages. if it contains lot of java
code like scriptlet tags and expression tags.

JSTL contains simple tags like html tags.When these tags are encounterd
jsp container calls the code from java class.These tags are easy to 
understand... even by HTML authors.. who know little java

So, the alternative to using java code is expression language.
But expression language doesn't have the ability iterating,conditional tags(if/else)
etc...

So,we need to use JSTL.

Which jar files need to work with JSTL ?

We need to add 2 jar files.

a)standard.jar
b)jstl.jar

If even jstl tags also not sufficient to.. complete your work, you have to develop 
custom tags to meet your needs.

JSTL tags are dividied in to following:
---------------------------------------
  • Core Tags(catch,forEach,choose,if,test,import,out,otherWise,redirect etc...)
  • Formatting tags
  • SQL tags
  • XML tags
  • JSTL Functions


How JSTL works ?Let us give you explanation with simple for loop example

a)Add jstl.jar and standard.jar
b)Add taglib directive in jsp.As discussed in one of my post taglib is one of the page directives.
   (include,taglib and page directives).
   
   <%@taglib  uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>

  You have to specify the uri.To use core Tags, the uri is,http://java.sun.com/jsp/jstl/core.
  Prefix is just like an alias name for uri,generally all developers give prefix ="c" for core tags.
 You can even give prefix="coretags" etc..

If you give prefix="c" in jsp you have to write.In the following example I used 
<c:forEach because I specified prefix as "c" in taglib uri attribute's value
<c:forEach

If you give prefix="coretags"

<coretags:forEach


c)Suppose, if you want to iterate all the string objects set in the page scope. 
   You can take help of  forEach tag in core tags.forEach is one out of many tags available
   in Core tags.

d)For forEach you have to give value for items attribute and var.items attribute specify the
   name of the collection available in some scope.

  Suppose if there are 3 friends in page scope in friends collection
  var will be assigned first friend,second friend and third friend respectively in each iteration.

Here is the complete example:
--------------------------------

<%-- 
    Document   : jstlHW
    Created on : Aug 10, 2013, 1:52:56 PM
    Author     : Rishitha
--%>

<%@page import="java.util.List"%>
<%@page import="java.util.ArrayList"%>
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<%@taglib  uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<!DOCTYPE html>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>JSP Page</title>
    </head>
    <body>
        <%
            List friends = new ArrayList();
            friends.add("Anil");
            friends.add("Kumar");
            friends.add("Rishitha");
            
            pageContext.setAttribute("friends",friends);
            
      %>
        <c:forEach items="${friends}" var="fr">
            <c:out value="${fr}"/>
            
        </c:forEach>
        
        
    </body>

</html>


  



















1 comment: