JSP(2)

2. JSTL [JSP standard tag library]
6.1. EL expression
format: ${expression/variable/method}
helps us to calculate the result of the expression, and directly output the result of the operation
Note: If we want to in the jsp page Use the EL expression language, then set the enable EL expression in the page command [isELIgnored="false"]
1. The result of the EL calculation expression

<h2>算术123+234 = ${123+234}</h2>
<h2>关系123>234 = ${123>234}</h2>
<h2>逻辑(123>234) && (123<234) = ${(123>234) && (123<234)}</h2>

2. EL access to variables declared on this page

 <%
     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>

3.EL access to variables declared in ordinary java programs

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>

Note: To access the data saved in the variable through the EL expression, you need to save this variable in the request/session/application/pageContext object.

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>

Advantages: Avoid a large number of <% %> java code fragments in the jsp page.
JSTL-JSP standard tag library
encapsulates some basic java operation code into a tag, when used in a jsp page, it can be used like html tags. [Simple]
1. Need to add jstl.jar and standard.jar
jstl. Jar download address: http://repo2.maven.org/maven2/javax/servlet/jstl/
standard.jar download address: http://repo2.maven.org/maven2/taglibs/standard/

Insert picture description here

2. Import dependency
3. Import the jstl tag through the taglib instruction in the jsp page
<%@taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %> uri-
tag library Path [Different paths indicate the use of different tag libraries]
core—core tag library [often used]
fmt—formatting tag library
functions—function tag library
sql—sql tag library
xml—xml tag library
prefix—is to use tag library The name of the prefix defined in the tag is used to determine the tag library used by the prefix name; prefix="c" The prefix name is c, and c is the core tag library.
6.2. <c:if> tag
Judge the value of the expression, if the value of the expression is true, execute its main content.
Format:
<c:if test="judgment expression" >
main content

</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>

Insert picture description here

6.3. The <c:forEach> tag
traverses the collection [it iterates over the objects in a collection]
Insert picture description here

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>

Insert picture description here

Guess you like

Origin blog.csdn.net/guoguo0717/article/details/109251099
jsp