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

这就需要用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. [转] Hive 内置函数

    原文见:https://cwiki.apache.org/confluence/display/Hive/LanguageManual+UDF 1.内置运算符1.1关系运算符 运算符 类型 说明 A ...

  2. poj 3669 Meteor Shower(bfs)

    Description Bessie hears that an extraordinary meteor shower is coming; reports say that these meteo ...

  3. Vlc for Android 全面阐述

    简单介绍 Vlc for android是一款开源安卓播放器.具备播放多媒体文件.光盘.设备以及网络流媒体协议等功能,支持ARMv7 CPU或一个x86 CPU的设备,全部播放控制特性都已经开发完整. ...

  4. Android应用程序绑定服务(bindService)的过程源代码分析

    文章转载至CSDN社区罗升阳的安卓之旅,原文地址:http://blog.csdn.net/luoshengyang/article/details/6745181 Android应用程序组件Serv ...

  5. UIProgressView-初识IOS

    好几天没更新了,学的时候太紧,没时间复习了都.今天刚好有时间,多更几个. 今天复习的是UIProgressView,我们常见使用在修改某些属性的时候经常用到,比如透明度,今天我们介绍一个简单的使用例子 ...

  6. HTTP协议2之基本认证--转

    http协议是无状态的, 浏览器和web服务器之间可以通过cookie来身份识别. 桌面应用程序(比如新浪桌面客户端)跟Web服务器之间是如何身份识别呢? 什么是HTTP基本认证 桌面应用程序也通过H ...

  7. jquery简单切换插件

    <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <meta http ...

  8. FZU1327 优先队列

    Problem 1327 Blocks of Stones II Accept: 318    Submit: 881Time Limit: 1000 mSec    Memory Limit : 3 ...

  9. Linux系统的组成和内核的组成

    关于linux的组成的宏观认识. 组成图: 内核组成: 一个完整的Linux内核一般由5部分组成,它们分别是内存管理.进程管理.进程间通信.虚拟文件系统和网络接口.

  10. (原)caffe在ubuntu中设置GPU的ID号及使用多个GPU

    转载请注明出处: http://www.cnblogs.com/darkknightzh/p/5884892.html 参考网址: http://caffe.berkeleyvision.org/tu ...