JSTL的相關使用

lonecloud發表於2016-06-12

index.jsp

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<!-- 先得定義一個taglib -->
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" 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>My JSP 'index.jsp' starting page</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>
  <!-- 輸出一個常量 -->
  <c:out value="this is a first demo"></c:out>
  <% 
  	String name="1q23";
  	session.setAttribute("name", name);
  	request.setAttribute("name", name);
  %>
  <!-- 輸出一個變數 -->
  <!-- 這裡通過el表示式輸出了name的值 -->
  <!-- EL表示式中的幾個物件 -->
  ${sessionScope.name }
  ${requestScope.name }
  ${responseScope.name }
  ${pageScope.name }
  <c:out value="${sessionScope.name}"></c:out><br>
  <!-- 輸出轉義字元  escapeXml設定為false-->
  <c:out value=" " escapeXml="false"></c:out><br>
  <c:out value=" " escapeXml="true"></c:out><br>
  <!-- 設定預設值如果這個變數不存在的話 -->
  <c:out value="${responseScope.name }" default="123"></c:out><br>
    This is my JSP page. <br>
  </body>
</html>

choose.jsp

<%@page import="java.util.ArrayList"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@ 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=UTF-8">
<title>Insert title here</title>
</head>
<body>
	<form action="chooose.jsp">
		<input type="text" name="score" value="${param.score }">
		<input type="submit" value="點選">
	</form>
	<c:choose>
		<c:when test="${param.score>90 }">
			<c:out value="太棒了"></c:out>
		</c:when>
		<c:otherwise>
			<c:out value="ohhhh"></c:out>
		</c:otherwise>
	</c:choose>
	<!-- foreach函式  -->
	<%
		ArrayList<String> list=new ArrayList<String>();
		list.add("x1");
		list.add("x2");
		list.add("x3");
		list.add("x4");
		list.add("x5");
		list.add("x6");
		session.setAttribute("list", list);
	 %>
	 <!-- 這裡的var是物件的遍歷內容 begin 和end是結束標準 step 是步長 varStatus是獲取這裡面的屬性值 -->
	<c:forEach var="f1" items="${list}" begin="0" end="5" step="1" varStatus="f">
		<c:out value="${f1}"></c:out><br>
	</c:forEach>
	<!-- forTaken的用法 -->
	<c:forTokens items="這裡是分割字串-的地方-這裡的內容是-文字" delims="-" var="str">
		<c:out value="${str }<br>" escapeXml="false"></c:out>
	</c:forTokens>
	<!-- 這個是匯入網站 scope儲存的區域-->
	<c:import url="http://www.imooc.com" charEncoding="utf-8" scope="session">
		
	</c:import> 
	<!-- 可以匯入其他的本地的專案的jsp -->
	<c:catch var="e">
		<c:import url="/index.jsp" context="LoginDemo"></c:import>
	</c:catch>
	<c:out value="${e }"></c:out>
</body>
</html>

redirect.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c1" %>
<!-- 匯入核心類庫 -->
<%@ taglib uri="http://java.sun.com/jsp/jstl/functions" prefix="fn" %>
<!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=UTF-8">
<title>Insert title here</title>
</head>
<body>
<!-- redirect標籤 
<c1:redirect url="index.jsp">
	<c1:param name="user">123</c1:param>
	<c1:param name="pass">456</c1:param>
</c1:redirect>-->
<!-- 下面是fn的例子 -->
<c1:out value="${fn:contains('21332','21') }"></c1:out>
</body>
</html>

set.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
    <!-- javaBean 的初始化 -->
<jsp:useBean id="person" class="cn.lonecloud.Person"></jsp:useBean>
<%@ 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=UTF-8">
<title>Insert title here</title>
</head>
<body>
	<!-- 設定session中的值 -->
	<c:set value="demo" var="name1" scope="session"></c:set>
	<c:out value="獲取session中的值:${sessionScope.name }<br>" escapeXml="false"></c:out>
	<!-- 設定javaBean中的值 target是獲取例項名稱 proerty 是設定例項中的哪個屬性 -->
	<!-- 必須注意的是target="${person }"  必須用El表示式不然會出錯-->
	<c:set  target="${person }" property="name" value="張三" ></c:set>
	<c:out value="${person.name}"></c:out>
	<!-- remove標籤--只能remove一個變數 -->
	<br>
	<c:remove var="name1"/>
	<c:out value="remove後的${name1 }"></c:out>
	<!-- catch 塊進行抓取error到裡面去 -->
	<c:catch var="err">
		<c:out value="${3/0}"></c:out>
	</c:catch>
	<c:out value="err"></c:out>
	<form action="set.jsp" method="post">
		<!-- 必須設定name屬性 -->
		<input type="text" name="score" value="${param.score }">
		<input type="submit" value="點選">
	</form>
	<c:out value="${param.score}"></c:out>
	<c:if test="${param.score>90 }" var="result">
		<c:out value="恭喜你"></c:out>
	</c:if>
	<c:out value="${result }"></c:out>
</body>
</html>

  

相關文章