JSTL常用写法
程序员文章站
2022-03-21 20:33:32
...
获取session的值
获取application的值
//init-method方法给赋值
servletContext.setAttribute("config",map);
//jsp获取值
${applicationScope.config.picture_URL}
获取list信息
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<%@ taglib prefix="fn" uri="http://java.sun.com/jsp/jstl/functions" %>
<c:forEach items="${productLists }" var="tcList" varStatus="status">
<!-- 获取长度 -->
<c:out value="${fn:length(productLists)}"></c:out>
<c:if test="${fn:length(productLists)>0}">
<!-- 获取index -->
${status.index }
获取request信息
${pageContext.request} |取得请求对象<br>
${pageContext.session} |取得session对象<br>
${pageContext.request.queryString} |取得请求的参数字符串<br>
${pageContext.request.requestURL} |取得请求的URL,但不包括请求之参数字符串<br>
${pageContext.request.contextPath} |服务的web application的名称<br>
${pageContext.request.method} |取得HTTP的方法(GET、POST)<br>
${pageContext.request.protocol} |取得使用的协议(HTTP/1.1、HTTP/1.0)<br>
${pageContext.request.remoteUser} |取得用户名称<br>
${pageContext.session.new} |判断session是否为新的,所谓新的session,表示刚由server产生而client尚未使用<br>
${pageContext.session.id} |取得session的ID<br>
${header["User-Agent"]}|用户浏览器的版本<br/>
${header["Host"]}|IP<br/>
${pageContext.request.remoteAddr } |取得用户的IP地址<br>
${pageContext.servletContext.serverInfo}|取得主机端的服务信息<br>
${pageContext.request.serverPort}|端口信息<br>
${pageContext.request.serverName}|服务器名称<br>
${pageContext.request.remoteHost}|客户机名称<br>
多级嵌套下if判断
<c:forEach items="${cmsCategory }" var="category">
<div class="pubfcon_fir">
<a href="${fileUrlConfig.contextPath}news/753.html">${category.categoryName }</a>
<ul>
<c:forEach items="${cmsArtical }" var="cms">
<c:if test="${category.categoryid eq cms.categoryid }">
<li>
<a target="_blank" href="${fileUrlConfig.contextPath}news/753.html">${cms.title }</a>
</li>
</c:if>
</c:forEach>
</ul>
</div>
</c:forEach>
jstl的if…else
<c:choose>
<c:when test="${cms.outUrl == null }">
<a target="_blank" href="${fileUrlConfig.contextPath}news/${cms.articleId}.html">${cms.title }</a>
</c:when>
<c:otherwise>
<a target="_blank" href="${cms.outUrl }">${cms.title }</a>
</c: otherwise>
</c:choose>