为什么在jsp页面上获取的常量值都为空?

为什么在jsp页面上获取的常量值都为空?

claim_voucher_deal.jsp

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ taglib prefix="spring" uri="http://www.springframework.org/tags" %>
<%@ page import="com.yuan.oa.global.Constant" %>
<jsp:include page="top.jsp"/>

<section id="content" class="table-layout animated fadeIn">
    <div class="tray tray-center">
        <div class="content-header">
            <h2> 待处理报销单 </h2>
            <p class="lead"></p>
        </div>
        <div class="admin-form theme-primary mw1000 center-block" style="padding-bottom: 175px;">
            <div class="panel  heading-border">
                <div class="panel-menu">
                    <div class="row">
                        <div class="hidden-xs hidden-sm col-md-3">
                            <div class="btn-group">
                                <button type="button" class="btn btn-default light">
                                    <i class="fa fa-refresh"></i>
                                </button>
                                <button type="button" class="btn btn-default light">
                                    <i class="fa fa-trash"></i>
                                </button>
                                <button type="button" class="btn btn-default light">
                                    <i class="fa fa-plus" onclick="javascript:window.location.href='/claim_voucher/to_add';"></i>
                                </button>
                            </div>
                        </div>
                        <div class="col-xs-12 col-md-9 text-right">
                            <div class="btn-group">
                                <button type="button" class="btn btn-default light">
                                    <i class="fa fa-chevron-left"></i>
                                </button>
                                <button type="button" class="btn btn-default light">
                                    <i class="fa fa-chevron-right"></i>
                                </button>
                            </div>
                        </div>
                    </div>
                </div>
                <div class="panel-body pn">
                    <table id="message-table" class="table admin-form theme-warning tc-checkbox-1">
                        <thead>
                        <tr class="">
                            <th class="text-center hidden-xs">Select</th>
                            <th class="hidden-xs">事由</th>
                            <th>状态</th>
                            <th class="hidden-xs">创建人</th>
                            <th class="hidden-xs">金额</th>
                            <th class="text-center">创建时间</th>
                            <th>操作</th>
                        </tr>
                        </thead>
                        <tbody>
                        <c:forEach items="${list}" var="cv">
                            <tr class="message-unread">
                                <td class="hidden-xs">
                                    <label class="option block mn">
                                        <input type="checkbox" name="mobileos" value="FR">
                                        <span class="checkbox mn"></span>
                                    </label>
                                </td>
                                <td>${cv.cause}</td>
                                <td class="hidden-xs">
                                    <span class="badge badge-warning mr10 fs11">${cv.status}</span>
                                </td>
                                <td>${cv.creater.name}</td>
                                <td class="text-center fw600">${cv.totalAmount}</td>
                                <td><spring:eval expression="cv.createTime"/></td>
                                <td>
                                    //为何这里的常量值都为空(如Constant.CLAIMVOUCHER_CREATED是个空字符串)
                                    <c:if test="${cv.status==Constant.CLAIMVOUCHER_CREATED || cv.status==Constant.CLAIMVOUCHER_BACK}">
                                        <a href="/claim_voucher/details?id=${cv.id}">修改</a>
                                        <a href="/claim_voucher/details?id=${cv.id}">提交</a>
                                    </c:if>
                                    <c:if test="${cv.status==Constant.CLAIMVOUCHER_SUBMIT || cv.status==Constant.CLAIMVOUCHER_RECHECK}">
                                        <a href="/claim_voucher/details?id=${cv.id}">打回</a>
                                        <a href="/claim_voucher/details?id=${cv.id}">审核</a>
                                    </c:if>
                                    <c:if test="${cv.status==Constant.CLAIMVOUCHER_APPROVED}">
                                        <a href="/claim_voucher/details?id=${cv.id}">打款</a>
                                    </c:if>
                                    <a href="/claim_voucher/details?id=${cv.id}">${Constant.CLAIMVOUCHER_CREATED}详细信息${cv.status}</a>
                                </td>
                            </tr>
                        </c:forEach>
                        </tbody>
                    </table>
                </div>
            </div>
        </div>
    </div>
</section>


<jsp:include page="bottom.jsp"/>

Constant.java

package com.yuan.oa.global;

import java.util.ArrayList;
import java.util.List;

//常量类
public class Constant {
    //职务
    public static final String POST_STAFF="员工";
    public static final String POST_FM="部门经理";
    public static final String POST_GM="总经理";
    public static final String POST_CASHIER="财务";
    //职务集合
    public static List<String> getPosts()
    {
        List<String> list=new ArrayList<String>();
        list.add(POST_STAFF);
        list.add(POST_FM);
        list.add(POST_GM);
        list.add(POST_CASHIER);
        return list;
    }

    //费用类型
    public static List<String> getItems()
    {
        List<String> list=new ArrayList<String>();
        list.add("交通");
        list.add("餐饮");
        list.add("住宿");
        list.add("办公");
        list.add("其他");
        return list;
    }

