calling a constant in a JSP dropdown menu - java

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

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)

Related

parameters not printing in jsp

I have a web.xml that calls selectfoods.jsp first, the there is this form:
<form name="ingredientsform" method="post" action="table.jsp">
<select name="ingredients" multiple>
<option value="tofu">Tofu</option>
<option value="pepper">Pepper</option>
<option value="spaghetti">Spaghetti</option>
<option value="paprika">Paprika</option>
<option value="onion">Onion</option>
<option value="beef">Beef</option>
<option value="mushrooms">Mushrooms</option>
</select>
<input type="submit">
</form>
which forwards to table.jsp where I would like to print out the ingredients selected but no error appears just an empty page here is the relevant code in the table.jsp
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Table</title>
</head>
<body>
<table>
<thead> <td> <b> Products </b></td></thead>
<%
String items[] = (String[]) request.getAttribute("ingredients");
for (int i = 0; i < items.length; i++)
{
%>
<tr> <td> <% out.println(items[i]); %> </td> </tr>
<%
}
%>
</table>
</body>
</html>
I would recommend using servlet-JSP MVC model for developing an application.
With MVC it would be easy to make a separate view and business logic and also it is easy to handle also.
To get parameter from submitted form , request.getParameter() is used not request.getAttribute().
Here you have make multiple selections so you have to use request.getParameterValues() which will fetch all selected values.

How to dynamically change HTML input name

I'm creating a small shopping cart project where I get a list of products in a linked list from a servlet. Now, I am printing the values of the linkedlist in a table with an option for users to select the quantities they want.
Which ever has quantity selected should go on to the next page as an attribute
Here are few issues I am facing:
1: Since the list has more than 1 items, each item listed should have
input field. How to I dynamically change the input name for the
quantity which I can later use as an attribute.
2: The available quantity of items vary from product to product, how
to I set the max value of the quantity to the available stock.
3: If I can manage to get the values, how do I set all the attributes.
Is it in the for loop or outside the for loop?
Here is the code for the reference.Picture of what the page looks like
<%# page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!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>Insert title here</title>
</head>
<body>
<%-- <%# taglib uri="http://java.sun.com/jstl/core" prefix="c" %> --%>
<%#page import="shoppingcart.model.items.*,java.util.*" %>
<%List<ItemDetailsPojo> listp = (List<ItemDetailsPojo>) session.getAttribute("ItemsData");
%>
<%-- <%# taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %> --%>
<Center>
<h3 style="color: blue">Welcome To the World of Shopping</h3>
</Center>
<div align='right'>Logged in as:</div>
<div align="center">
<table border="10" cellpadding="5">
<caption>
<h2>List of Items</h2>
</caption>
<tr>
<th>ItemId</th>
<th>Name</th>
<th>Category</th>
<th>Price</th>
<th>Available</th>
<th>Quantity</th>
</tr>
<%for(int i=0;i<listp.size();i++){%>
<tr>
<td><%out.println(listp.get(i).getItemId());%></td>
<td><%out.println(listp.get(i).getItemName());%></td>
<td><%out.println(listp.get(i).getCategory());%></td>
<td><%out.println(listp.get(i).getPrice());%></td>
<td><%out.println(listp.get(i).getQuantity());%></td>
<% int number = listp.get(i).getQuantity(); %>
<td><input type="number" name="should dynamically change according to the size of the list" min="0" max="should change according the the quantity available"></td>
</tr>
<%} %>
</table>
</div>
</body>
</html>
Try this:
<td><input type="number" name="quantity<%=i%>" min="0" max="<%=listp.get(i).getQuantity()%>"></td>
and you need to set all the attributes outside the loop.
Edit your code like this
<%List<ItemDetailsPojo> listp = (List<ItemDetailsPojo>) session.getAttribute("ItemsData");
%>
<%-- <%# taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %> --%>
<Center>
<h3 style="color: blue">Welcome To the World of Shopping</h3>
</Center>
<div align='right'>Logged in as:</div>
<div align="center">
<table border="10" cellpadding="5">
<caption>
<h2>List of Items</h2>
</caption>
<tr>
<th>ItemId</th>
<th>Name</th>
<th>Category</th>
<th>Price</th>
<th>Available</th>
<th>Quantity</th>
</tr>
<%
String name;
for(int i=0;i<listp.size();i++){%>
<tr>
<td><%out.println(listp.get(i).getItemId());%></td>
<td><%out.println(listp.get(i).getItemName());%></td>
<td><%out.println(listp.get(i).getCategory());%></td>
<td><%out.println(listp.get(i).getPrice());%></td>
<td><%out.println(listp.get(i).getQuantity());%></td>
<% int number = listp.get(i).getQuantity();
name="quantity".concat(listp.get(i).getQuantity());
%>
<td><input type="number"name="<%=name%>" min="0" max="should change according the the quantity available"></td>
</tr>
<%} %>
</table>
</div>
</body>
</html>

