JSP页面通过JSTL标签进行if-esle进行判断是否显示页面相关信息

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>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值