how can I change special jsp scriptlet to jstl? - java

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.

Related

How can I save all the data from different textboxs in an array, using JSTL?

I want to recieve any number of textbox values, and save it to an array with JSTL if it's possible.
I generate all the texbox where numberAsked can be any number.
<c:if test="${param.buttonSend != null}">
<form action="index.jsp" method="post">
<c:forEach var = "i" begin = "1" end = "${param.numberAsked}">
<input type="text" name="textbox[]" class="form-control">
</c:forEach>
<button type="submit" name="buttonSave">Save</button>
</form>
</c:if>
Now I want to save all the textboxs in an array.
<c:if test="${param.buttonSave != null}">
<c:set var="data" value="${request.getParameterValues(textbox)}"/>
<c:forEach var = "item" items="${param.data}">
<c:out value="${item}"/>
</c:forEach>
</c:if>
But, it doesn't work, how can I save all the data from all the generated textboxs in an array?.
Here is a demonstration JSP.
<%# taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<form method="post">
<c:forEach var = "i" begin = "0" end = "5">
<input type="text" name="textbox">
</c:forEach>
<button type="submit" name="buttonSave">Save</button>
</form>
<c:set var="data" value="${paramValues.textbox}"/>
<c:forEach var = "item" items="${data}">
${item}
</c:forEach>

Java Jstl foreach

I'm having a few problems with some JSTL code. The thing is that i'm trying to compare the id of the user that logged in with the id from the reservation class which has a foreign key of the user id.And after doing that i'm trying to populate the page with update links based on the reservation id.
Here's the code of what i tried but i can't find a way to make it work.
<table>
<tr>
<th>Reservation Id</th>
<th>Data CheckIn</th>
<th>Data CheckOut</th>
<th>Numar Persoane</th>
<th>Numar Camere</th>
<th>Action</th>
<%
List<ReservationBean> theReserv = (List<ReservationBean>) request.getAttribute("RESERVATION_LIST");
%>
<% int userId = (Integer) request.getSession().getAttribute("userId"); %>
<c:forEach var="tempReservation" items="${RESERVATION_LIST}">
<!-- set up a link for each res -->
<c:url var="tempLink" value="UserControllerServlet">
<c:param name="command" value="LOAD"/>
<c:param name="reservationId" value="${tempReservation.reservationId}"/>
</c:url>
<c:url var="deleteLink" value="UserControllerServlet">
<c:param name="command" value="DELETE"/>
<c:param name="reservationId" value="${tempReservation.reservationId}"/>
</c:url>
</c:forEach>
<% for(ReservationBean res : theReserv){
/* int userId = (Integer) request.getSession().getAttribute("userId"); */
if(userId == res.getUserId()){ %>
<tr>
<td> <%= res.getReservationId() %> </td>
<td> <%= res.getDataCheckin() %> </td>
<td> <%= res.getDataCheckout() %> </td>
<td> <%= res.getNrPersoane()%> </td>
<td> <%= res.getNrCamere() %> </td>
<td>Update
|
<a href="${deleteLink}"
onclick="if(!(confirm('Are you sure you want to delete this reservation?'))) return false">Delete</a>
</td>
</tr>
<%}%>
<%}%>
</tr>
</table>
In the second for i'm comparing the id of the user that logged in with the id of the user from the reservation class but of course the updated links will contain the last value of the first for and i don't know how to do what i did in pure java with JSTL to have only one for and get the correct values of the update link or delete ones.Do you guys have any clue on how to do that?
I've tried something like this
<table>
<tr>
<th>Reservation Id</th>
<th>Data CheckIn</th>
<th>Data CheckOut</th>
<th>Numar Persoane</th>
<th>Numar Camere</th>
<th>Action</th>
<%
List<ReservationBean> theReserv = (List<ReservationBean>) request.getAttribute("RESERVATION_LIST");
%>
<% int userId = (Integer) request.getSession().getAttribute("userId"); %>
<c:forEach var="tempReservation" items="${RESERVATION_LIST}">
<!-- set up a link for each res -->
<c:if test="${tempReservation.userId}" == userId >
<c:url var="tempLink" value="UserControllerServlet">
<c:param name="command" value="LOAD"/>
<c:param name="reservationId" value="${tempReservation.reservationId}"/>
</c:url>
<c:url var="deleteLink" value="UserControllerServlet">
<c:param name="command" value="DELETE"/>
<c:param name="reservationId" value="${tempReservation.reservationId}"/>
</c:url>
<tr>
<td>${tempReservation.reservationId}</td>
<td>${tempReservation.dataCheckin}</td>
<td>${tempReservation.dataCheckout}</td>
<td>${tempReservation.nrPersoane}</td>
<td>${tempReservation.nrCamere}</td>
<td>Update
|
<a href="${deleteLink}"
onclick="if(!(confirm('Are you sure you want to delete this student?'))) return false">Delete</a>
</td>
</tr>
</c:if>
</c:forEach>
But that if condition doesn't work or i don't know how to compare jstl code with java code
Edit2: I made it work! You had to compare it something like this
<c:if test="${tempReservation.userId == userId }">
Thanks for help!

