iterating the list in JSTL - java

I have the controller file which holds a list of values in the model like,
List caseIds = new ArrayList();
for(int i=0; i<caseLists.size(); i++)
{
if(caseIds.contains(caseLists.get(i).getCaseId()))
continue;
else
caseIds.add(caseLists.get(i).getCaseId());
}
model.put("caseIdList", caseIds);
and i want to show this caseIds in UI in a dropdown box. My code looks like this,
<select name="caseIds" id="caseIds">
<option value="">All</option>
<c:forEach var="item" items="${model.caseIdList}">
<option value="<c:out value='${item.caseIdList}'/>" >
<c:out value="${item.caseIdList}"/>
</option>
</c:forEach>
</select>
I mean I want the dropdown(select box) with an id and value as caseId's. I understand that there is an error in getting item with item.caseIdList. How should I approach?

item in the for loop in the jsp will be the caseId that was added with caseLists.get(i).getCaseId(), so I think you just want the following:
<select name="caseIds" id="caseIds">
<option value="">All</option>
<c:forEach var="item" items="${model.caseIdList}">
<option value="<c:out value='${item}'/>" >
<c:out value="${item}"/>
</option>
</c:forEach>
</select>

This is what you want to do :
<select name="caseIds" id="caseIds">
<option value="">All</option>
<c:forEach var="item" items="${model.caseIdList}">
<option value="<c:out value='${item}'/>" >
<c:out value="${item}"/>
</option>
</c:forEach>
</select>
You need to use item instead of item.caseIdList since the caseIdList contains Integer.

Related

Create URL based on user input

I am very new to Java and will like to learn it by applying in my work.
I will like to generate a link based on user input via drop down list.
https://example.com/"input1"/testing_"input2"_xx.html
Input 1 drop down list
Input 2 is date
How do I concatenate input 1 and 2 into the URL?
You have to get the values of the input fields:
function generateURL()
{
var part1 = document.getElementById('part1').value;
var part2 = document.getElementById('part2').value;
var url = "https://example.com/"+part1+"/"+part2+".html";
document.getElementById('result').innerHTML = url;
}
Part 1 of URL:<br/>
<select id="part1">
<option value="one">one</option>
<option value="two">two</option>
<option value="three">three</option>
<option value="four">four</option>
</select>
<br/>
Part 2 of URL:<br/>
<select id="part2">
<option value="one">one</option>
<option value="two">two</option>
<option value="three">three</option>
<option value="four">four</option>
</select>
<br/>
<button onClick="generateURL();">Generate URL</button>
<br/>
URL Generated:
<span id="result"></span>
In place of the second dropdown select you can use anything from date to input box.
UPDATE
A date example:
function generateURLX()
{
var partx = document.getElementById('partx').value;
var party = document.getElementById('party').value;
var urlx = "https://example.com/"+partx+"/"+party+".html";
document.getElementById('resultx').innerHTML = urlx;
}
Part 1 of URL:<br/>
<select id="partx">
<option value="one">one</option>
<option value="two">two</option>
<option value="three">three</option>
<option value="four">four</option>
</select>
<br/>
Part 2 of URL:<br/>
<input type="date" id="party" placeholde="Date">
<br/>
<button onClick="generateURLX();">Generate URL</button>
<br/>
URL Generated:
<span id="resultx"></span>

Refreshing JSP select list value on Page refresh

I have below code in my jsp :
<select name="productId" id ="productId" onchange="getDetail()">
<% for (int i = 0;i < no_rows;i++){
%>
<option value="<%=ar[i]%>"><%=ar[i]%></option>
<%
}
%>
</select>
On page refresh(F5), the values in the drop down is not getting refreshed.The previously selected value remain there. DB call to fetch the detail is in JSP itself.
How to refresh the values?
Try JSTL <c:forEach> Tag instead
<select name="productId" id ="productId" onchange="getDetail()">
<c:forEach var="item" items=${yourListNameStoredInSomeScope}>
<option value="{item}">${item}<option>
</c:forEach>
</select>
try something like this:
<select name="productId" id ="productId" onchange="getDetail()">
<option value=" " selected></option>
<% for (int i = 0;i < no_rows;i++){
%>
<option value="<%=ar[i]%>"><%=ar[i]%></option>
<%
}
%>
</select>
if you use selected attribute , when the drop down is loaded, the pre-selected option is the default in your case " ".

