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对象~


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM