在JSP下拉菜单中调用常量

时间:2014-05-26 18:28:50

标签: java html jsp constants

我的JSP中有一个下拉菜单,而不是用文本对值进行硬编码,我想从类中调用常量。这是我的常量类的片段,名为master.dao.util.MasterDataConstants

//DIVISIONS FOR DROPDOWN
    public static final String DIVISION_TYPE_DROPDOWN_AUDIT_MANAGEMENT_GLOBAL_ID = "Audit Management - Global";
    public static final String DIVISION_TYPE_DROPDOWN_CHANGE_MANAGEMENT_GLOBAL_ID = "Change Management - Global";
    public static final String DIVISION_TYPE_DROPDOWN_DEA_MANAGEMENT_GLOBAL_ID = "DEA Management - Global";
    public static final String DIVISION_TYPE_DROPDOWN_EHS_MANAGEMENT_GLOBAL_ID = "EH&S Management - Global";
    public static final String DIVISION_TYPE_DROPDOWN_EVENT_MANAGEMENT_GLOBAL_ID = "Event Management - Global";

这是我的JSP页面:

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>    
<%@ page import="java.sql.*"%> 
<%@ page import="java.io.*"%>   
<%@ page import="java.util.*"%>
<%@ page import="javax.servlet.*"%>
<%@ page import="master.dao.MasterDataDao"%>
**<%@ page import="master.dao.util.MasterDataConstants"%>**
<%@ page import="master.dto.SiteDto"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"     "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Master Data</title>
</head>
<script>
</script>
<body>

<form name="input" action="getMasterData" method="get">



        <br />
        <br />
        <h1 align='center'>Master Data File</h1>
        <br />
        <br />


        <table border="0" align='center'>
            <tr>
                <td>
                    <h2>Site Name</h2>
                </td>
                <td align='left'>
                <jsp:useBean id="masterDao" clas    s="master.dao.MasterDataDao"/>
                <select name="siteId" id="siteId">
                <option value="0">ALL</option>
                 <c:forEach items="${masterDao.allSites}" var="siteDto">
                 <option value="${siteDto.id}">${siteDto.name}</option>
                </c:forEach>
                </select></td>
            </tr>
            <tr>
                <td>
                    **<h2>Division</h2>
                </td>
                <td align='left'>
                <jsp:useBean id="masterDaoUtil"     class="master.dao.util.MasterDataConstants"/>
                <select name="divisionId" id="divisionId">
                <option value="33">${MasterDataConstants.DIVISION_TYPE_DROPDOWN_AUDIT_MANAGEMENT_GLOBAL_ID}    </option>
                <option value="31">${MasterDataConstants.DIVISION_TYPE_DROPDOWN_CHANGE_MANAGEMENT_GLOBAL_ID}    </option>
                <option value="34">${MasterDataConstants.DIVISION_TYPE_DROPDOWN_DEA_MANAGEMENT_GLOBAL_ID}</option>
                <option value="35">${MasterDataConstants.DIVISION_TYPE_DROPDOWN_EHS_MANAGEMENT_GLOBAL_ID}</option>
                <option value="23">${MasterDataConstants.DIVISION_TYPE_DROPDOWN_EVENT_MANAGEMENT_GLOBAL_ID}</option>**
                </select></td>
            </tr>

        </table>
        <br />
        <br />
        <div style="text-align: center">
            <input type="submit" value="Submit">
        </div>

    </form>
</body>
</html>

当我执行此页面时,我得到标记为Division的第二个下拉列表的空白值。我已经从JSP中复制了代表以下部门的部分:

<td>
                <h2>Division</h2>
            </td>
            <td align='left'>
            <jsp:useBean id="masterDaoUtil" class="master.dao.util.MasterDataConstants"/>
            <select name="divisionId" id="divisionId">
            <option value="33">${MasterDataConstants.DIVISION_TYPE_DROPDOWN_AUDIT_MANAGEMENT_GLOBAL_ID}</option>
            <option value="31">${MasterDataConstants.DIVISION_TYPE_DROPDOWN_CHANGE_MANAGEMENT_GLOBAL_ID}</option>
            <option value="34">${MasterDataConstants.DIVISION_TYPE_DROPDOWN_DEA_MANAGEMENT_GLOBAL_ID}</option>
            <option value="35">${MasterDataConstants.DIVISION_TYPE_DROPDOWN_EHS_MANAGEMENT_GLOBAL_ID}</option>
            <option value="23">${MasterDataConstants.DIVISION_TYPE_DROPDOWN_EVENT_MANAGEMENT_GLOBAL_ID}</option>
            </select></td>

我不确定我到底错过了什么。请帮我解决一下这个。提前致谢。如果我提供了足够的信息或者需要更多信息,请告诉我。 再次感谢

1 个答案:

答案 0 :(得分:0)

你错过了导入课程吗?

<%@ page import="master.dao.util.MasterDataConstants" %>

MasterDataConstants类中创建与每个常量对应的getter方法。

例如如下所示。也以同样的方式为他人做。

MasterDataConstants.java

public static final String DIVISION_TYPE_DROPDOWN_AUDIT_MANAGEMENT_GLOBAL_ID = "Audit Management - Global";

public String getDIVISION_TYPE_DROPDOWN_AUDIT_MANAGEMENT_GLOBAL_ID() {
    return DIVISION_TYPE_DROPDOWN_AUDIT_MANAGEMENT_GLOBAL_ID;
}

JSP:

${masterDaoUtil.getDIVISION_TYPE_DROPDOWN_AUDIT_MANAGEMENT_GLOBAL_ID()}

请查看accessing constants in JSP (without scriptlet)