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>
Related
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>
I have a foreach value in an iteration in a jsp page as shown
<c:forEach var="lists" items="${lists}" varStatus="theCount">
String val= ${lists.getVal() } ; //this is giving me error
</c:forEach>
https://www.tutorialspoint.com/jsp/jstl_core_set_tag.htm
<c:forEach var="list" items="${lists}" varStatus="theCount">
<c:set var="val" value="${list.val}"/>
<p>The value is: ${val}.</p>
</c:forEach>
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 '' .
I already have posted this post yesterday..but nobody answerd it..
I am using spring mvc framework.
I want to do pagination like the below picture:--
So I have done :--
#RequestMapping(value = "/detailssection/id",method=RequestMethod.GET)
public #ResponseBody String showthedetails(Map<String, Object> map, HttpServletRequest request) throws IOException{
//I want to display 5 details per page
int recordsPerPage = 5;
//It will count total no of records(like 300 records are there)
int totalnoOfrecords = viewService.TotalnoOfRecoredsbanglains1();
//If the totalnoOfrecords=300 then page noumber will be 300/5=60 that means 1,2....60
int pagenumbers = (int) Math.ceil(totalnoofrecords * 1.0 / recordsPerPage);
map.put("detail", new Detail());
map.put("noOfPages", pagenumbers);
map.put("detailList", viewService.viewDetails());
return "detailssection";
}
and my jsp page is like:--
<div id= "part1">
<div id= "details">
<p>${detail.description}</p>
</div>
</c:forEach>
<%--For displaying Previous link except for the 1st page --%>
<c:if test="${currentPage != 1}">
Previous
</c:if>
<%--For displaying Page numbers.
The when condition does not display a link for the current page--%>
<c:forEach begin="1" end="${noOfPages}" var="i">
<c:choose>
<c:when test="${currentPage eq i}">
${i}
</c:when>
<c:otherwise>
${i}
</c:otherwise>
</c:choose>
</c:forEach>
<%--For displaying Next link --%>
<c:if test="${currentPage lt noOfPages}">
Next
</c:if>
</div>
But I am not getting any page numbers.its only showing the "previous page" section.Its like:--
what am I doing wrong?Is there any problem in jsp page??
Let's start from the begining.
If you have a map on your requestScope, suppose it is MyMap with the following keys: details, noOfPages and detailList, to access it's values by keys, the syntax is the following:
<c:forEach begin="1" end="$MyMap['noOfPages']" var="i" varStatus="loop">
<c:choose>
<c:when test="${currentPage eq i}">
${i}
</c:when>
<c:otherwise>
${loop.index}
</c:otherwise>
</c:choose>
</c:forEach>
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.