Wednesday, February 17, 2016

JSTL

jstl(jsp standard tag library)

Version:
Not lower than JSP1.2 and Servlet 2.3 can use jstl
JSP2.0 can use jstl1.2
  1. normal usage tag
  2. conditional tag
  3. iterator tag
  4. url related tag(xml, sql)
Config jstl library
We need two librarys: jstl and taglibs-standard



import

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

Four levels, 



  1. pageContext.setAttribute("abc", "abc1
  2. <c:if test="${empty abc}">
  3. abc is empty
  4. </c:if>
  5. <c:if test="${!empty abc}">
  6. abc is not empty
  7. </c:if>
  8. ");
  9. request.setAttribute("abc", "abc2");
  10. session.setAttribute("abc", "abc3");
  11. application.setAttribute("abc", "abc3");

${pageContext.request.contextPath} is the contextPath I prefer to use, it returns the web app path, as same as request.getContextPath()

empty
detect if value is null, "", empty array, empty map or empty collection
<c:if test="${empty abc}">
abc is empty
</c:if>
<c:if test="${!empty abc}">
abc is not empty

</c:if>

quick condition
${2>1?"bigger":"smaller"}

func(arg,arg1,arg2)
call a function with zero to multiple arguments

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

${} is the mark for that enclosed value is a variable or expression
With the same attribute name, c:out value always looking for the lowest level value first.

<c:out value="${abc}" default="default_value"></c:out> : default value if no attribute found in any level

<c:out value="${abc}"  escapeXml="false"></c:out>
true: make the output to be text only
false: make the output to be html
default is true


<c:out value="${user.name}"></c:out>: user is an object, name is the field of the object
Which equals ((User)request.getAttribute("user")).getName();

<c:set>
<c:set var="abc" value="abc4" scope="request"></c:set>
Do not include ${} in var because it's already variable name, only use ${} when it's for value

<c:remove>
<c:remove var="abc" scope="request"/>
If scope is not set, this will remove all four levels attributes with that variable name

<c:catch>
<c:catch var="myException" >
<% int i=8/0; %>
</c:catch>
<c:out value="${myException}"></c:out><c:out value="${myException.message}"></c:out>
Catch and assign the exception a variable name, so you can catch it latter

<c:if>
<c:if> can only work with single condition
<c:if test="${a=='hello'}">
OK!
</c:if>
<c:if test="${a!='hello'}">
NOT OK!
</c:if>

<c:if test="${age>56}">
older
</c:if>

<c:if test="${age1>10 and age1<56}">
in middle
</c:if>

<br />

<c:choose><c:when><c:otherwise>
Same as:
switch(){
case:
case:
default:
}
<c:choose>
<c:when test="${age>56}">Old</c:when>
<c:when test="${age<29}">Young</c:when>
<c:when test="${age>=29 and age<=56}">Middle</c:when>
<c:otherwise>Age not available</c:otherwise>
</c:choose>


<c:forEach>

<%
ArrayList<User> list = new ArrayList<User>();
list.add(new User("aaa",19));
list.add(new User("bbb",29));
list.add(new User("ccc",39));
pageContext.setAttribute("list", list);
%>
1.Simple forEach
<c:forEach items="${list}" var="user">
<c:out value="${user.name}" />
<c:out value="${user.age}" />
<br />
</c:forEach>
2. iterate fix times, like for loop
<c:forEach var="i" begin="1" end="10">
<c:out value="${i}" />
</c:forEach>
3. iterate fix times with number of steps, like for loop
instance 1 can use `step` also
<c:forEach var="i" begin="1" end="10" step="3">
<c:out value="${i}" />
</c:forEach>
4. can iterator through map or set
<c:forEach items="${map}" var="entry">
key=${entry.key},value=${entry.value}
</c:forEach>

<c:forEach items="${set}" var="value">
value=${value}
</c:forEach>
5. nested level also work fine
<c:forEach items="${set}" var="value">
name=${value.person.name}
departmentName=${value.department.name}
</c:forEach>

<c:forTokens>
<c:forTokens items="a,b,c,d,e,f" begin="0" end="2" step="2" delims="," var="value">
<c:out value="${value}" />
</c:forTokens>

<c:redirect>
<c:redirect url="http://www.google.com"></c:redirect>

<c:import>
import other pages, can also pass parameters, use ${param.name} to get passed value
<c:import url="/WEB-INF/a.jsp">
<c:param name="name" value="abc"></c:param>
</c:import>

No comments:

Post a Comment