JSTLでのCoreの使用


まずJSTLをダウンロードして、それからjstl.jar, standard.jarの2つのjarパッケージcopyからlibの下に、使用するCoreラベルのc.tldファイルcopyをWEB-INFの下に、webを修正します.xmlファイル
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" 
	xmlns="http://java.sun.com/xml/ns/javaee" 
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
	xsi:schemaLocation="http://java.sun.com/xml/ns/javaee 
	http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
  
  <jsp-config>
  	<taglib>
  		<taglib-uri>jstl-c</taglib-uri>
  		<taglib-location>/WEB-INF/c.tld</taglib-location>
  	</taglib>
  </jsp-config>
  
  <welcome-file-list>
    <welcome-file>index.jsp</welcome-file>
  </welcome-file-list>
</web-app>
はtaglibを定義する.jspは、タグ
<%@ taglib prefix="c" uri="jstl-c"%>
を導入するoutタグの
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8" %>
<%@include file="taglib.jsp" %>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <base href="<%=basePath%>">
    
    <title>c:out   </title>
    
	<meta http-equiv="pragma" content="no-cache">
	<meta http-equiv="cache-control" content="no-cache">
	<meta http-equiv="expires" content="0">    
	<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
	<meta http-equiv="description" content="This is my page">

  </head>
  
  <body>
    <h2 align="center">c:out        <%="param1" %></h2><br>
    <h3>  value    value     default  ,escapeXml         </h3>
    <br><br>
    <c:out value="  JSTL"></c:out><br>
    <c:out value="${sessionScope.username}" default="libinxuan"></c:out><br>
    <c:out value="${username}" default="libinxuan"></c:out><br>
	<c:out value="<h1>  JSTL</h1>" escapeXml="false"></c:out><br>
	<c:out value="<h1>  JSTL</h1>" escapeXml="true"></c:out><br>
  </body>
</html>
setを用いる
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@taglib uri="jstl-c" prefix="c" %>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <base href="<%=basePath%>">
    
    <title>c:set   </title>
    
	<meta http-equiv="pragma" content="no-cache">
	<meta http-equiv="cache-control" content="no-cache">
	<meta http-equiv="expires" content="0">    
	<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
	<meta http-equiv="description" content="This is my page">

  </head>
  
  <body>
    <h2 align="center">         JavaBeans    JSP         </h2><br><br>
    <h2>      :</h2><br>
    <h3>    value:       ,   EL      </h3><br>
    <h3>    target:          ,   JavaBeans   </h3><br>
    <h3>    property:     JavaBeans   </h3><br>
    <h3>    var:            </h3><br>
    <h3>    scope:          </h3><br><br><br>
    <h2>  :    target        property  </h2><br><br>
    <c:set value="libinxuan" var="username" scope="session"></c:set><br>
    <c:set value="libinxuan" var="user" scope="session"></c:set><br>
    <c:set var="username2" scope="session">libinxuan</c:set><br><br>
    <h2> value     target   propertyName   </h2><br>
    <c:out value="${sessionScope.user}" default="sss"></c:out>
  </body>
</html>
removeを用いる
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@taglib uri="jstl-c" prefix="c" %>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <base href="<%=basePath%>">
    
    <title>c:remove   </title>
    
	<meta http-equiv="pragma" content="no-cache">
	<meta http-equiv="cache-control" content="no-cache">
	<meta http-equiv="expires" content="0">    
	<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
	<meta http-equiv="description" content="This is my page">

  </head>
  
  <body>
    <h2 align="center">           </h2><br><br>
    <h3>  session   username     </h3><br>
    <c:remove var="username" scope="session"/>
  </body>
</html>
ifを用いる
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@include file="taglib.jsp" %>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <base href="<%=basePath%>">
    
    <title>c:if   </title>
    
	<meta http-equiv="pragma" content="no-cache">
	<meta http-equiv="cache-control" content="no-cache">
	<meta http-equiv="expires" content="0">    
	<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
	<meta http-equiv="description" content="This is my page">

  </head>
  
  <body>
  	<c:set value="libinxuan" var="name" scope="session"></c:set>
  	<c:out value="${sessionScope.name}"></c:out><br>
    <c:if test="${sessionScope.name == 'libinxuan'}">
    	       
    </c:if><br>
    <c:if test="${1>0}">
    	  
    </c:if>
  </body>
</html>
chooseの使用を先に見るために用いられ、ここではjspの要求inputを用いる.jsp
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <base href="<%=basePath%>">
    
    <title>    choose,when,otherwise</title>
    
	<meta http-equiv="pragma" content="no-cache">
	<meta http-equiv="cache-control" content="no-cache">
	<meta http-equiv="expires" content="0">    
	<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
	<meta http-equiv="description" content="This is my page">
	<!--
	<link rel="stylesheet" type="text/css" href="styles.css">
	-->

  </head>
  
  <body>
  	<form action="choose.jsp">
    	       :<input type="text" name="age" id="age" >
    	<input type="submit" value="  " >
    </form>	
  </body>
</html>
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@taglib uri="jstl-c" prefix="c" %>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <base href="<%=basePath%>">
    
    <title>c:choose,c:when,s:otherwise   </title>
    
	<meta http-equiv="pragma" content="no-cache">
	<meta http-equiv="cache-control" content="no-cache">
	<meta http-equiv="expires" content="0">    
	<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
	<meta http-equiv="description" content="This is my page">

  </head>
  
  <body>
    <c:choose>
    	<c:when test="${param.age>70}">
    		  
    	</c:when>
    	<c:when test="${param.age<=70 and param.age>=35 }">
    		   
    	</c:when>
    	<c:when test="${param.age>=0 and param.age<=35 }">
    		   
    	</c:when>
    	<c:otherwise>
    		    
    	</c:otherwise>
    </c:choose>
  </body>
</html>
forEachの使用
<%@ page language="java" import="java.util.*"  pageEncoding="UTF-8"%>
<%@include file="taglib.jsp" %>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <base href="<%=basePath%>">
    
    <title>c:forEach     </title>
    
	<meta http-equiv="pragma" content="no-cache">
	<meta http-equiv="cache-control" content="no-cache">
	<meta http-equiv="expires" content="0">    
	<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
	<meta http-equiv="description" content="This is my page">
	<%
    	String names[] = new String[4];
    	names[0]="  ";
    	names[1]="  ";
    	names[2]="  ";
    	names[3]="  ";
    	pageContext.setAttribute("names",names);
    %>
  </head>
  
  <body>
  	<h2 align="center">       ,  ,  </h2>
    <h2>      :</h2><br/>
    <h3>     begin:      </h3><br>
    <h3>     end:      </h3><br>
    <h3>     step:   </h3><br>
    <h3>     var:          </h3><br>
    <h3>     items:        </h3><br>
    <h3>     varStatus:          </h3><br>
    <c:forEach items="${names}" var="name" varStatus="i">
    	${name}<br>
    	${i.index}<br>
    </c:forEach>
  </body>
</html>