Java Web:使用jstl标签

maven依赖

<dependency>
    <groupId>jstl</groupId>
    <artifactId>jstl</artifactId>
    <version>1.2</version>
</dependency>

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

常用标签:

foreach

<c:forEach items="${person[0] }" var="item" varStatus="status">
    ${status.count }: ${item.key }	
</c:forEach>

if

<c:if test="${title == 'this is title'}"> 
    ...
</c:if>

choose...when...otherwise

<c:choose> 
    <c:when test="${people.age < 18 }">
	    <td>未成年</td>
    </c:when>
    <c:when test="${people.age < 40 }">
        <td>青年</td>
    </c:when>
    <c:otherwise>
        <td>中老年</td>
    </c:otherwise>
</c:choose> 

set

<c:set var="i" value="7"></c:set>

out

<c:out value="${i }"></c:out>

假如i的值为字符串"<script>alert();</script>"c:out会对变量内容进行转义,输出为字符串;

若用EL表达式${i }直接出则不转义,会弹出对话框。

posted @ 2019-01-15 14:06  xuejianbest  阅读(631)  评论(0编辑  收藏  举报