【javaweb笔记】11、JSTL基础(set、out、remove、if、choose、forEach)


JSTL:比EL更加强大
需要引入2个jar :jstl.jar   standard.jar
引入tablib  :
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>    
其中prefix="c" :前缀

核心标签库:  通用标签库、条件标签库  迭代标签库

a.通用标签库
<c:set>赋值
i:
在某个作用域之中(4个范围对象),给某个变量赋值

    <%-- 
        request.setAttribute("name", "zhangsan") ;
    --%>
        <c:set var="name"    value="zhangsan"   scope="request"/>
        ${requestScope.name }

<c:set var="变量名"    value="变量值"   scope="4个范围对象的作用域"/>
ii:
给普通对象赋值

在某个作用域之中(4个范围对象),给某个对象的属性复制 (此种写法,不能指定scope属性)

        <c:set target="${requestScope.student}" property="sname"  value="zxs" />

iii:

给map对象赋值
<c:set target="${requestScope.countries}" property="cn"  value="中国" />

<c:set target="对象" property="对象的属性"  value="赋值" />

注意 <c:set>可以给不存在的变量赋值 (但不能给不存在的对象赋值)


<c:out>  :显示
true:<c:out value='<a href="https://www.baidu.com">百度</a>' default="当value为空的,显示的默认值" escapeXml="true" />
false:    <c:out value='<a href="https://www.baidu.com">百度</a>' escapeXml="false" />


<c:remove >:删除属性
<c:remove var="a" scope="request"/>


<c:if>:单重选择
选择:
if(boolean)
单重选择
<c:if test="" >


<c:choose>:多重选择
if else if... esle if... else  /switch
<c:choose>
    <c:when test="...">   </c:when>
    <c:when test="...">   </c:when>
    <c:when test="...">   </c:when>
    <c:otherwise>   </c:otherwise>
</c:choose>
在使用 test="" 一定要注意后面是否有空格
例如:test="${10>2 }"   true
     test="${10>2 } "  非true,为true+" "


<c:forEach>:循环
循环(迭代标签库)
for(int i=0;i<5;i++)
    <c:forEach  var="name" items="${requestScope.names }" >
        -${name }-
    </c:forEach>


for(String str:names)
    <c:forEach  var="student" items="${requestScope.students }" >
        ${student.sname }-${student.sno }
    </c:forEach>


InitServlet.java

package org.student.servlet;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

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

import org.student.entity.Address;
import org.student.entity.Student;

/**
 * Servlet implementation class InitServlet
 */
public class InitServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public InitServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		Student student = new Student();
		student.setSno(1);
		student.setSname("zs");
		Address add = new Address();
		add.setHomeAddress("xa");
		add.setSchoolAddress("bj");
		
		student.setAddress(add);
		
		request.setAttribute("student", student);
		request.setAttribute("he-llo", "world");
		
		String[] arr = new String[] {"aa","cc","dd"};
		request.setAttribute("he-llo", "world");
		request.setAttribute("arr", arr);
		response.setHeader("Cache-Control","no-cache"); //HTTP 1.1    
		response.setHeader("Pragma","no-cache"); //HTTP 1.0    
		response.setDateHeader ("Expires", 0); //prevents caching at the proxy server  
		
		Map<String,String> map = new HashMap<>();
		map.put("cn", "China");
		map.put("us", "USA");
		
		request.setAttribute("countries", map);
		
		
		String[] names = new String[] {"aaa","bb","ccc"};
		
		request.setAttribute("names", names);
		
		Student stu1 = new Student();
		Student stu2 = new Student() ;
		stu1.setSname("zs");
		stu1.setSno(1);
		stu2.setSname("ls");
		stu2.setSno(2);
		
		List<Student> students = new ArrayList<>();
		students.add(stu1);
		students.add(stu2);
		request.setAttribute("students", students);
		
		request.getRequestDispatcher("jstl2.jsp").forward(request, response);
		
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doGet(request, response);
	}

}

jstl.jsp

<%@ 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>	
	<%-- 
		request.setAttribute("name", "zhangsan") ;
	--%>
		<c:set var="name"    value="zhangsan"   scope="request"/>
		${requestScope.name }
		<br/>
		=======给普通对象的属性赋值========<br/>
		${requestScope.student.sname } <br/>
		
		<c:set target="${requestScope.student}" property="sname"  value="zxs" />
		${requestScope.student.sname } <br/>

	=======给map对象的属性赋值========<br/>
		${requestScope.countries.cn }<br/>
		<c:set target="${requestScope.countries}" property="cn"  value="中国" />
			${requestScope.countries.cn }
			
			
		=======给不存在的变量赋值========<br/>
	<c:set var="x" value="y" scope="request"/>
		${requestScope.x }
		=======给不存在的对象赋值========
		
		
		<br/>
		<br/>
		<br/>
		
		=======c:out====<br/>
		
		传统EL:${requestScope.student }<br/>
		c:out方式:<c:out value="${requestScope.student }"   /><br/>
		c:out显示不存在的数据:<c:out value="${requestScope.stu }"   default="zs-23"  /><br/>
		<a href="https://www.baidu.com">百度</a><br/>
		
		
		true:<c:out value='<a href="https://www.baidu.com">百度</a>'  escapeXml="true" />
		false:<c:out value='<a href="https://www.baidu.com">百度</a>' escapeXml="false" />
		
		<c:set  var="a" value="b" scope="request"/><br/>
		显示:${a }
		<c:remove var="a" scope="request"/>
		删除后再显示:${a }
		
		
</body>
</html>

jstl2.jsp

<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!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>

	<c:if test="${10>2 }" var="result" scope="request">
			真
			${requestScope.result }
	</c:if>
	
	<c:set var="role" value="学生"  scope="request"></c:set>
	
	
	
	
	<c:choose>
		<c:when test="${requestScope.role == '老师' }"> 
				老师代码...
		  </c:when>
		<c:when test="${requestScope.role eq '学生' }"> 
		     	学生代码...  
		</c:when>
		<c:otherwise>  
				管理员等其他人员....
		 </c:otherwise>
	</c:choose>
	
	
	=============
	<c:forEach  begin="0"  end="5" step="1"  varStatus="status">
		${status.index }
		  test....
	</c:forEach>
	=====<br/>
	<c:forEach  var="name" items="${requestScope.names }" >
		-${name }-
	</c:forEach>

	************<br/>
	
	<c:forEach  var="student" items="${requestScope.students }" >
		${student.sname }-${student.sno }
	
	</c:forEach>
	

</body>
</html>

猜你喜欢

转载自blog.csdn.net/kuaileky/article/details/86726498