Some other way to display data from sql table on jsp

I have used jstl to display particular data from table named status(all columns are string type) on found.jsp. In my table I have entered 1 and 0 under status column where 1 is for IN and 0 is for OUT.
found.jsp
<%#page contentType="text/html" pageEncoding="UTF-8"%>
<%# page import="java.io.*,java.util.*,java.sql.*"%>
<%# page import="javax.servlet.http.*,javax.servlet.*" %>
<%# taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<%# taglib uri="http://java.sun.com/jsp/jstl/sql" prefix="sql"%>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Found page</title>
<style>
header {
background-color:teal;
color:white;
text-align:center;
padding:5px;
}
section {
height:270px;
width:1050px;
float:right;
padding:87px;
}
footer {
background-color:black;
float:bottom;
color:white;
clear:both;
text-align:center;
padding:5px;
}
</style>
</head>
<body style="background-color:lightsteelblue;">
<header><h3>File Status!!</h3>
<br>
</header>
<font color="black">back</font>
<form action=" LogoutServlet" method="post">
<input type="submit" value="Logout" >
</form>
<form method="POST">
File Number:<input type="text" name="status" value="" size="20" />
<input type="submit" value="submit" name="submit" />
</form>
<br>
<section>
<sql:setDataSource var="snapshot" driver="com.mysql.jdbc.Driver"
url="jdbc:mysql://localhost/login"
user="root" password="root"/>
<sql:query dataSource="${snapshot}" var="result">
SELECT * from status where fname="${param.status}";
</sql:query>
<table border="1" width="100%">
<tr>
<th>File Number</th>
Below is the column where 1 and 0 are shown under
<th>File Status(IN=1 and OUT=0)</th>
<th>File Department</th>
<th>Date and Time</th>
</tr>
<c:forEach var="row" items="${result.rows}">
<tr>
<td><c:out value="${row.fname}"/></td>
value comes from here
<td><c:out value="${row.fstatus}"/></td>
<td><c:out value="${row.department}"/></td>
<td><c:out value="${row.datetime}"/></td>
</tr>
</c:forEach>
</table>
</section>
<footer>
Copyright 2016 NSIC. All right reserved.
</footer>
</body>
</html>
I don't want 1 and 0 to be shown instead i want IN and OUT to be shown under the column and I don't want to change the database table entery from 1 and 0 to IN and OUT Is there a way I can do this?
you can do it with <c:choose> tag
<c:choose>
<c:when test="${row.fstatus=='1'}">
IN
</c:when>
<c:otherwise>
OUT
</c:otherwise>
</c:choose>

struts resource bundle properties file not mapping some keys

