-
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
-
<c:out value='${xyz}' escapeXml='true' />
Any HTML tags are not evaluated. If you set set escapeXml to false, HTML tags are evaluated.
-
<c:out value='${user}' default='guest' />
If the EL or expression (value) evaluates to null, default value is printed.
-
<c:forEach var="movie" items="${movieList}" varStatus="loopCount">
${movie}, ${loopCount.count}
</c:forEach>
<c:forEach> is used to iterate through collections and arrays. It iterates through items.
-
<c:if test="{userType eq 'member'}" >
doSomething
</c:if>
-
<c:choose>
<c:when test="${x > 1}" >doSomething</c:when>
<c:when test="${x > 2}" >doSomething</c:when>
<c:otherwise>something else</c:otherwise>
</c:choose>
Only one of the whens or otherwise runs. There is no fall through logic like switch statement.
-
Setting an attribute variable - var with <c:set>
<c:set var="userName" value="cowboy" scope="session" />
or
<c:set var="userName" scope="session" >
cowboy
</c:set>
- 1. If there is no attribute in the specified scope, tag creates one (assuming value is not null)
- 2. If the value evaluates to null, the variable is REMOVED.
- 3. Value doesn't have to be String.
- 4. Scope is optional. Default is page scope.
-
Setting property or value of a bean or a Map - <c:set> with target
<c:set target="${petMap}" property="dogName" value="clover" />
Value can be specified as body
- 1. The target must evaluate to Object and NOT String. That means we can use a EL expression or scripting expression (<%= %>) or <jsp:attribute>
- 2. If target expression is null or is not a bean or a Map, the container throws an exception.
- 3. If target is a bean and the property is not found, exception is thrown. (same with ${bean.notAProperty} ).
-
<c:remove var="status" scope="request"/>
- 1. The var must be a String literal. It can't be an expression.
- 2. Scope is optional. If you leave out the scope, the attribute is removed from ALL scopes
-
<c:import url="http://akdjask.com" />
or
<c:import url="http://akdjask.com" >
<c:param name="subtitle" value="aksdjkadja" />
</c:import>
- 1. Adds the content dynamically
- 2. Can reach outside the web app
- 3. ${param.subtitle}
-
<c:url value="/comments.jsp" />
Url rewriting
-
<c:catch var="myException">
</c:catch>
- 1. myException is of type Throwable,
- 2. You can use ${myException.message}
- 3. Nothing in the catch after exception will be printed.
- 4. Error Page mechanism will not kick in when using <c:catch>
|
|