日向博客最近在優化,有這一樣一個小問題,我希望在下面的消息中心頁面,未讀的消息鏈接顯示藍色,已讀的消息顯示紅色:
這就需要用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對象~