display selected value of the drop down in jsp page

I want to display selected value. In the text field I can display it within value like below
value ="<%=event_data.getE_venue()%>"
code :
<input type="text" name="where" placeholder="Add a place" size="23" value ="<%=event_data.getE_venue()%>"/>
<select name="category" value ="<%=event_data.getE_target_category()%>" id="single1">
<option>Sports</option>
<option>Corporate</option>
<option>Religious</option>
<option>Music</option>
</select>
but in dropdown box it doesn't work.
please help me. thanks..
Firstly, select doesn't work in that way , you need to put selected attribute in option that matches your input.
for example:
<option selected='selected'>Sports</option>
check this fiddle :
http://jsfiddle.net/ZLTS7/
your code should be something like :
<input type="text" name="where" placeholder="Add a place" size="23" value ="<%=event_data.getE_venue()%>"/>
<select name="category" id="single1">
<option <%= (event_data.getE_target_category().equals("Sports")?"selected='selected'":"") %>>Sports</option>
<option <%= (event_data.getE_target_category().equals("Corporate")?"selected='selected'":"") %>>Corporate</option>
<option <%= (event_data.getE_target_category().equals("Religious")?"selected='selected'","") %>>Religious</option>
<option <%= (event_data.getE_target_category().equals("Music")?"selected='selected'":"") %>>Music</option>
</select>
You need to change the dropdown value through Javascript or jQuery. You can assign desired value to dropdown just like input
var dd = document.getElementById('single1');
var opts = ddl.options.length;
var value = <%=event_data.getE_venue()%>;
for (var i=0; i<opts; i++){
if (dd.options[i].value == value){
dd.options[i].selected = true;
break;
}
}
or if you are using jQuery.
$("#single1").val(value);
See this example:
<html>
<head>
<script type="text/javascript" src="http://code.jquery.com/jquery-1.7.2.js"></script>
<script type="text/javascript">
$(document).ready(function() {
$('#slectboxid option').click(function(){
$('#textboxid').val($(this).val());
});
});
</script>
</head>
<body>
<form action="#" method="post">
<select name="select" id="slectboxid">
<option value="test">test</option>
<option value="test2">test2</option>
</select>
<input type="text" name="text" id="textboxid" />
</form>
</body>
</html>

get map from jsp-form spring

I am new in Spring MVC, and I have a problem.
I'm sending to FORM LinkedHashMap, and it's showing great.
model.addAttribute("resultForm", resultForm);
Part of my jsp:
<c:forEach items="${resultForm}" var="resultMap" varStatus="status">
<tr id="tableRow" class="table-row">
<td>
${resultMap.key}
</td>
<td>
<select name="resultMap['${resultMap.key}']" class="espa-config-select">
<option selected value ="${resultMap.value}">${resultMap.value}</option>
<c:forEach items="${mainParams}" var="item2">
<c:if test="${item2.key == resultMap.key}">
<c:forEach items="${item2.value}" var = "q">
<c:if test="${resultMap.value != q}">
<option value="${q}"> ${q} </option>
</c:if>
</c:forEach>
</c:if>
</c:forEach>
</select>
</td>
</tr>
</c:forEach>
Now I need to get it back
Here is part of Controller
#RequestMapping( value = "espa/update", method = RequestMethod.POST )
public String save(#ModelAttribute("resultForm") LinkedHashMap<String,String> resultForm) {
System.out.println("resultMap post "+resultForm.toString());
if (resultForm!=null){
//resultForm.put("Port", port);
espaService.setConfiguration(selectedDevice, resultForm);
LOG.debug("Saving configuration: {} /nPort{}",resultForm, selectedDevice);
}
return "redirect:/espa";
}
But it is empty!
How can I fix it?
In your select you are using the name "resultMap". The name attribute needs to correlate to the Model Attribute, which you are called "resultForm".

how can I change special jsp scriptlet to jstl?

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.

Categories

Resources