Avoid multi forEach loop in JSTL in JSP

I want to avoid multi loop in JSTL which is shown by the code presented below. I got attributes WRTSC, DTA, DTA_PRZEDST_TR_OSW from api response and they are passing randomly so that is why the code looks like this.
<c:forEach items="${ctx.model.customerAttributes}" var="customerAttribute">
<tr>
<td class="code">${customerAttribute.subGroupName}</td>
<td class="value">
<c:forEach items="${customerAttribute.attributes}" var="attribute">
${attribute.attrName == 'WRTSC' ? attribute.attrValue : ''}
</c:forEach>
</td>
<td class="value">
<c:forEach items="${customerAttribute.attributes}" var="attribute">
${attribute.attrName == 'DTA' ? attribute.attrValue : ''}
</c:forEach>
</td>
<td class="value">
<c:forEach items="${customerAttribute.attributes}" var="attribute">
${attribute.attrName == 'DTA_PRZEDST_TR_OSW' ? attribute.attrValue : ''}
</c:forEach>
</td>
</tr>
</c:forEach>
I need to read every attribute (if there is not attribute sent I need to create empty <td></td> block.
Can it be done in one loop instead of three (in this case this number respresents the number of different attributes).
Thanks for helping.
I've got something like this now. Guys, do you think this is better?
<c:forEach items="${ctx.model.customerAttributes}" var="customerAttribute">
<tr>
<c:set var="WRTSC" value="" />
<c:set var="DTA" value="" />
<c:set var="DTA_PRZEDST_TR_OSW" value="" />
<c:forEach items="${customerAttribute.attributes}" var="attribute">
<c:if test="${WRTSC eq ''}">
<c:set var="WRTSC" value="${attribute.attrName == 'WRTSC' ? attribute.attrValue : ''}" />
</c:if>
<c:if test="${DTA eq ''}">
<c:set var="DTA" value="${attribute.attrName == 'DTA' ? attribute.attrValue : ''}" />
</c:if>
<c:if test="${DTA_PRZEDST_TR_OSW eq ''}">
<c:set var="DTA_PRZEDST_TR_OSW" value="${attribute.attrName == 'DTA_PRZEDST_TR_OSW' ? attribute.attrValue : ''}" />
</c:if>
</c:forEach>
<td class="code">${customerAttribute.subGroupName}</td>
<td class="value">${WRTSC}</td>
<td class="value">${DTA}</td>
<td class="value">${DTA_PRZEDST_TR_OSW}</td>
</tr>
</c:forEach>
You can use something like this
<c:forEach items="${ctx.model.customerAttributes}" var="customerAttribute">
<tr>
<td class="code">${customerAttribute.subGroupName}</td>
<td class="value">
<c:forEach items="${customerAttribute.attributes}" var="attribute">
${(attribute.attrName == 'WRTSC') || (attribute.attrName == 'DTA') || (attribute.attrName == 'DTA_PRZEDST_TR_OSW')? attribute.attrValue : ''}
</c:forEach>
</td>
</tr>
</c:forEach>
or
<c:forEach items="${ctx.model.customerAttributes}" var="customerAttribute">
<tr>
<td class="code">${customerAttribute.subGroupName}</td>
<td class="value">
<c:forEach items="${customerAttribute.attributes}" var="attribute">
<c:if test="${(attribute.attrName == 'WRTSC') || (attribute.attrName == 'DTA') || (attribute.attrName == 'DTA_PRZEDST_TR_OSW')}">
${attribute.attrValue}
</c:if>
</c:forEach>
</td>
</tr>
</c:forEach>
Rather than running 3 inner loop take 3 inner variable for
var WRTSC='';
var DTA ='';
var DTA_PRZEDST_TR_OSW ='';
and run only one inner loop in which check the condition with the variables , if condition is match set the value of variable otherwise by default value is '' .

jstl how to equal string int(ascii)

In My jsp i want to equal the String and int(assii)
<c:foreach var="i" begin="65" end="97" step="1">
<c:set var="answer1" value="&#${i }"></c:set>
<%-- it is always false --%>
<c:if test="${answer1 == 'A' }"> == true</c:if>
<%-- it is always false --%>
<c:if test="${answer1 eq 'A' }"> eq true</c:if>
</c:foreach>
i had try ,eq,fn:trim,fn:contrines.. ,ne,there are not work
can someone please help me.
This is a bit lengthy, but it should work:
<c:forTokens var="i" delims=","
items="A,B,C,D,E,F,G,H,I,J,K,L,M,N,O,P,Q,R,S,T,U,V,W,X,Y,Z">
<c:if test="${i == 'A'}">
...
</c:if>
</c:forTokens>
Try with:
<c:set var="answer1" value="<%=(char)((int)((Integer)pageContext.getAttribute("i")))%>"></c:set>

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

Categories

Resources