Survey
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
JSTL What is JSTL • JSTL is an acronym for Java Standard Tag Library • JSTL contain tags that developer can use in JSP pages Tags in JSTL • What tags are provided by JSTL – Core tags – Xml tags – Sql tags – Formatting tags – Function tags We will only do Core tags. For other tags you need to know Servlets and XML. Core Tags • The following tags are part of Core Tag <c:set <c:out <c:if test= “ <c:choose , <c:when , <c:otherwise <c:forEach <c:forTokens <c:import <c:url <c:redirect <c:param How to use JSTL in JSP • To use JSTL in JSP, you need to do some configuration in JSP Step1: download jstl.jar and standard.jar Step2: copy both the files in WEB-INF directory Step3: Write jsp file that can use core tags. Writing JSP file • Make a HTML file jstl_ex.html and store it in the directory \webapps\myJSPApps\proj1\ • • • • • • • • <HTML> <BODY> <form method="post" action="greeting.jsp"> <input type="text" name="text1"> <input type="submit"> </form> </BODY> </HTML> Writing JSP file • • • <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %> <c:set var="s" value="${param.text1}" /> welcome <c:out value="${s}" /> Save the above file as greeting.jsp in webapps\MyJSPApps\proj1\ 1. You can change prefix = “d”, in that case you have to use <d:set> or <d:out> and so on. 2. The variable text1 comes from HTML file. Explanation <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %> The line above means, include jstl library in jsp. <c:set var="s" value="${param.text1}" /> This line means set the value of variable s to the value of variable text1 from HTML file. <c:out value="${s}" /> The line means, print the value of variable s. Using <c:if> • • • • • • • • • • • • Write the following code in HTML file <HTML> <BODY> <form method="post" action="greeting.jsp"> <select name="combo1"> <option value="Park">Park </option> <option value="Chae">Chae </option> </select> <input type="submit"> </form> </BODY> </HTML> Contd…. • Write the following code in JSP file • <%@ taglib prefix="d" uri="http://java.sun.com/jsp/jstl/core" %> • <d:set var="s" value="${param.combo1}" /> • Welcome <d:out value="${s}" /><br> • <d:if test="${s eq 'Park'}"> • <d:out value="Good Morning Mr.Park"/> • </d:if> • <d:if test="${s == 'Chae'}"> • <d:out value="How are you Mr. Chae"/> • </d:if> Explanation • <d:if test="${s eq 'Park'}"> • <d:out value="Good Morning Mr.Park"/> • </d:if> • The above means, check the value of variable s. If it is equal to 'park', then execute <d:out>. • This tag is similar to if-then • In the second <d:if> note that, I have used == instead of eq. Using <c:choose> Syntax: • <c:choose> • <c:when test=""> output1 </c:when> • <c:otherwise> output2 </c:otherwise> • </c:choose> • Note that, we donot need <c:out> for output here. • This tag is similar to switch-case Example Write the following code in html file • • • • • • • • • • • • • • <HTML> <BODY> <form method="post" action="greeting.jsp"> <select name="combo1"> <option value="1">1 </option> <option value="2">2 </option> <option value="3">3 </option> <option value="4">4 </option> <option value="5">5 </option> </select> <input type="submit"> </form> </BODY> </HTML> Contd…. • • • • • • • • • • • • • • Write the following code in jsp file <%@ taglib prefix="d" uri="http://java.sun.com/jsp/jstl/core" %> <d:set var="s" value="${param.combo1}" /> Today is <d:choose> <d:when test="${s == 1}"> Sunday </d:when> <d:when test="${s == 2}"> Monday </d:when> <d:when test="${s == 3}"> Tuesday </d:when> <d:when test="${s == 4}"> Wednesday </d:when> <d:when test="${s == 5}"> Thrusday</d:when> <d:otherwise> Select between 1 and 5 </d:otherwise> </d:choose> <br><a href="jstl_ex.html"> go Back</a> Using <c:forEach> • This tag is same as for loop. Syntax • <c:forEach var="" start="" end="" step="" items="" varStatus=""> • </c:forEach> • var = "variable name" • start = "start value" • end = "ending value" • step="increment or decrement value" • varStatus = "" // i will explain thru example • items = "" // i will explain thru example Example • Create a jsp file and write the following code • <%@ taglib prefix="d" uri="http://java.sun.com/jsp/jstl/core" %> • <d:forEach var="n" begin="3" end="8"> • <d:out value="${n}"/> • </d:forEach> • The output is 3 4 5 6 7 8. • If I write step=“2”, then output will be 3 5 7. 2nd Example of <c:forEach> • • • • • • • • • • • • • • • • • <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %> <% String[] str = {"park", "chae", "won", "jung", "moon"}; pageContext.setAttribute("str1",str); %> <table border=1> <tr><td>Index</td><td>Value</td><td>FirstValue</td><td>LastValue</td><td>Count </td> </tr> <c:forEach items="${str1}" varStatus="a"> <tr> <td><c:out value="${a.index}"/></td> <td><c:out value="${a.current}"/></td> <td><c:out value="${a.first}"/></td> <td><c:out value="${a.last}"/></td> <td><c:out value="${a.count}"/> </td> </tr> </c:forEach> </table> Explanation String[] str = {"park", "chae", "won", "jung", "moon"}; • The line above declares an array pageContext.setAttribute("str1",str); • This line gives page scope to str variable and stores the value of str in str1. • That means str will be referred by str1 from now. <c:forEach items="${str1}" varStatus="a"> • In this line, varStatus acts like an index for array str1. • • • • • a.index returns the index a.current returns the value of the element where a is pointing now. a.first returns true if index = 0 or count = 1 a.last returns true if index = size of array-1 or count = size of array a.count returns the position where a is pointing. <c:forTokens> with delimiter • <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %> • <c:set var="s" value="park,Gyeonggido,suwon,korea"/> • <c:forTokens items="${s}" delims="," var="a"> • <c:out value="${a}"/> • </c:forTokens> Using <c:import> • <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %> • This is conjoint.html • <c:import url="conjoint.html" /> • welcome to this website Using <c:redirect> • <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %> • <c:redirect • url="ConjointCards.jsp" /> 2nd Example of <c:redirect> • • • • • • • • Another Example of <c:redirect> <c:redirect url="ConjointCards.jsp" /> If you want you can also pass parameters to the forwarded page <c:redirect url="ConjointCards.jsp" > <c:param name="name1" value="SAM"/> </c:redirect>