JSTL
Code for this page is at :
https://github.com/gnurmatova/jersey-mvc-jstl-scopes
JSTL stands for JavaServer Pages Standard Tab Library. It is a set of Java tag libraries that simplify coding on JSP, giving your JSP page a tag (like HTML and XML) look-and-feel rather than a scriptlet/Java look-and-feel
https://github.com/gnurmatova/jersey-mvc-jstl-scopes
JSTL stands for JavaServer Pages Standard Tab Library. It is a set of Java tag libraries that simplify coding on JSP, giving your JSP page a tag (like HTML and XML) look-and-feel rather than a scriptlet/Java look-and-feel
dependencies
<dependency>
<groupId>jstl</groupId>
<artifactId>jstl</artifactId>
<version>1.2</version>
</dependency>
<dependency>
<groupId>taglibs</groupId>
<artifactId>standard</artifactId>
<version>1.1.2</version>
</dependency>
Note: If you get errors about dependencies, remove the tomcat dependencies from your classpath
Your jsp file to lists songs will change to
<%@ page language="java" contentType="text/html; charset=US-ASCII"
pageEncoding="US-ASCII"%>
<%@ page import="model.*" %>
<%@ page import="java.util.*" %>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=US-ASCII">
<title>Passing Model to MVC example</title>
</head>
<body>
<h1>you are logged in as ${it.user}!</h1>
<p>
songs available in the store :<br />
<c:forEach var="song" items="${it.songs}">
${song.getTitle()}<br />
</c:forEach>
</p>
</body>
</html>
Core jstl tags
Tag | Description |
---|---|
<c:out > | Like <%= ... >, but for expressions. |
<c:set > | Sets the result of an expression evaluation in a 'scope' |
<c:remove > | Removes a scoped variable (from a particular scope, if specified). |
<c:catch> | Catches any Throwable that occurs in its body and optionally exposes it. |
<c:if> | Simple conditional tag which evalutes its body if the supplied condition is true. |
<c:choose> | Simple conditional tag that establishes a context for mutually exclusive conditional operations, marked by <when> and <otherwise> |
<c:when> | Subtag of <choose> that includes its body if its condition evalutes to 'true'. |
<c:otherwise > | Subtag of <choose> that follows <when> tags and runs only if all of the prior conditions evaluated to 'false'. |
<c:import> | Retrieves an absolute or relative URL and exposes its contents to either the page, a String in 'var', or a Reader in 'varReader'. |
<c:forEach > | The basic iteration tag, accepting many different collection types and supporting subsetting and other functionality . |
<c:forTokens> | Iterates over tokens, separated by the supplied delimeters. |
<c:param> | Adds a parameter to a containing 'import' tag's URL. |
<c:redirect > | Redirects to a new URL. |
<c:url> | Creates a URL with optional query parameters |
Examples:
<c:out value="put something here"></c:out>
<c:out value="${it.user}"></c:out>
<c:set var="rVar" value="1" scope="page"></c:set>
<c:set var="rVar" value="100" scope="request"></c:set>
<c:set var="sVar" value="200" scope="session"></c:set>
<c:set var="aVar" value="300" scope="application"></c:set>
<c:set var="rrVar" value="100" scope="request"></c:set>
<c:remove var="rrVar"></c:remove>
<c:out value="request scope variable:${requestScope.rrVar}"></c:out><br/>
<c:forEach > example is above in the jsp code
<c:catch var="exception">
<%int x = 100 / 0;%>
</c:catch>
<c:if test="${exception ne null}">
<br>Got Exception : ${exception}: <br />
${exception.message}
</c:if>
<c:set var="rnumber" scope="session" value="${300*3}" />
<p><c:out value="Number is:${rnumber}" /></p>
<c:choose>
<c:when test="${rnumber < 0}">
Negative!
</c:when>
<c:when test="${rnumber > 0}">
Positive!
</c:when>
<c:otherwise>
Zero!
</c:otherwise>
</c:choose>
<c:import var="mydata" url="username.jsp"/>
<c:out value="${mydata}"/>
<c:import var="mydata" url="username.jsp">
<c:param name="uname" value="Glenn"/>
</c:import>
<c:out value="${mydata}"/>
<c:out value="put something here"></c:out>
<c:out value="${it.user}"></c:out>
<c:set var="rVar" value="1" scope="page"></c:set>
<c:set var="rVar" value="100" scope="request"></c:set>
<c:set var="sVar" value="200" scope="session"></c:set>
<c:set var="aVar" value="300" scope="application"></c:set>
<c:set var="rrVar" value="100" scope="request"></c:set>
<c:remove var="rrVar"></c:remove>
<c:out value="request scope variable:${requestScope.rrVar}"></c:out><br/>
<c:forEach > example is above in the jsp code
<c:catch var="exception">
<%int x = 100 / 0;%>
</c:catch>
<c:if test="${exception ne null}">
<br>Got Exception : ${exception}: <br />
${exception.message}
</c:if>
<c:set var="rnumber" scope="session" value="${300*3}" />
<p><c:out value="Number is:${rnumber}" /></p>
<c:choose>
<c:when test="${rnumber < 0}">
Negative!
</c:when>
<c:when test="${rnumber > 0}">
Positive!
</c:when>
<c:otherwise>
Zero!
</c:otherwise>
</c:choose>
<c:import var="mydata" url="username.jsp"/>
<c:out value="${mydata}"/>
<c:import var="mydata" url="username.jsp">
<c:param name="uname" value="Glenn"/>
</c:import>
<c:out value="${mydata}"/>