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

这就需要用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标签比较枚举的更多相关文章
- JSP、JSTL标签、EL表达式
JSP.JSTL标签.EL表达式 1.EL表达式:${} 功能: 获取数据 执行运算 获取web开发的常用对象 2.JSP标签 例如: jsp标签还有很多功能,这里只列举出一种. <jsp:fo ...
- JSP的JSTL标签使用
JSTL标签和asp.net中的webform控件很像,但是功能确比asp.net的强很多. 配置过程,从最简单的项目开始: 1.下载JSTL标签库:http://archive.apache.org ...
- jsp之jstl标签
常用jstl标签 一.<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %> ...
- 【jsp】JSTL标签大全详解
一.JSTL标签介绍 1.什么是JSTL? JSTL是apache对EL表达式的扩展(也就是说JSTL依赖EL),JSTL是标签语言!JSTL标签使用以来非常方便,它与JSP动作标签一样,只不过它不是 ...
- jsp 添加jstl标签
jsp页面中添加下列代码即可使用jstl标签. <%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix=" ...
- jsp中jstl标签的类似 if - else 语句 的语法
在jsp中引入jstl的标签库和函数库 <%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c&q ...
- 转载:jsp中jstl标签的类似 if - else 语句 的语法
原文链接:http://www.cnblogs.com/wanggd/archive/2013/05/27/3101788.html 在jsp中引入jstl的标签库和函数库 <%@ taglib ...
- jsp页面,jstl标签中的数据在<%%>java中使用
可参考jsp的API隐式对象.. 这部分数据其实被保存在page域中,但jsp中如果使用java代码需要在特定的标签中<%%>,在这个标签中可使用的只有pageContext对象,所以可以 ...
- fn:indexOf()详解(jsp中JSTL标签库)
fn:indexOf()函数返回一个字符串中指定子串的位置. 语法 fn:indexOf()函数的语法如下: ${fn:indexOf(<原始字符串>,<子字符串>)} 实例演 ...
随机推荐
- 第04讲- Android项目目录结构分析
学习内容: 1. 认识R类(R.java)的作用 R.java是在建立项目时自动生成的,这个文件是只读模式,不能更改.R类中包含很多静态类,且静态类的名字都与res中的一个名字对应,即R ...
- wpf纯前台绑定
<Window x:Class="Example1.MainWindow" ... xmlns:local="clr-namespace:Example1" ...
- DoTween学习笔记(一)
DOTween是一个快速,高效,完全统一的类型安全的对象属性动画引擎,免费开源,大量的高级特性. DoTween兼容Unity4.5以上的版本,支持的平台: Win, Mac, Unity WebPl ...
- python高级编程之选择好名称:命名指南
# # -*- coding: utf-8 -*- # # python:2.x # __author__ = 'Administrator' #命名指南 #一组常用的命名规则可以被应用到变量,方法函 ...
- PPT去掉图片白色背景
双击图片,点击菜单栏“删除背景”,用矩形框选中想要的区域,然后将鼠标焦点移到图片外,单击鼠标即可.
- 黑马程序猿 IO流 ByteArrayInputStream与ByteArrayOutputStream
---------------------- ASP.Net+Unity开发..Net培训.期待与您交流! ---------------------- package cn.itcast.IO; i ...
- Tomcat 官网知识总结篇
Tomcat 官网知识总结一.Tomcat 基本介绍 1.关键目录 a) bin 该目录包含了启动.停止和启动其他的脚本,如startup.sh.shutdown.sh等; b) conf 配置文件和 ...
- Tomcat 常见问题篇
Tomcat 常见问题一.Tomcat常见问题 1.Tomcat web容器出现故障时,我们通过Tomcat自带的logs查看原因,以下错误提示都是源于logs 2.Connection refuse ...
- js的 new image()---转
创建一个Image对象:var a=new Image(); 定义Image对象的src: a.src=”xxx.gif”; 这样做就相当于给浏览器缓存了一张图片. 图像对象: 建立图像对象:图 ...
- studio中集成.so文件的两种方式
.so文件作为NDK的开发包,如果不进行依赖使用将会报错所以,需要.so的一定要配置 注:如果没有引用so文件,可能会在程序执行的时候加载类库失败,有类似如下的DEBUG提示: java.lan ...