【JSTL】--JSTL表达式:c:set,c:if,c:choose,--drp214

jstl_core.jsp

<%@ page language="java" contentType="text/html; charset=GB18030"
    pageEncoding="GB18030"%>
   <%@ 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=GB18030">
<title>Insert title here</title>
</head>
<body>
	<h1>测试JSTL核心库</h1>
	<hr>
	<li>采用c:out标签</li><br>
	 hello(使用标签:)<c:out value="123"/><br>
	 hello(使用标签:)<c:out value="hello"/><br>
	 hello(使用标签:)<c:out value="${hello}"/><br>
	 hello(使用标签:EL表达式)${hello}<br>
	 hello(default):${hello123 }<br>
	 hello(使用缺省值):<c:out value="${hello123 }" default="没有值"/><br>
	 hello(使用缺省值):<c:out value="${hello123 }" >没有值</c:out><br>
	 Welcome(使用EL表达式):${welcome}<br>
	 welcome(使用标签,escapeXml=true):<c:out value="${welcome }" escapeXml="true"/><br>
	 welcome(使用标签,escapeXml=false):<c:out value="${welcome }" escapeXml="false"/><br>
	 
	 <p>
	 <li>测试 c:set ,c:remove</li><br>
	 <c:set value="root" var="userid"/>
	 userid:${userid}<br>
	 <c:remove var="userid"/>
	 userid:${userid }<br>
	 
	 <p>
	 <li>条件控制标签c:if</li><br>
	 <c:if test="${v1 lt v2 }">
	 	v1小于v2
	 </c:if>
	 
	 <p>
	 <li>条件控制标签:c:choose,c:when,c:otherwise</li><br>
	 <c:choose>
	 	<c:when test="${v1 gt v2 }">
	 		v1大于v2<br>
	 		
	 	</c:when>
	 	<c:otherwise>
	 		v1小于v2<br>
	 	</c:otherwise>
	 </c:choose>
	 
	 <c:choose>
	 	<c:when test="${empty userList }">
	 		没有符合条件的数据<br>
	 	</c:when>
	 	<c:otherwise>
	 		存在用户数据<br>
	 	</c:otherwise>
	 </c:choose>
	 
	 
	
</body>
</html>

JstlCoreServlet:

/**
 * 
 */
package com.bjpowrnode.jstl;

import java.io.IOException;
import java.util.ArrayList;

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

/**
 * @ClassName:JstlCoreServlet
 * @Description:TODO
 * @author wm
 * @date 2016年2月15日上午11:40:32
 */
public class JstlCoreServlet extends HttpServlet {

	protected void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		//普通字符串
		request.setAttribute("hello","Hello World!");
		request.setAttribute("welcome", "<font color='red'>欢迎你来到这个世界!</font>");
		
		//条件控制标签、
		request.setAttribute("v1", 10);
		request.setAttribute("v2", 20);
		
		request.setAttribute("userList", new ArrayList());
		request.getRequestDispatcher("/jstl_core.jsp").forward(request, response);
	}


}

 

原文地址:https://www.cnblogs.com/wangmei/p/5190681.html