日向博客最近在优化,有这一样一个小问题,我希望在下面的消息中心页面,未读的消息链接显示蓝色,已读的消息显示红色:

这就需要用jstl做一个判断。

之前的代码是这种形式:

                消息中心:<br>
<c:forEach items="${msgPage.content}" begin="0" end="15" step="1" var="msg">
<!-- 如果消息类型为评论 -->
<div id="msg_line">
<c:choose>
<c:when test="${empty msg.sender.username}">
一名游客在${fn:substring(msg.date,0,16)}评论了你的文章<a href="/RiXiang_blog/msg/show.form?id=${msg.id}">${msg.article.title}</a><br>
</c:when>
<c:otherwise>
用户 ${msg.sender.username}在${fn:substring(msg.date,0,16)}评论了你的文章<a href="/RiXiang_blog/msg/show.form?id=${msg.id}">${msg.article.title}</a><br>
</c:otherwise>
</c:choose>
</div>
</c:forEach>

msg.content是后台传来的Message对象的List。遍历显示在页面。

Message实体类的代码:

package sonn.entity;

import java.util.Date;

import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.OneToOne; import sonn.enums.MsgIsRead;
import sonn.enums.MsgType; /**
* @ClassName: Message
* @Description: entity class of message
* @author sonne
* @date 2016-12-23 20:32:16
* @version 1.0
*/
@Entity
public class Message { /*id*/
@Id
@GeneratedValue(strategy=GenerationType.IDENTITY)
private int id; private String content; @OneToOne(fetch = FetchType.EAGER)
private User sender; @OneToOne(fetch = FetchType.EAGER)
private User reciever; private MsgType type; private MsgIsRead is_read; private Date date; @OneToOne(fetch = FetchType.EAGER)
private Article article; public int getId() {
return id;
} public void setId(int id) {
this.id = id;
} public String getContent() {
return content;
} public void setContent(String content) {
this.content = content;
} public User getSender() {
return sender;
} public void setSender(User sender) {
this.sender = sender;
} public User getReciever() {
return reciever;
} public void setReciever(User reciever) {
this.reciever = reciever;
} public MsgType getType() {
return type;
} public void setType(MsgType type) {
this.type = type;
} public Date getDate() {
return date;
} public void setDate(Date date) {
this.date = date;
} public Article getArticle() {
return article;
} public void setArticle(Article article) {
this.article = article;
} public MsgIsRead getIs_read() {
return is_read;
} public void setIs_read(MsgIsRead is_read) {
this.is_read = is_read;
} }

我需要的是根据enum字段is_read来判断是否已读。

package sonn.enums;

public enum MsgIsRead {
Yes, No;
}

于是我最初写下下面的代码:

<%@ page import="sonn.enums.MsgIsRead" %>

.............................

               <c:choose>
<c:when test="${empty msg.sender.username}">
<c:choose>
<c:when test="${msg.is_read eq MsgIsRead.No}">
一名游客在${fn:substring(msg.date,0,16)}评论了你的文章<a style="color:red" href="/RiXiang_blog/msg/show.form?id=${msg.id}">${msg.article.title}</a><br>
</c:when>
<c:otherwise>
一名游客在${fn:substring(msg.date,0,16)}评论了你的文章<a href="/RiXiang_blog/msg/show.form?id=${msg.id}">${msg.article.title}</a><br>
</c:otherwise>
</c:choose>
</c:when>
<c:otherwise>
<c:choose>
<c:when test="${msg.is_read eq MsgIsRead.No}">
用户 ${msg.sender.username}在${fn:substring(msg.date,0,16)}评论了你的文章<a style="color:red" href="/RiXiang_blog/msg/show.form?id=${msg.id}">${msg.article.title}</a><br>
</c:when>
<c:otherwise>
用户 ${msg.sender.username}在${fn:substring(msg.date,0,16)}评论了你的文章<a href="/RiXiang_blog/msg/show.form?id=${msg.id}">${msg.article.title}</a><br>
</c:otherwise>
</c:choose>
</c:otherwise>
</c:choose>

上面代码是没有效果的,我以为页面最上方引入了<%@ page import="sonn.enums.MsgIsRead" %>的话,jstl标签就可以获取这个enum对象了。

实际上,jstl标签获取变量的范围只是pageScope, sessionScope, requestScope, applicationScope……

所以,为了获取这个enum需要在判断前先set一下:

<%@ page import="sonn.enums.MsgIsRead" %>
..........................................................................................
消息中心:<br>
<c:set var="Not_Read" value="<%=MsgIsRead.No%>"/>
<c:forEach items="${msgPage.content}" begin="0" end="15" step="1" var="msg">
<!-- 如果消息类型为评论 -->
<div id="msg_line">
<c:choose>
<c:when test="${empty msg.sender.username}">
<c:choose>
<c:when test="${msg.is_read eq Not_Read}">
一名游客在${fn:substring(msg.date,0,16)}评论了你的文章<a style="color:red" href="/RiXiang_blog/msg/show.form?id=${msg.id}">${msg.article.title}</a><br>
</c:when>
<c:otherwise>
一名游客在${fn:substring(msg.date,0,16)}评论了你的文章<a href="/RiXiang_blog/msg/show.form?id=${msg.id}">${msg.article.title}</a><br>
</c:otherwise>
</c:choose>
</c:when>
<c:otherwise>
<c:choose>
<c:when test="${msg.is_read eq Not_Read}">
用户 ${msg.sender.username}在${fn:substring(msg.date,0,16)}评论了你的文章<a style="color:red" href="/RiXiang_blog/msg/show.form?id=${msg.id}">${msg.article.title}</a><br>
</c:when>
<c:otherwise>
用户 ${msg.sender.username}在${fn:substring(msg.date,0,16)}评论了你的文章<a href="/RiXiang_blog/msg/show.form?id=${msg.id}">${msg.article.title}</a><br>
</c:otherwise>
</c:choose>
</c:otherwise>
</c:choose>
</div>
</c:forEach>

