JSP三(JSTL)

夏至,離別發表於2020-10-23

JSTL【JSP標準標籤庫】

一、EL表示式

  格式:${表示式/變數/方法}
  幫助我們去計算表示式的結果,並且直接將運算結果輸出
  注意:如果我們要在jsp頁面中使用EL表示式語言,那麼要在page指令中設定啟用EL 表示式【isELIgnored=“false”】

1.1 EL計算表示式的結果
<h2>算術123+234 = ${123+234}</h2>
<h2>關係123>234 = ${123>234}</h2>
<h2>邏輯(123>234) && (123<234) = ${(123>234) && (123<234)}</h2>

1.2 EL訪問本頁中宣告的變數

 <%
     String name="zhangsan";
   	 //request.setAttribute("myname", name);
   	 //session.setAttribute("myname", name);
   	 //application.setAttribute("myname", name);
   	 //pageContext.setAttribute("myname", name, PageContext.PAGE_SCOPE);
   %>
 <h2>jsp中java程式碼片段中的變數name==${myname}</h2>

1.3 EL訪問普通java程式中宣告的變數

package com.wangxing.bean;
public class Student {
  	private String  stuname;
public String getStuname() {
	return stuname;
}
public void setStuname(String stuname) {
		this.stuname = stuname;
}
}

<%
   		Student  stu=new Student();
   		stu.setStuname("lisi");
   		String stuname=stu.getStuname();
   		request.setAttribute("stuname", stuname);
      	 //session.setAttribute("myname", name);
      	 //application.setAttribute("myname", name);
      	 //pageContext.setAttribute("myname", name, PageContext.PAGE_SCOPE);
%>
<h2>jsp中java程式碼片段中的變數stuname==${stuname}</h2>
<%
Student  stu=new Student();
   		stu.setStuname("wangwu");
   		request.setAttribute("stu", stu);
   %>
<h2>jsp中java程式碼片段中的變數stuname==${stu.stuname}</h2>

  注意:通過EL表示式來訪問變數中儲存的資料,需要將這個變數儲存到request/session/application/pageContext物件中。

package com.wangxing.bean;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class StudentServlet extends HttpServlet{

	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		doPost(req, resp);
	}

	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		String stuname=req.getParameter("stuname");
		Student stu=new Student();
		stu.setStuname(stuname);
		req.setAttribute("stu", stu);
		req.getRequestDispatcher("/test2.jsp").forward(req, resp);
	}

}

 <h2>jsp中訪問Servlet中的傳遞來的變數stuname==${stu.stuname}</h2>

  優點:避免在jsp頁面中出現大量的<% %>java程式碼片段。

二、JSTL--JSP標準標籤庫

  將一些基本的java操作程式碼封裝成一個標籤,在jsp頁面中使用的時候,就可以像使用html標籤一樣.[簡單]

  1.需要新增jstl.jar和standard.jar

  jstl.jar下載地址:http://repo2.maven.org/maven2/javax/servlet/jstl/

  standard.jar下載地址:http://repo2.maven.org/maven2/taglibs/standard/
在這裡插入圖片描述
  2. 匯入依賴
  3. 在jsp頁面中通過taglib指令匯入jstl標籤

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

  uri — 標籤庫路徑【不同的路徑表示使用不同的標籤庫】
  core — 核心標籤庫【經常使用】
  fmt — 格式化標籤庫
  functions — 函式標籤庫
  sql — sql標籤庫
  xml — xml的標籤庫
  prefix — 是為使用標籤庫中的標籤時定義的字首名稱,通過字首名稱來判斷使用的標籤庫; prefix=“c” 字首名稱就是c,c就是core標籤庫。

三、< c:if> 標籤

  判斷表示式的值,如果表示式的值為 true 則執行其主體內容。
  格式:
    <c:if test=“判斷表示式” >
      主體內容
    </c:if>

package com.wangxing.servlet;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class TestServlet extends HttpServlet{
	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		doPost(req, resp);
	}
	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		  String name="wangwu";
		  req.setAttribute("myname", name);
		  req.getRequestDispatcher("/test1.jsp").forward(req, resp);
	}
}

<%@ 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>
     <%-- 
	 <%
	   String name="lisi";
	   if(name==""){
		   out.write("<h2>name變數的值為空</h2>");
	   }
	   if(name!=""){
		   out.write("<h2>name變數的值為"+name+"</h2>");
	   }
	 %>
	 --%>
	 <%-- 
      <%
        String name="zhangsan";
        request.setAttribute("myname", name);
      %>
      <c:if test="${myname==''}">
        <h2>name變數的值為空</h2>
      </c:if>
      <c:if test="${myname!=''}">
        <h2>name變數的值為${myname}</h2>
      </c:if>
      --%>
      <h2>通過jstl的便籤和EL訪問變數</h2>
      <c:if test="${myname==''}">
        <h2>name變數的值為空</h2>
      </c:if>
      <c:if test="${myname!=''}">
        <h2>name變數的值為${myname}</h2>
      </c:if>
</body>
</html>

執行結果:

在這裡插入圖片描述

四、< c:forEach>標籤

  遍歷集合【它迭代一個集合中的物件】

items要被迴圈的資訊【集合/陣列】
var代表當前條目的變數名稱
package com.wangxing.bean;

	public class Student {
		private int stuid;
		private String  stuname;
		private String stuaddress;
		public int getStuid() {
			return stuid;
		}
		public void setStuid(int stuid) {
			this.stuid = stuid;
		}
		public String getStuname() {
			return stuname;
		}
		public void setStuname(String stuname) {
			this.stuname = stuname;
		}
		public String getStuaddress() {
			return stuaddress;
		}
		public void setStuaddress(String stuaddress) {
			this.stuaddress = stuaddress;
		} 
}

package com.wangxing.servlet;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.wangxing.bean.Student;
public class TestServlet extends HttpServlet{

	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		doPost(req, resp);
	}
	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		  /*
		  String name="wangwu";
		  req.setAttribute("myname", name);
		  req.getRequestDispatcher("/test1.jsp").forward(req, resp);
	      */
		 Student  stu1=new Student();
	      stu1.setStuid(1001);
	      stu1.setStuname("zhangsan");
	      stu1.setStuaddress("西安");
	      Student  stu2=new Student();
	      stu2.setStuid(1002);
	      stu2.setStuname("lisi");
	      stu2.setStuaddress("北京");
	      Student  stu3=new Student();
	      stu3.setStuid(1003);
	      stu3.setStuname("王五");
	      stu3.setStuaddress("上海");
	      List<Student> stulist=new ArrayList<Student>();
	      stulist.add(stu1);
	      stulist.add(stu2);
	      stulist.add(stu3);
	      req.setAttribute("mystulist", stulist);
	      req.getRequestDispatcher("/test3.jsp").forward(req, resp);
	}
}

<%@ 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>
   <h1>通過JSTL標籤+EL顯示錶格資料</h1>
   <table border="1px">
     <c:forEach items="${mystulist}" var="stu">
       <tr>
       		<td>${stu.stuid}</td>
       		<td>${stu.stuname}</td>
       		<td>${stu.stuaddress}</td>
       </tr>
     </c:forEach>
   </table>
</body>
</html>

執行結果:

在這裡插入圖片描述

相關文章