My Java code,
public ModelAndView getDefault(ModelAndView mav,#RequestParam(value = "QueryCode",
required = false, defaultValue = "207") String QueryCode) {
List<String> list= columnService.getColumnName(QueryCode);
mav.addObject("columnList",list);
return mav;
}
I tried something like this in JSP,
<c:set var="jspList" value="${columnList}" />
<%
for(int i = 0; i < jspList.length; i++){
%>
<input type="checkbox" name="<%= jspList[i]%>"><br/>
<%
}
%>
but it didn't work and I am getting error like,
cannot resolve symbol jspList.
You can use c:forEach:
<c:forEach items="${columnList}" var="item">
<input type="checkbox" name="${item}">
</c:forEach>
Related
I have dropdown with options and the values. I can get the option value by the dropdown name in servlet but how can i get the dropdown "value" in servlet. In screenshot, temporarily i concatenated the with options but i want to store value in variable in servlet.
Please help:
HTML:
<input type="text" name="taxiDropdown" id= "taxiDropdown" placeholder="Search taxi...">
</div>
<div class="scrolling menu">
<%
List eList = (ArrayList) session.getAttribute("taxiInfo");
%>
<%
for (int i = 0; i < eList.size(); i++) {
%>
<div class="item" data-value="<%=((TaxiInfo) eList.get(i)).getID()%>">
<div class="ui green empty circular label"></div>
<%=((TaxiInfo) eList.get(i)).getTaxiPlate() +" "+ ((TaxiInfo) eList.get(i)).getID() %>
</div>
<%
}
%>
</div>
</div>
Servlet:
String val = request.getParameter("taxiDropdown");
(in "val", I want to store the value of the dropdown not the option text)
In JSP you should have something like that:
<form method="post">
<select name="taxiDropdown" id="taxiDropdown">
<%
List<TaxiInfo> eList = (List<TaxiInfo>) request.getAttribute("taxiInfo");
for (TaxiInfo taxiInfo : eList) {
%>
<option name="<%=taxiInfo.getTaxiPlate()%>" value="<%=taxiInfo.getID()%>"><%=taxiInfo.getTaxiPlate()%></option>
<%
}
%>
</select>
<input type="submit" />
</form>
Then in controller/servlet you will receive the id of TaxiInfo:
String val = request.getParameter("taxiDropdown");
System.out.println(val);
Or in your case you should set a hidden input with javascript with desired value.
added this code in html:
Move value of the dropdown selection to hidden textbox
<script type='text/javascript'>
$(function() {
$('#driverdp').change(function() { <-- this is my dropdown -->
var x = $(this).val();
$('#driverid').val(x); <-- this is my textbox -->
});
});
</script>
Servlet:
get the value of hidden text in servlet
String text= request.getParameter("driverId");
hope it will help someone
I'm trying to pass an arry into a new HashMap and then pass it into a jsp file. Problem is that it doesn't seem to be working. There is no output of my array on the jsp display. This is someof the code for the HashMap declaration.
Map<String, Object> params = new HashMap<String, Object>();
for (int x = 0; x < nl3.getLength(); x++){
Node currentItem3 = nl3.item(x);
String key3 = currentItem3.getAttributes().getNamedItem("name").getNodeValue();
if (requirement.equals(key3)) {
matchingID[x] = build;
params.put("message", matchingID[x]);}}
My jsp file looks like this
<%# include file="/include.jsp" %>
<bs:page>
<p>Please Input an Agent-Requirement</p>
<form method="get" action="/QueueStat.html">
Agent Requirement: <input type ="text" id='agentPram' name="requirement" value="${requirement}"/> <br/>
<input type="submit" value="Submit" />
List of Matching builds:
<c:forEach items="${mathcingId}" var="mathcingId">
<br><span class="mono mono-12px"><c:out value="${message}"/></span>
</c:forEach>
</jsp:attribute>
</bs:page>
Any ideas on what I'm doing wrong? I suspect that the prams.put might be incorrect. Or maybe the jsp file?
I am assigning an integer converted into string value to a hidden input through a loop where s1 is the loop iterator.
<% int k=0;
String s1 = null;
while(mar.next()){
k++;
s1 = Integer.toString(k);%>
<table>
<tr><td><b>User Name </b> </td> <td><%= mar.getString("UserName") %></td></tr>
<tr><td><b>Type </b></td><td> <%= mar.getString("Type") %></td></tr>
<tr><td><b>Complain </b></td> <td><%= mar.getString("Complaint") %></td></tr>
<tr><td><b>Status </b> </td> <td><%= mar.getString("Status") %></td></tr>
<tr><td><b>Date </b> </td> <td><%= mar.getString("Date") %></td></tr>
</table>
<form action = "demo1.jsp" method = "post">
<% out.println(mar.getString("Id")); %>
<input type="radio" name = "<%= mar.getString("Id") %>"value="pending"checked>Pending
<input type="radio" name = "<%= mar.getString("Id") %>" value="done">Done
<input type="submit" name = "B" id = "submit" >
<input type = "hidden" name = "try" value = <%= s1 %> >
<% out.println(s1); %>
</form>
<br>
<br>
<%
}
Below is the code which should return a string of values from 1 to 3 (values of s1)
String[] vals;
vals = new String[3];
out.println(vals.length);
vals = request.getParameterValues("try");
out.println(vals.length);
for(int i=0; i<vals.length; i++){
out.println("Hy");
out.println(vals[i]);
}
It is only returning one value which is the first one and printing vals.length prints 1.
You are creating three forms. For each form there will be separate request made. Since each form contains only one hidden input you will always get only one value for each request for below code
request.getParameterValues("try");
Now if you have to submit all the forms with one click. here or here are the answers
I'm trying to pass an ArrayList from handle.jsp to main.jsp, but it doesn't let me do it. It keeps saying "Type mismatch: cannot convert from Object to ArrayList".
main.jsp:
<%# page import="java.util.ArrayList" %>
<html>
<body>
<h1>Hobby Manager</h1>
<%
ArrayList<String> hobbies = session.getAttribute("hobbies");
out.println(hobbies.size());
out.println(session.getAttribute("hobbies"));
%>
<h2>Add new hobby!</h2>
<FORM action="handleAddHobby.jsp" method="get">
What new hobby are you wishing to add? <INPUT TYPE=text name=hobbyName /> <br/>
<INPUT TYPE=submit name=addHobby value="Add Hobby" />
</FORM>
</body>
</html>
handle.jsp:
<%# page import="java.util.ArrayList" %>
<html>
<body>
<%
ArrayList<String> hobbies = new ArrayList<String>();
String hobbyName = request.getParameter("hobbyName");
if(hobbyName == null){
out.println("Please enter a hobby before clicking add! Dummy.<br/>");
}
else{
hobbies.add(hobbyName);
for(int index = 0; index < hobbies.size(); index ++){
out.println(hobbies.get(index) + "<br/>");
}
session.setAttribute("hobbies", hobbies);
}
%>
</body>
</html>
I have tried passing it as a string object, and passing it as an object alone but nothing seems to work.
the problem is here ..
ArrayList<String> hobbies = session.getAttribute("hobbies");
Try typecasting it as getAttribute always returns Object.
ArrayList<String> hobbies = (ArrayList<String>)session.getAttribute("hobbies");
i want to change all scriptlets in my jsp pages to jstl,
how can i change this code to jstl
<% Map validationResults = (HashMap) request.getAttribute("validationResults");%>
<% if (validationResults != null) {
if (validationResults.containsKey("userName")) { //how can i chage this line to jstl ?
%>
<%=((ValidationResult) (validationResults.get("userName"))).getDetails()%> //how can i chage this line to jstl too ?
<%}%>
<%}%>
MY JSTL
<c:set var="validationResults" value="validationResults" scope="request"></c:set>
<c:if test="validationResults != null">
//how can i change the code of map here?
</c:if>
and another problem with ArrayList which contains list of Group object , in the loop i want to get each Group object and check a specific method inside Group object , how can I reach to these method through jstl??
I want to change this code
<%List<Group> allGroupList = new ArrayList<Group>();
allGroupList = (ArrayList) request.getAttribute("groups");%>
<% for (int index = 0; index < allGroupList.size(); index++) {%>
<%Group aGroup = (Group) allGroupList.get(index);%>
<label ><%=aGroup.getGroupEName()%></label>
<%if (aGroup.isIsUserGroup()) {%>
<input type="checkbox" name="group" value="<%=aGroup.getGroupNo()%>" CHECKED />
<%} else {%>
<input type="checkbox" name="group" value="<%=aGroup.getGroupNo()%>" />
<%}%>
<%}%>
here's my changed code:
<jsp:useBean id="GroupBean" class="ps.iugaza.onlineinfosys.entities.Group" type="ps.iugaza.onlineinfosys.entities.Group" scope="reqeust">
<c:set var="allGroupList" value="groups" scope="request"></c:set>
<c:forEach var="grp" items="${allGroupList}" varStatus="status">
//?????? what should i do ?
</c:forEach>
For The First Part
JSTL and EL only work with method that follows Java Bean convention. If you really wanna go this route, then you can loop around your map.
<c:forEach items="${requestScope.validationResults}" var="mapEntry" varStatus="index">
<c:if test="${mapEntry.key == 'userName'}">
<tr>
<td>${mapEntry.value.details}</td>
</tr>
</c:if>
</c:forEach>
The other way can be just get userName from the map, and check for if its null or not, and then do whatever you like. This is indeed a better idea.
<c:if test="${requestScope.validationResults['userName'] != null}">
<tr>
<td>${requestScope.validationResults['userName'].details}</td>
</tr>
</c:if>
For The Second
<c:forEach var="grp" items="${requestScope.groups}" varStatus="status">
<label>${grp.groupEName}</label>
<input type="checkbox" name="group" value="${grp.groupNo}" ${grp.isUserGroup ? 'checked' : ''} />
</c:forEach>
As for no 1), You would have to populate your request through an action/controller, and have a JSTL script that iterates through your map as follows:
Warning: Untested
<c:if test="${requestScope.validationResults != null}">
<c:forEach var="entry" items="${requestScope.validationResults}">
<c:if test="${entry.key == 'userName'}">
Result: ${entry.value.details};
</c:if>
</c:forEach>
</c:if>
Adeel Ansari answered number 2 for you.