<c:set var="Not_Read" value="<%=MsgIsRead.No%>"/>先这样写,jstl才能获取到该java对象~

jsp用jstl标签比较枚举的更多相关文章

  1. JSP、JSTL标签、EL表达式

    JSP.JSTL标签.EL表达式 1.EL表达式:${} 功能: 获取数据 执行运算 获取web开发的常用对象 2.JSP标签 例如: jsp标签还有很多功能,这里只列举出一种. <jsp:fo ...

  2. JSP的JSTL标签使用

    JSTL标签和asp.net中的webform控件很像,但是功能确比asp.net的强很多. 配置过程,从最简单的项目开始: 1.下载JSTL标签库:http://archive.apache.org ...

  3. jsp之jstl标签

    常用jstl标签 一.<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %> ...

  4. 【jsp】JSTL标签大全详解

    一.JSTL标签介绍 1.什么是JSTL? JSTL是apache对EL表达式的扩展(也就是说JSTL依赖EL),JSTL是标签语言!JSTL标签使用以来非常方便,它与JSP动作标签一样,只不过它不是 ...

  5. jsp 添加jstl标签

    jsp页面中添加下列代码即可使用jstl标签. <%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix=" ...

  6. jsp中jstl标签的类似 if - else 语句 的语法

    在jsp中引入jstl的标签库和函数库 <%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c&q ...

  7. 转载:jsp中jstl标签的类似 if - else 语句 的语法

    原文链接:http://www.cnblogs.com/wanggd/archive/2013/05/27/3101788.html 在jsp中引入jstl的标签库和函数库 <%@ taglib ...

  8. jsp页面,jstl标签中的数据在<%%>java中使用

    可参考jsp的API隐式对象.. 这部分数据其实被保存在page域中,但jsp中如果使用java代码需要在特定的标签中<%%>,在这个标签中可使用的只有pageContext对象,所以可以 ...

  9. fn:indexOf()详解(jsp中JSTL标签库)

    fn:indexOf()函数返回一个字符串中指定子串的位置. 语法 fn:indexOf()函数的语法如下: ${fn:indexOf(<原始字符串>,<子字符串>)} 实例演 ...

随机推荐

  1. python高级编程之元类(第3部分结束)

    # -*- coding: utf-8 -*- # python:2.x __author__ = 'Administrator' #元编程 #new-style类带来了一种能力,通过2个特殊方法(_ ...

  2. xcode6和ios 8 百度无法定位解决

    . @interface里: CLLocationManager *locationManager; . 初始化: locationManager = [[CLLocationManager allo ...

  3. 高性能WEB开发(6) - web性能測试工具推荐

    WEB性能測试工具主要分为三种.一种是測试页面资源载入速度的,一种是測试页面载入完成后页面呈现.JS操作速度的,另一种是整体上对页面进行评价分析,以下分别对这些工具进行介绍,假设谁有更好的工具也请一起 ...

  4. cocos2dx 3.0 研究(4)渲染分析

    http://blog.csdn.net/epeaktop/article/details/26730909中已经说明了程序的设计,让我们来看看在cocos2dx 3.0中代码是怎样实现的. void ...

  5. 基于MapReduce的SimRank++算法研究与实现

    一.算法应用背景 计算广告学(Computational Advertising)是一门广告营销科学,以追求广告投放的收益最大化为目标,重点解决用户与广告匹配的相关性和广告的竞价模型问题,涉及到自然语 ...

  6. Filter简单介绍

    一.简单介绍 Filter也称为过滤器,WEB开发者通过Filter技术.对webserver管理的全部web资源:比如Jsp, Servlet, 静态图片文件或静态 html 文件等进行拦截.从而实 ...

  7. td 单元格 内容自动换行

    <table width="100%" border="1" align="center"> <tr> <td ...

  8. 设置改变oracle字符集

      修改过密码之后就能以dba的身份进行修改了,不是dba的话在执行修改命令的时候会提示你权限不足. 开始-->运行-->cmd,之后输入:"sqlplus sys/oracle ...

  9. 我跟着siki学Unity3D游戏开发——PongGame

    一.屏幕坐标转换为世界坐标. 1.游戏逻辑,根据界面布局,将墙体控制到对应的位置: vector3 position=Camer.main.ScreenToWorldPoint(new vetor2( ...

  10. web验证码

    前台引用.aspx: <%@ Page Language="C#" AutoEventWireup="true" CodeFile="YanZh ...