1

I have a drop down menu in my JSP and instead of hardcoding the values with text I would like to call constants from a class. Here is a snippet of my constants class called 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";

And here is my JSP Page:

<%@ 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>

When I execute this page I get blank values for the second dropdown labeled Division. I have copied down the portion from the JSP that represents division below:

<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>

I'm not sure exactly what I am missing. Please help me with this. Thanks in advance. Please let me know if I have provided enough information or if more is needed. Thanks again

Sonny
  • 321
  • 1
  • 3
  • 10

1 Answers1

0

Have you missed to import the class?

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

Create getter methods in the MasterDataConstants class corresponding to each constant.

For example as shown below. Do in the same way for others as well.

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()}

Please have a look at accessing constants in JSP (without scriptlet)

Community
  • 1
  • 1
Braj
  • 46,415
  • 5
  • 60
  • 76
  • I actually have both the import statement and the jsp:useBean id statement in my code already. I'm not sure if this statement is missing something: – Sonny May 26 '14 at 18:41
  • Look at my post again. – Braj May 26 '14 at 18:47