Iterate over a collection and put results in a Variable? - jstl

I tried asking this question but may have worded it wrong. Im doing this:
<c:forEach var="cell" items="${row}" varStatus="rowStatus">
<td class="data_extract">
<c:choose>
<c:when test="${results.types[rowStatus.index].array}">
<c:forEach var="elem" items="${cell}" varStatus="cellStatus">
${elem}<c:if test="${!cellStatus.last}">, </c:if>
</c:forEach>
</c:when>
<c:otherwise>
${cell}
</c:otherwise>
</c:choose>
</td>
For example ${elem} will loop a couple times and will have this in the cell: "10GT, 137S" after the loop is done. I want to maybe concatenante a variable so "10GT, 137S" is assigned a variable so that I might pass into:
<span class="mouseover_text" title="${NEW VARIABLE HERE!!!!!!!}"></span>
Please help a brother...
EDIT:
This seems to work!!
<td class="data_extract">
<c:choose>
<c:when test="${results.types[rowStatus.index].array}">
<c:set var="comma" value="," />
<c:forEach var="elem" items="${cell}" varStatus="cellStatus">
<c:set var="myVar" value="${cellStatus.first ? '' : myVar} ${elem} ${cellStatus.last ? '' : comma}" />
</c:forEach>
<span class="mouseover_text" title="${myVar}">${myVar}</span>
</c:when>
<c:otherwise>
${cell}
</c:otherwise>
</c:choose>
</td>

<td class="data_extract">
<c:choose>
<c:when test="${results.types[rowStatus.index].array}">
<c:set var="comma" value="," />
<c:forEach var="elem" items="${cell}" varStatus="cellStatus">
<c:set var="myVar" value="${cellStatus.first ? '' : myVar} ${elem} ${cellStatus.last ? '' : comma}" />
</c:forEach>
<span class="mouseover_text" title="${myVar}">${myVar}</span>
</c:when>
<c:otherwise>
${cell}
</c:otherwise>
</c:choose>
</td>

Related

To show the default value and maintain ordering in dropdown values

I am working on an application that needs to display a set of questions as a drop down(the user needs to answer 3 questions) in the jsp page.
It works fine, but the issue is for each time I display the drop down list(the questions are same), the questions selected by default are different - the actual question I set in the controller. I need to be able to just display the first or "Select" string as the default option.
My jsp:
<tr>
<br />
<form:label path="qs1">
<span class="">qs 1</span>
</form:label>
<br />
<form:select path="qs1" multiple="false">
<form:option value="${obj.qs0}" label="${obj.qs0}" />
<form:option value="${obj.qs1}" label="${obj.qs1}" />********In this instance this option is shown in the drop down
<form:option value="${obj.qs2}" label="${obj.qs2}" />
<form:option value="${obj.qs3}" label="${obj.qs3}" />
</form:select>
<br />
</tr>
<tr>
<br />
<form:label path="as1">
<span class="">as 1</span>
</form:label>
<br />
<form:input class="" path="as1" />
<br />
</tr>
<tr>
<br />
<form:label path="qs2">
<span class="">qs 2</span>
</form:label>
<br />
<form:select path="qs2" multiple="false">
<form:option value="${obj.qs0}" label="${obj.qs0}" />
<form:option value="${obj.qs1}" label="${obj.qs1}" />
<form:option value="${obj.qs2}" label="${obj.qs2}" />********In this instance this option is shown in the drop down
<form:option value="${obj.qs3}" label="${obj.qs3}" />
</form:select>
<br />
</tr>
<tr>
<br />
<form:label path="as2">
<span class="">as 2</span>
</form:label>
<br />
<form:input class="" path="as2" />
<br />
</tr>
I even tried selected="selected" for the right ones, but it did not work. Any suggestions would be helpful. Thanks.
EDIT: I should have been clear. I did try the option Amit suggested, as of now the obj.qs0 has the value instead of mentioning that in the JSP. I have just added that option along with the questions. The issue is I need to set the questions to the Questions object like below
Questions qs = new Questions();
qs.setQs0("----------Select-------------");
qs.setQs1("what is the name");
qs.setQs2("what is the color");
and so on.
So in the jsp I am setting the path
<form:select path="qs1" multiple="false">
<form:select path="qs2" multiple="false">
So those questions(q1, q2, and so on) are selected automatically and I cannot change the selection to always point to the qs0(where I have the default option). I also need to set the paths differently as I need to be able to capture the different questions & answers the users select.
1) To display "Select" as default, you can add below line :-
<form:select path="qs1" multiple="false">
**<form:option value="NONE" label="--- Select ---" />**
<form:option value="${obj.qs0}" label="${obj.qs0}" />
<form:option value="${obj.qs1}" label="${obj.qs1}" />
<form:option value="${obj.qs2}" label="${obj.qs2}" />
<form:option value="${obj.qs3}" label="${obj.qs3}" />
</form:select>
2) Use the LinkedHashMap or LinkedList to store the drop-down values in order to maintain ordering.

