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(<原始字符串>,<子字符串>)} 实例演 ...
随机推荐
- New Year Table(几何)
New Year Table Time Limit:2000MS Memory Limit:262144KB 64bit IO Format:%I64d & %I64u Sub ...
- 常调用的Webservice接口 集合
1. 查询手机:http://www.yodao.com/smartresult-xml/search.s?type=mobile&q=手机号码 2. 查询IP:http://www.yoda ...
- 划分树 poj2104 hdu5249
KPI Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total Submis ...
- 常用Git命令汇总
常用Git命令汇总 跟着R哥来到了新公司(一个从硬件向互联网转型中的公司),新公司以前的代码基本是使用SVN做版本控制,甚至有些代码没有做版本控制,所以R哥叫HG做了一次Git分享,准备把公司所有的代 ...
- .net批量删除和添加
往页面上拖一个GridView,设置好数据源,并为GridView添加一个模板列,往模板列里添加一个chekcbox,比如下面的代码 <asp:GridView ID="GridVie ...
- 公司项目笔记-导出excel
一.asp.net中导出Excel的方法: 在asp.net中导出Excel有两种方法,一种是将导出的文件存放在服务器某个文件夹下面,然后将文件地址输出在浏览器上:一种是将文件直接将文件输出流写给浏览 ...
- 注册界面设计及实现之(三)SharedPerferences实现数据暂存
开发步骤: 创建一个SharedPerferences接口对象,并使用其putString方法放入相关的公共数据 将验证通过的注册账号写入到该文件中 将数据进行提交 给出客户提示 //Register ...
- lucene.net 3.0.3、结合盘古分词进行搜索的小例子(转)
lucene.net 3.0.3.结合盘古分词进行搜索的小例子(分页功能) 添加:2013-12-25 更新:2013-12-26 新增分页功能. 更新:2013-12-27 新增按分类查询功能, ...
- protobuf 参考资料
Protocol Buffers 官网下载地址:https://developers.google.com/protocol-buffers/docs/downloads Protocol Buffe ...
- poj1458 求最长公共子序列 经典DP
Common Subsequence Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 45763 Accepted: 18 ...