JSP页面通过JSTL标签进行if-esle进行判断是否显示页面相关信息
目录
1.前提准备需求
2.JSP相关引入
3.值的获取
4.使用JSTL标签使用if-else进行判断
5.JSTL标签中< c:choose> 和 < c:when>的使用
1.前提准备需求
项目工程中需要有JSTL的两个jar包:jstl.jar 和 standard.jar。
2.JSP相关引入
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<%@ taglib prefix="fn" uri="http://java.sun.com/jsp/jstl/functions"%>
备注:这两句必须在JSP页面顶部引入.
需要的类的引入
<%@ page import="com.eaedit.web.bean.user.UserVo" %>
3.值的获取
<%
String username = null;
UserVo user = (UserVo)request.getSession().getAttribute("eaedit_user_session_key");
if(user != null) {
username = user.getName();
}
%>
4.使用JSTL标签使用if-else进行判断
方法1:通过直接用JSTL获取存入session中的值进行判断
<c:if test="${not empty eaedit_user_session_key}">
<a href="user/commnew.action" style="color: white;">发表文章</a>
<a href="user/personalDesk.action" style="color: white;">个人空间</a>
</c:if>
方法2:通过引入的Java值进行判断
<c:if test="<%=username!=null%>">
<a href="user/commnew.action" style="color: white;">发表文章</a>
<a href="user/personalDesk.action" style="color: white;">个人空间</a>
</c:if>
5.JSTL标签中< c:choose> 和 < c:when>的使用
< c:choose> 和 < c:when>的使用类似于Java中switch语句与when的使用
<c:choose>
<c:when test="<%=username!=null%>">
<img src="<%=request.getContextPath() %>/user/img/defualtPicture.png" id="userPhoto"/>
</c:when>
<c:when test="<%=username!="admin"%>">
<img src="<%=request.getContextPath() %>/user/img/userAdminPhoto.png" id="userPhoto"/>
</c:when>
<c:otherwise>
<img src="<%=request.getContextPath() %>/<%=userPhoto%>" id="userPhoto"/>
</c:otherwise>
</c:choose>