JSTL padding int with leading zeros - jstl

I'm trying to use JSTL to build a form. I have a select input for the months but I need the months to always be two digits i.e. padded left with a Zero for 1-9.
I have this but obvious it doesn't give me what I want.
<%# taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %>
<select class="formInput">
<c:forEach var="i" begin="1" end="12" step="1" varStatus ="status">
<option><fmt:formatNumber pattern="##" value="${i}" /></option>
</c:forEach>
</select>
This has to have been done before but I can't find an example after a bit of searching.

found the answer: minIntegerDigits
<select class="formInput">
<c:forEach var="i" begin="1" end="12" step="1" varStatus ="status">
<option><fmt:formatNumber minIntegerDigits="2" value="${i}" /></option>
</c:forEach>
</select>

Related

Does any one know how to create Empty Array In JSTL (JSP Standard Tag Library)

I am trying to create an empty in my jstl page is it possible to create an empty array and assign some value to that array in the jstl page itself .?
Your question is quite unusual. You want to do scripting without a scriplet. The only way that I could think of is to use the useBean tag to create an ArrayList. Its add method returns a boolean. That is why I used empty if tags to insert the elements. The forEach tag is not necessary. I used it to display the elements.
<%# taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<jsp:useBean id="myList" class="java.util.ArrayList" />
<c:if test = '${myList.add("My first element")}'>
</c:if>
<c:if test = '${myList.add("My second element")}'>
</c:if>
<c:forEach var="element" items="${myList}">
${element}
</c:forEach>
Output:
My first element My second element
You can create an array with some values using c:set:
<c:set var="array" value="${['item a','item b','item c']}" />
<c:forEach var="item" items="${array}">
<c:out value="${item}" /><br />
</c:forEach>

My checkbox should not repeat in an iterate loop

I have a <logic:iterate></logic:iterate> loop in which I added a checkbox I want that the checkbox should not be added after every element in <iterate> loop.
I only want to print my:
<input type="checkbox" id="configureCheckBox" >Configure<br/>
this checkbox with only first element.
Assuming that you are using an s:iterator tag (since the question is tagged as struts2), you can use the status attribute to check if you are printing the first element:
<s:iterator value="yourVar" status="cycleStatus">
<s:if test="#cycleStatus.first">
<input type="checkbox" id="configureCheckBox" >Configure<br/>
</s:if>
<%-- other code --%>
</s:iterator>
See also the s:iterator and IteratorStatus documentation.
You can play with indexId in this case.
You can use something like
<logic:iterate indexId="index">
<%if (Integer.parseInt(String.valueOf(index))){%>
<input type="checkbox" id="configureCheckBox" >Configure<br/>
<%}%>
</logic:iterate>

jsp forEach Loop through Array of Images and place theme in specific divs

I have 60 images and I want to place 3 images in a div each.
<c:forEach items="${images}" var="image" varStatus="imgCount">
<c:if test="${imgCount.count == 3 }">
<div class="${gridImage.displayposition}">
<img src="${imagePath}${gridImage.image}" />
</div>
</c:if>
</c:forEach>
Would this be the way to accomplish this?
The condition in your c:if will only be true once: when you're on the third item. You can use the mod or % operators in EL to handle the images in groups of three. Take care with the varStatus.count property - it's one-based rather than zero-based, so you'd access the next image in the array with ${images[imgCount.count]}.
<c:forEach items="${images}" var="image" varStatus="imgCount">
<c:if test="${(imgCount.count-1) mod 3 eq 0}">
<div class="${gridImage.displayposition}">
<img src="${imagePath}${image}/>
<img src="${imagePath}${images[imgCount.count]}/>
<img src="${imagePath}${images[imgCount.count+1]}/>
</div>
</c:if>
</c:forEach>
You could also use the step attribute of the forEach tag and eliminate the need for the if statement. Here, you only create a div every third item. The count, however, will still continue in the sequence of 1,2,3... (as opposed to 1,4,7...) so you need to multiply the index by three.
<c:forEach items="${images}" varStatus="imgCount" step="3">
<div class="${gridImage.displayposition}">
<img src="${imagePath}${images[3*(imgCount.count-1)]}"/>
<img src="${imagePath}${images[3*(imgCount.count-1) + 1]}"/>
<img src="${imagePath}${images[3*(imgCount.count-1) + 2]}"/>
</div>
</c:forEach>