I'm starting to study struts and I have a problem using resource properties file
some text on page is displayed as this:
???login.message???
???login.username???
???login.password???
but some other messages are correctly taken from properties file. I think that the propertis file is correctly configured but I'm missing something to display anything correctly.
the file ApplicationResources.properties
# Resources for Login Project
# Struts Validator Error Messages
# These two resources are used by Struts HTML tag library
# to format messages. In this case we make sure that errors
# are red so that they can be noticed.
errors.header=<font color="red">*
errors.footer=</font>
#errors associated with the Login page
error.username.required=username required.
error.password.required=password required
error.login.invalid=The system could not verify your username or password. Is your CAPS LOCK on? Please try again.
#login page text
login.title=this is a title
login.message=please log in
login.username=username:
login.password=password:
login.button.signon=Log In
#loggedin page text
loggedin.title=Login Project
loggedin.msg=Benvenuto, {0}. You are now logged in.
"error.login.invalid" is correctly displayed and "error.username.required" too
the login label not
this is my jsp page
<%# page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<%# taglib uri="http://java.sun.com/jstl/fmt" prefix="fmt" %>
<%# taglib uri="http://struts.apache.org/tags-html" prefix="html" %>
<html:html locale="true"/>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<fmt:bundle basename="ApplicationResources"/>
<title><fmt:message key="login.title"/></title>
</head>
<body>
<html:errors property="login"/>
<html:form action="login.do" focus="userName" >
<table align="center">
<tr align="center">
<td><H1><fmt:message key="login.message"/></H1></td>
</tr>
<tr align="center">
<td>
<table align="center">
<tr>
<td align="right">
<fmt:message key="login.username"/>
</td>
<td align="left">
<html:text property="userName"
size="15"
maxlength="15" />
<html:errors property="userName" />
</td>
</tr>
<tr>
<td align="right">
<fmt:message key="login.password"/>
</td>
<td align="left">
<html:password property="password"
size="15"
maxlength="15"
redisplay="false"/>
<html:errors property="password" />
</td>
</tr>
<tr>
<td colspan="2" align="center">
<html:submit>
<fmt:message key="login.button.signon"/>
</html:submit>
</td>
</tr>
</table>
</td>
</tr>
</table>
</html:form>
</body>
</html>
Can you help me ?
tkz
Your
<fmt:message ... />
tags need to be inside an
<fmt:bundle ... >
tag. Currently you are closing your bundle tag right away
<fmt:bundle basename="ApplicationResources"/>
Instead, open it
<fmt:bundle basename="ApplicationResources">
and close it
</fmt:bundle>
when you no longer need it, possibly at the end of your JSP. Nest your
<fmt:message key="login.title"/>
tags inside it.

Call external javascript function frm JSP

I'm brand new to the javascript. I'm trying to call a javaScript function from my jsp.Its not working. I was trying to debug it through bugzila. It saying "No Javascript on this page"
<%# page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4 /loose.dtd">
<html>
<script type="text/javascript" src="ajaxs.js"></script>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
</head>
<body>
<%
out.println("Bismillahir Rahmanir Rahim");
%>
<jsp:useBean id="numBean" class="beans.NumberBean">
<jsp:setProperty name="numBean" property="*" />
</jsp:useBean>
<form action="index.jsp" method="post">
<table>
<tr>
<td>Enter Integers:</td>
<td><input type="text" id="inputString" name="inputString"
value="<jsp:getProperty name="numBean" property="inputString" />"
/> </td>
<td>(Use semi-colon to separate number)</td>
</tr>
<tr>
<td>M - number of largest element to find.:</td>
<td>
<input type="text" id="nthHighest" name="nthHighest"
value="<jsp:getProperty name="numBean" property="nthHighest" />" />
</td>
</tr>
</table>
<input type="submit" value="Identify" onclick="ajaxFunction()" />
<div id="result"></div>
</form>
</body>
</html>
I'm trying to call "ajaxFunction()" on onclick event.
I included js file as following at the top:
<script type="text/javascript" src="ajaxs.js"></script>
can u pls help me to identify what I'm doing wrong here...
Thanks in advance
Try with including script tag with "src="ajaxs.js" (your current script that including "src="ajaxs.js") within head tag.The contents within head tag load at the loading page .
thanks....

Categories