JSPX page breaks when script tag has src attribute

For some odd reason whenever I add a src element to the script tag at the end of this JSPX block, the first half of the page refuses to load. Cutting the HTML prior to the line out, and completely hosing the page. I've tried several different option here, including a CDATA block, making the script inline, and completely removing it from the page. For whatever reason, having anything that renders within that script block breaks the page.
<jsp:root xmlns:jsp="http://java.sun.com/JSP/Page"
xmlns="http://www.w3.org/1999/xhtml"
xmlns:spring="urn:jsptld:/WEB-INF/tlds/spring.tld"
xmlns:spring-form="urn:jsptld:/WEB-INF/tlds/spring-form.tld"
xmlns:intela-coreg="urn:jsptld:/WEB-INF/tlds/intela-coreg.tld"
xmlns:c="urn:jsptld:/WEB-INF/tlds/c.tld"
xmlns:x="urn:jsptld:/WEB-INF/tlds/x.tld"
xmlns:fmt="urn:jsptld:/WEB-INF/tlds/fmt.tld"
xmlns:fn="urn:jsptld:/WEB-INF/tlds/fn.tld"
xmlns:intela="urn:jsptagdir:/WEB-INF/tags/intela" version="2.0">
<jsp:directive.page pageEncoding="UTF-8" contentType="text/html; charset=UTF-8"/>
<c:set var = "myPath" value ="${sessionPath.regPath.pathName}" />
<c:set var = "pathParts" value="${fn:split(myPath, '_')}" />
<c:set var = "myPathPlacement" value = "${pathParts[0]}" />
<!-- Add the container code based on domain -->
<c:choose>
<!-- Australia -->
<c:when test ="${myPathPlacement == 'ap'}">
<c:set var ="gCode" value ="GTM-FR7W" />
</c:when>
<c:when test ="${myPathPlacement == 'as'}">
<c:set var ="gCode" value ="GTM-64GD" />
</c:when>
<c:when test ="${myPathPlacement == 'xaph'}">
<c:set var ="gCode" value ="GTM-TBNP" />
</c:when>
<!-- Canada -->
<c:when test ="${myPathPlacement == 'cp'}">
<c:set var ="gCode" value ="GTM-J578" />
</c:when>
<c:when test ="${myPathPlacement == 'xcph'}">
<c:set var ="gCode" value ="GTM-6WG2" />
</c:when>
<!-- Netherlands -->
<c:when test ="${myPathPlacement == 'nl2'}">
<c:choose>
<c:when test = "${pathParts[1] == 'voucher'}" >
<c:set var ="gCode" value ="GTM-6S7B" />
</c:when>
<c:when test = "${pathParts[1] == 'scooter'}">
<c:set var ="gCode" value = "GTM-8W4R" />
</c:when>
</c:choose>
</c:when>
<!-- Spain -->
<c:when test ="${myPathPlacement == 'es'}">
<c:choose>
<c:when test = "${fn:contains(pathParts[1], 'apple')}" >
<c:set var ="gCode" value ="GTM-JVG5" />
</c:when>
<c:when test = "${pathParts[1] == 'elcorteingles'}">
<c:set var ="gCode" value = "GTM-53L3" />
</c:when>
</c:choose>
</c:when>
<!-- France -->
<c:when test ="${myPathPlacement == 'fr'}">
<c:choose>
<c:when test = "${fn:contains(pathParts[1], 'peage')}" >
<c:set var ="gCode" value ="GTM-F5DR" />
</c:when>
</c:choose>
</c:when>
<c:when test ="${myPathPlacement == 'fr2'}">
<c:set var ="gCode" value ="GTM-ZG2J" />
</c:when>
<!-- UK -->
<c:when test ="${myPathPlacement == 'hp'}">
<c:choose>
<c:when test = "${pathParts[1] == 'uk'}" >
<c:set var ="gCode" value ="GTM-4LX8" />
</c:when>
</c:choose>
</c:when>
<c:when test ="${myPathPlacement == 'p2w'}">
<c:set var ="gCode" value ="GTM-S73N" />
</c:when>
<c:when test ="${myPathPlacement == 'pu'}">
<c:set var ="gCode" value ="GTM-7BRV" />
</c:when>
<c:when test ="${myPathPlacement == 'xurt'}">
<c:set var ="gCode" value ="GTM-TH5Q" />
</c:when>
<c:when test ="${myPathPlacement == 'TechRewardsUK'}">
<c:set var ="gCode" value ="GTM-Q3PL" />
</c:when>
<!-- USA -->
<c:when test ="${myPathPlacement == 'cd'}">
<c:set var ="gCode" value ="GTM-B4D5" />
</c:when>
<c:when test ="${myPathPlacement == 'uspr'}">
<c:set var ="gCode" value ="GTM-PZTJ" />
</c:when>
<c:when test ="${myPathPlacement == 'usrt'}">
<c:set var ="gCode" value ="GTM-QMZG" />
</c:when>
<c:when test ="${myPathPlacement == 'ussr'}">
<c:set var ="gCode" value ="GTM-GRJK" />
</c:when>
<c:when test ="${myPathPlacement == 'us'}">
<c:set var ="gCode" value ="GTM-W285" />
</c:when>
</c:choose>
<c:set var = "gCodeLength" value = "${fn:length(gCode)}" />
<!-- Test to see if this page needs the code or move along, these are not the droids we are looking for -->
<c:if test = "${gCodeLength > 0}" >
<script src="/media/common/js/googleTagManager.js"><!-- test --></script>
</c:if>