how can i set a default value using jstl in a drop down

am getting a list of values in the below itereate logic.I need to set one particular value "Count" as a default value in my dropdown
<logic:new name="val">
<logic:iterate name="val" id="Opt" type="parameter">
<c:set var="Key" value="${fn:trim(Opt.key)}"/>
<c:choose>
<c:when test="${Key == selectedValue}">
<option value="${Key}" selected="selected" ><c:out value="${Opt.Value}" /></option>
</c:when>
<c:otherwise>
<option value="${Key}"><c:out value="${opt.Value}" /></option>
</c:otherwise>
</c:choose> </logic:iterate>
I tried as below but its not defaulting the value in drop down.Any body can suggest any other way to set the default.
<c:if test="${paramName == 'Count'}">
<option value="-"><c:out value="${defaultLabel}"/> </option>
</c:if>
I am not sure whether i understood your question correctly or not but you can set the default value before starting your iteration
<option value="-" selected="selected" >your Default Value</option>
if my understanding is wrong please help me understand your requirement.

Spring JSP Select List for Range of Numbers

I'm creating a time entry system where the user has the option of selecting the hour and minute via two separate drop down select boxes. So the hour box has the numbers 1-12 in it, and the minute box has 00-59.
This is part of a Spring 2.5 Java EE project.
I have this in my JSP, for example, to create the option values as part of a select dropdown list:
<% for( int i=1; i<=12; i++) { %>
<option value="<%=i %>" <%= Integer.parseInt(time1fromHr)==i?selected:"" %> />
<% } %>
The for loop generates all the hours and marks the currently selected hour as default. Well, it looks pretty ugly to me, mostly because there is quite a bit of Java code involved here and I was wondering if there is a more elegant solution to approaching this problem using JSP tags or the Spring library. I am passing in the currently set parameters via the ModelAndView object in Spring.
In your model you could pass a List of Integers for hours, and another for minutes. Then you could use the form:select tag.
<form:select path="hour">
<form:options items="${hours} />
</form:select>
If your command object for the form has the selected value set in the "hour" value, and the model contains 1-12 in the "hours" value, then it should render the select and take care of marking the appropriate option selected. Then you do the same for minutes.
If you don't want to go the spring form taglib direction, you could again place the hours in the model and use JSTL. Something like:
<c:forEach var="hour" items="${hours}">
<c:if test="${hour} == ${selectedHour}">
<option value="${hour}" selected="selected">${hour}</option>
</c:if>
<c:if test="${hour} != ${selectedHour}">
<option value="${hour}" >${hour}</option>
</c:if>
</c:forEach>
I know there's a better way to do the c:if part, maybe using a c:choose, but you get the gist. You have your selected value in selectedHour and your choices in hours in the model.
Yes there is, this part of Spring MVC:
#RequestMapping(value="/index.html",method=RequestMethod.GET)
public String form(ModelMap map) {
Map<String,String> country = new LinkedHashMap<String,String>();
country.put("US", "United Stated");
country.put("CHINA", "China");
country.put("SG", "Singapore");
country.put("MY", "Malaysia");
map.addAttribute("countryList", country);
return "index";
}
and then use:
<form:select path="country" items="${countryList}" />
don't forget to add Spring type Library to your page:
<%# taglib prefix="form" uri="http://www.springframework.org/tags/form"%>
Use JSTL for comparison
<c:forEach var="hour" items="${hours}">
<c:if test="${hour == selectedHour}">
<option value="${hour}" selected="selected">${hour}</option>
</c:if>
<c:if test="${hour != selectedHour}">
<option value="${hour}" >${hour}</option>
</c:if>
</c:forEach>
OR
<c:forEach var="hour" items="${hours}">
<c:if test="${hour eq selectedHour}">
<option value="${hour}" selected="selected">${hour}</option>
</c:if>
<c:if test="${hour ne selectedHour}">
<option value="${hour}" >${hour}</option>
</c:if>
</c:forEach>

Resources