    //报销单状态(不要忘了加final)
    public static final String CLAIMVOUCHER_CREATED="新创建";
    public static final String CLAIMVOUCHER_SUBMIT="提交";
    public static final String CLAIMVOUCHER_APPROVED="已审核";
    public static final String CLAIMVOUCHER_BACK="已打回";
    public static final String CLAIMVOUCHER_TEAMINATED="已终止";
    public static final String CLAIMVOUCHER_RECHECK="待复审";
    public static final String CLAIMVOUCHER_PAID="已打款";

    //审核额度
    public static final double LIMIT_CHECK=5000;

    //处理方式(处理状态):不要忘了加final
    public static final String DEAL_CREATED="创建";
    public static final String DEAL_SUBMIT="提交";
    public static final String DEAL_UPDATE="修改";
    public static final String DEAL_BACK="打回";
    public static final String DEAL_REJECT="拒绝";
    public static final String DEAL_PASS="通过";
    public static final String DEAL_PAID="打款";




}


正在回答

登陆购买课程后可参与讨论,去登陆

5回答

你的现象好诡异,我测了一下也是能正常显示的。。。。我的是tomcat8.5 , jdk1.8

  • 源自我心 提问者 #1
    是的,我觉得也很奇怪,关键我跑老师的源码竟然也是一样的结果,要说和你有什么不同的话就是我用的是Tomcat7
    2018-05-26 11:56:37
  • 一叶知秋519 回复 提问者 源自我心 #2
    好吧,,,同学换个Tomcat再试下呢~~
    2018-05-27 20:12:00
  • 源自我心 提问者 #3
    谢谢同学了!还真是Tomcat版本的问题!多谢了!
    2018-05-31 18:09:42
提问者 源自我心 2018-05-24 18:53:18
${Constant.CLAIMVOUCHER_CREATED}
<c:out value="${Constant.CLAIMVOUCHER_CREATED}"/>

我使用了这两种方法还是没有取出常量值,显示的依然为空。

我这里的把常量值直接改成字符串,相应的这些链接是可以显示的,比如

<td>
    <c:out value="${Constant.CLAIMVOUCHER_CREATED}"/>
    <c:if test="${cv.status=='新创建' || cv.status=='已打回'}">
        <a href="/claim_voucher/to_update?id=${cv.id}">修改</a>
        <a href="/claim_voucher/submit?id=${cv.id}">提交</a>
    </c:if>
    <c:if test="${cv.status=='已提交' || cv.status=='待复审'}">
        <a href="/claim_voucher/to_check?id=${cv.id}">审核</a>
    </c:if>
    <c:if test="${cv.status=='已审核'}">
        <a href="/claim_voucher/to_check?id=${cv.id}">打款</a>
    </c:if>
    <a href="/claim_voucher/details?id=${cv.id}">详细信息</a>
</td>

但是使用常量值就不行了

  • 你在你的环境中运行一下老师的源码,看下常量的值可以取到吗? 如果还有问题,可以再次提问~~
    2018-05-24 19:12:44
  • 提问者 源自我心 回复 一叶知秋519 #2
    的确有点诡异,我直接跑了下老师的源码,竟然也取不到常量值......
    2018-05-24 23:51:33
  • 一叶知秋519 回复 提问者 源自我心 #3
    额,,如果同学不是用的JDK1.8,建议同学使用JDK1.8试下~~
    2018-05-25 09:55:45
一叶知秋519 2018-05-24 18:36:22

嗯,,我将你的代码贴到源码中,常量的值是可以取出来的;

http://img1.sycdn.imooc.com//climg/5b0695350001a11a07790368.jpg

页面的显示结果:

http://img1.sycdn.imooc.com//climg/5b06954a0001fc9c08860123.jpg

通过jstl的out标签,可以把常量的值取出来;

同学说的获取常量值是空,是怎么获取的呢?

同学可以把具体现象再描述一下,方便帮助你解答~

祝学习愉快!

提问者 源自我心 2018-05-24 17:40:56

和常量相关的源代码不是就只有这两个文件吗?请问还有什么代码和常量有关?

一叶知秋519 2018-05-23 19:17:13

我将你的代码贴到老师的源码中,是可以显示的,建议同学将涉及到的代码都贴一下,贴到回答里并进行格式化且说明文件名,方便我们运行你的代码帮助你解答~

问题已解决,确定采纳
还有疑问,暂不采纳

恭喜解决一个难题,获得1积分~

来为老师/同学的回答评分吧

0 星
SSM主流框架入门与综合项目实战2018版
  • 参与学习           人
  • 提交作业       205    份
  • 解答问题       4317    个

Java中非常实用的SSM整合开发内容,从Spring开始,到MyBaits的进阶内容,再到SpringMVC的应用,最后是SSM整合开发案例,逐步深入,助你成长为一名Java工程师!

了解课程
请稍等 ...
意见反馈 帮助中心 APP下载
官方微信

在线咨询

领取优惠

免费试听

领取大纲

扫描二维码,添加
你的专属老师