How to display <form:errors> outside <form:form> in Spring?

I'm new to Spring and I've been having some trouble trying to show a form errors.
I have the following form:
<form:form action="loginform.html" commandName="loginForm" onsubmit="goWait();">
<table><tr>
<td>User Name: </td>
<td><form:input path="userName" /></td>
</tr>
<tr>
<td>Password: </td>
<td><form:password path="password"/> </td>
<tr>
<td colspan="2">
<input type="submit" value="Login" style="butt-login">
</td>
</tr>
</table>
</form:form>
The form, as can be seen, is backed by a LoginForm Bean that validates it.
I want to show all the errors from the validation, outside this form. Something like this:
<div>
<ul>
<li><form:errors path="userName"/></li>
<li><form:errors path="password"/></li>
</ul>
</div>
This div should be outside the , so I can't use the tag.
How can I do this?
You could just capture the html produced by form:errors and display it somewhere after the form:
<form:form ...>
<c:set var="err">
<form:errors path="*" element="div" id="err" cssClass="hidden" />
</c:set>
</form:form>
....
<c:out value="${err}" />
If you want to show the errors before the form, you can do that with a bit of jQuery
Yes, you can user form:errors tag outside form:form tag
<form:errors path="yourBindedObject.*" />
<form:form action="loginform.html" commandName="loginForm" onsubmit="goWait();">
<table><tr>
<td>User Name: </td>
<td><form:input path="userName" /></td>
</tr>
<tr>
<td>Password: </td>
<td><form:password path="password"/> </td>
<tr>
<td colspan="2">
<input type="submit" value="Login" style="butt-login">
</td>
</tr>
</table>
</form:form>
If you want to show all errors associated with any fields just pass a star to the path attribute.
<form:errors path="*" />
But if you want to show global error messages just pass an empty string:
<form:errors path="" />
More information related: http://www.mkyong.com/spring-mvc/spring-mvc-form-errors-tag-example/

JSTL Illegal text inside "c:choose" tag: "<!-- ..."

<c:choose>
<c:when test="${somecondition}">
...
</c:when>
<!-- if not somecondition do otherwise -->
<c:otherwise>
...
</c:otherwise>
</c:choose>
The above code will throw a jspError, "JSTL Illegal text inside "c:choose" tag: "
<c:choose>
<c:when test="${somecondition}">
...
</c:when>
<%-- if not somecondition do otherwise --%>
<c:otherwise>
...
</c:otherwise>
</c:choose>
You can't use < !-- --> comments.
Thought I would share since I didn't see this on stackoverflow and ran into it today.
http://youtrack.jetbrains.com/issue/IDEA-44363
While comments would be useful, this constraint is documented in the JSTL spec. For Apache's implementation, it is enforced by a taglib validator: org.apache.taglibs.standard.tlv.JstlCoreTLV (declared in c.tld).
HTML comments in between c tags of JSTL core, are invalid. They must be JSP comments.i.e; <%-- -->

JSTL split Radio button into two lists

I am trying to split my Radio Button list into "n" number of columns based on number of Rows. This is what I was trying, here programs is my list which currently has 17 Radio Buttons. I am trying to divide (17/2) rows and 2 columns currently.
<c:set var="numRows" value="${fn:length(programs)/2-0.5}" />
<c:set var="totalRows" value="${fn:length(programs)}" />
<c:forEach begin="0" end="${numRows }" varStatus="i">
<c:out value="i=${i.index} " />
<c:set var="rowStart" value="${i.index * numColumns}" />
<fmt:formatNumber var="numColumns" value="${fn:length(programs) / numRows}"
maxFractionDigits="0" />
<c:forEach begin="0" end="${numColumns - 1}" varStatus="j" >
<c:set var="index" value="${rowStart + j.index}"/>
<c:out value="j=${j.index} " />
<c:choose>
<c:when test="${index lt fn:length(programs)}">
<c:forEach var="prgip" items="${programs}">
<c:forEach begin="0" end="${totalRows }" varStatus="k" var="prgip" items="${programs}">
<input type="radio" value="${prgip.program_id}" name="program"> ${prgip.program_name}
<c:out value="k=${k.count} " />
</c:forEach>
</c:forEach>
</c:when>
<c:otherwise> </c:otherwise>
</c:choose>
</c:forEach>
</c:forEach>
Thanks in Advance
-Mim Jones.

Resources