Displaying string in list and taking key as input - java

This is my dropdown list
<select name="dropdown">
<option value="NoValue"><br /></option>
<c:forEach var="route_desc" items="${routes}">
<option>
<c:if test="${not empty route_desc}">
<c:out value="${route_desc}" />
</c:if>
</option>
</c:forEach>
</select>
In my class I took a list and then added one
routes.add(routeDetailsPk);
routes.add(route_description);
Now I want to display the route_description only but when a user selects a value i want routeDetailsPk to come as a value. How to do it ? Currently it shows all the values

This is the way through which we can display the value and select the key using map and jstl
<c:forEach var="route_desc" items="${routes}">
<option value="${route_desc.key}">
<c:if test="${not empty route_desc}">
<c:out value="${route_desc.value}" />
</c:if>
</option>
</c:forEach>

Related

Set drop down list's selected value according to the database retrieved

I am trying to do an update form in my jsp. I need help in setting the drop down list display to the value retrieved from database. So when the user wants to edit a particular row of data, they will just need to click on the update button corresponding to it and the data will be shown in the form.
I am able to use input type="text" name="expenseTitle" style="margin-left:12px" value="<%=rec.getString("expense_title")%>">to retrieve data to my textbox.
I am using this connection to connect and retrieve my database:
<%
Connection connect = null;
Statement s = null;
try {
Class.forName("com.mysql.jdbc.Driver");
connect = DriverManager.getConnection("jdbc:mysql://localhost:3306/asldb" + "?user=root&password=mysql");
s = connect.createStatement();
String id = request.getParameter("id");
String sql = "SELECT * FROM input_expense WHERE id = '" + id +"'";
ResultSet rec = s.executeQuery(sql);
if(rec != null) {
rec.next();
%>
I have tried several methods. Firstly, for this method, no matter which row I use, it always retrieve Yearly in drop down list:
<select id="LT_occurrenceDDL" class="LT_formDDL" name="expenseOccurrence">
<option value="-1">Select an option</option>
<option value="One-Time" selected="<%=rec.getString("payment_occurrence").equals("One-Time")%>">One-Time</option>
<option value="Daily" selected="<%=rec.getString("payment_occurrence").equals("Daily")%>">Daily</option>
<option value="Weekly" selected="<%=rec.getString("payment_occurrence").equals("Weekly")%>">Weekly</option>
<option value="Monthly" selected="<%=rec.getString("payment_occurrence").equals("Monthly")%>">Monthly</option>
<option value="Quarterly" selected="<%=rec.getString("payment_occurrence").equals("Quarterly")%>">Quarterly</option>
<option value="Yearly" selected="<%=rec.getString("payment_occurrence").equals("Yearly")%>">Yearly</option>
</select>
Secondly:
<select class="LT_formDDL" name="expenseCategory"">
<option value="-1">Select a category</option>
<option value="mortgage/rent" <%= (rec.getString("expense_category")=="Mortgage/Rent Payment"?"selected='selected'":"")%>>Mortgage/Rent Payment</option>
<option value="loan" <%= (rec.getString("expense_category")=="Loans"?"selected='selected'":"")%>>Loans</option>
<option value="insurance" <%= (rec.getString("expense_category")=="Insurance"?"selected='selected'":"")%>>Insurance</option>
<option value="utilities" <%= (rec.getString("expense_category")=="Utilities"?"selected='selected'":"")%>>Utilities</option>
<option value="groceries" <%= (rec.getString("expense_category")=="Groceries"?"selected='selected'":"")%>>Groceries</option>
<option value="food" <%= (rec.getString("expense_category")=="Food"?"selected='selected'":"")%>>Food</option>
<option value="clothing" <%= (rec.getString("expense_category")=="Clothing"?"selected='selected'":"")%>>Clothing</option>
<option value="entertainment" <%= (rec.getString("expense_category")=="Entertainment"?"selected='selected'":"")%>>Entertainment</option>
<option value="others" <%= (rec.getString("expense_category")=="Others"?"selected='selected'":"")%>>Others</option>
</select>
Thirdly, I have input jar and the taglib:
<select id="LT_occurrenceDDL"class="LT_formDDL" name="expenseOccurrence">
<option value="-1">Select an option</option>
<c:choose>
<c:when test='${rec.getString("payment_occurrence") == "One-Time"}'>
<option value="One-Time" selected>One-Time</option>
</c:when>
<c:otherwise>
<option value="One-Time">One-Time</option>
</c:otherwise>
</c:choose>
<c:choose>
<c:when test='${rec.getString("payment_occurrence") == "Daily"}'>
<option value="Daily" selected>Daily</option>
</c:when>
<c:otherwise>
<option value="Daily">Daily</option>
</c:otherwise>
</c:choose>
<c:choose>
<c:when test='${rec.getString("payment_occurrence") == "Weekly"}'>
<option value="Weekly" selected>Weekly</option>
</c:when>
<c:otherwise>
<option value="Weekly">Weekly</option>
</c:otherwise>
</c:choose>
<c:choose>
<c:when test='${rec.getString("payment_occurrence") == "Monthly"}'>
<option value="Monthly" selected>Monthly</option>
</c:when>
<c:otherwise>
<option value="Monthly">Monthly</option>
</c:otherwise>
</c:choose>
<c:choose>
<c:when test='${rec.getString("payment_occurrence") == "Quarterly"}'>
<option value="Quarterly" selected>Quarterly</option>
</c:when>
<c:otherwise>
<option value="Quarterly">Quarterly</option>
</c:otherwise>
</c:choose>
<c:choose>
<c:when test='${rec.getString("payment_occurrence") == "Yearly"}'>
<option value="Yearly" selected>Yearly</option>
</c:when>
<c:otherwise>
<option value="Yearly">Yearly</option>
</c:otherwise>
</c:choose>
</select>
Sorry the drop down list is different for some because I have a few drop down list in the form, thus I tried different once using different method.
As for me, you should create two .jsp pages to do what you want. First one will display all users you have with the update and create buttoms and second one will display user that you want to update (with current id). You can forward your request without updating of main page only using Ajax. For more information please look at this test project test
It seems You are only using the first row of resultset without iteration. You should iterate the resultset like this -
if(rec!=null){
while(rec.next()){
// your retrieval code
}
}
I think this might solve the problem

How to remove an element from any list object while iterating through <c:foreach> tag in select tag in JSP?

Sample code in JSP:
<select>
<option value="0">Select</option>
<c:forEach items="${list}" var="someList">
<option value="${someList.value}">${someList.displayText}</option>
</c:forEach>
</select>
list object coming from Spring controller stored in model object.
Now someList.value and someList.displayText both the values are the same.
Example:
[iphone,samsung,lenovo,motog,oneplus]
I want to remove iphone from this.
You can't remove the item in the c:forEach tag but you can use c:if tag to filter 'iphone' from the options.
<select>
<option value="0">Select</option>
<c:forEach items="${list}" var="someList">
<c:if test="${someList.value != 'iphone'}">
<option value="${someList.value}">${someList.displayText}</option>
</c:if>
</c:forEach>
</select>

How to retrive data from DB for a selected dropdown value using only JSTL?

guys i am working on web application in jsp and servlets .. in myjsp.jsp i have a list
List<String> sexList = (List<String>)request.getAttribute("sexList"); %>
and put it in cumbo box in html tag like this
<td>Gender:</td>
<td><select name="sex">
<%for(String i : sexList) { %>
<option value="<%=i%>"><%=i %></option><%}%>
</select>
</td>
but now i want to select the selected item from database using JSTL and i want to know how to return or get this selected item .. any help
You need to include it in the form and post it to servlet:
selection.jsp:
<form action="display.jsp" method="GET">
<select name="sex">
<c:forEach items="${sexList}" var="sex">
<option value="${sex}">${sex}</option>
</c:forEach>
</select>
<input type="submit" value="submit"/>
</form>
display.jsp
<c:set var="sex" value="${param.sex}" scope="page" /> //Gets the selected sex
<sql:query var="person" >
select * from Person where sex = ?
<sql:param value="${sex}" />
</sql:query>

retrieving value from a dynamic selection list

I am working on a jsp project where I have a dynamic selection list. The values in this list change according to the value selected in the 1st selection list.
Here's the code:
<script language="JavaScript" type="text/javascript">
function optionsChange(){
var service = document.getElementById("service").value;
if(service == 'GSM'){
document.getElementById("cdmaService").value= '';
document.getElementById("cdmaService").style.display = 'none';
document.getElementById("gsmService").style.display = 'block';
$('gsmService').attr('name', 'services');
}else if(service == 'CDMA'){
document.getElementById("gsmService").value= '';
document.getElementById("gsmService").style.display = 'none';
document.getElementById("cdmaService").style.display = 'block';
$('cdmaService').attr('name', 'services');
}
}
</script>
<select id="service" onChange="javascript:optionsChange();">
<option value="GSM">GSM</option>
<option value="CDMA">CDMA</option>
</select>
<td id="gsmService" ><select name="services" >
<option value="COMBO OFFER">COMBO OFFER</option>
<option value="CRICKET">CRICKET</option>
<option value="ASTRO">ASTRO</option>
</select>
</td>
<td id="cdmaService" style="display:none"><select name="services" >
<option value="COMBO OFFER CDMA">COMBO OFFER CDMA</option>
<option value="WIN THE DREAM">WIN THE DREAM</option>
<option value="VOICE CHAT">VOICE CHAT</option>
</select>
</td>
now when the user selects a service, lets say "GSM", and then selects a service from the second list, lets say "ASTRO". He clicks on a button which redirects him to the next page where he sees "ASTRO" printed. This works fine.
But if the user selects "CDMA" from the 1st list and then selects, let's say "VOICE CHAT" from the second list. It still prints "ASTRO" on the next page. IT should print "VOICE CHAT".
this is the method to submit form:
<script language=javascript>
function submitForm(actionStr)
{
if(actionStr=="User Details")
{
document.login.action="showUsrDetail.jsp";
document.login.submit();
}
}
this is the code for the button:
<input type="button" value="User Details" onclick="submitForm(this.value);"/>
then it redirects to the page ""showUsrDetail.jsp". And when it does the name of the service is printed on the console. For which the code is:
<%
String service = request.getParameter("services");
System.out.println("Value Added Service selected is ="+service);
%>
if i change the first selection to CDMA and then select any service from the second selection list, it still prints the Service which is under GSM.
Can somebody please help me out?
Since you are doing nothing in submitForm() function other that submitting the form that can be achieved directly by <input type="submit>" as shown in below sample.
<form action="showUsrDetail.jsp" id="login" method="post">
<!-- other fields -->
<input type="submit" value="User Details"/>
</form>
Solution 1
Use different names for all the select fields and check the values in showUsrDetail.jsp.
<form id="login" action="showUsrDetail.jsp" method="post">
<table>
<tr>
<td><select id="service" onChange="javascript:optionsChange();"
name="service">
<option value="GSM">GSM</option>
<option value="CDMA">CDMA</option>
</select></td>
<td id="gsmService"><select name="gsmService">
<option value="COMBO OFFER">COMBO OFFER</option>
<option value="CRICKET">CRICKET</option>
<option value="ASTRO">ASTRO</option>
</select></td>
<td id="cdmaService" style="display: none"><select
name="cdmaService">
<option value="COMBO OFFER CDMA">COMBO OFFER CDMA</option>
<option value="WIN THE DREAM">WIN THE DREAM</option>
<option value="VOICE CHAT">VOICE CHAT</option>
</select></td>
<td><input type="submit" value="User Details" /></td>
</tr>
</table>
</form>
showUsrDetail.jsp:
<c:if test="${param.service == 'GSM'}">
<c:out value="${param.gsmService}" />
</c:if>
<c:if test="${param.service == 'CDMA'}">
<c:out value="${param.cdmaService}" />
</c:if>
Solution 2
Add a hidden input field and update its value based on selection change in cdmaService and gsmService select item. Add change listener on both the select item.
<input type="hidden" name="serviceValue"/>
showUsrDetail.jsp:
<c:out value="${param.serviceValue}" />
Note:
I suggest you to use JavaServer Pages Standard Tag Library or Expression Language instead of Scriplet that is more easy to use and less error prone.

How to make an option selected from a dropdown list in jsp?

In my project. I want to populate the drop down list on a jsp from a database.
<select id="names" name="names"> <c:forEach items="${names}" var="names">
<option><c:out value="${names}"/></option>
</c:forEach>
</select>
The ${names} is a list of names from the database. I want to select an option dynamically in the drop down list. Suppose there are three names in the database Rohan, Dean, Justin. If Dean is logged, i want select the option Dean as selected.
I try a code like this but this does not work.
<option value="${names}" ${names == names ? 'selected' : ''}>${names}</option>
Try like this assuming that loggedInUser variable holds the String value of the currently logged in user.
<select id="names" name="names">
<c:forEach items="${names}" var="names">
<c:when test="${loggedInUser eq names}">
<option value ="<c:out value="${names}"/>" selected="selected">${names}</option>
</c:when>
<c:otherwise>
<option><c:out value="${names}"/></option>
</c:otherwise>
</c:forEach>

Categories