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>
Related
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>
I have 3 SolrSorts:
relevance
A-Z
Z-A
On the search page, there should be all of 3 sorts available. But on the category page just A-Z and Z-A. So how can I hide the relevance sort on the category page?
I have overridden the class DefaultSolrProductSearchService but there is nothing that could help me.
I think there should be something like a configuration in spring.xml?
If you want to make this configurable from the backend so that tomorrow you can hide another field for a particular page from HMC/backoffice then you need to make a lot of changes right from the Model to backend to frontend. But if you want to simply hardcode this requirement, you can handle this easily on the frontend side. Like this...
Modify searchresultsgridcomponent.jsp to pass an additional flag to the pagination.tag. Which help us to identify search page vs category page.
Please note, you can find two references of nav:pagination in that file, please modify both with isSearchPage="${true}".
<nav:pagination top="true" supportShowPaged="${isShowPageAllowed}" supportShowAll="${isShowAllAllowed}" searchPageData="${searchPageData}" searchUrl="${searchPageData.currentQuery.url}" numberPagesShown="${numberPagesShown}" isSearchPage="${true}"/>
Repeat above step for searchresultslistcomponent.jsp
In the pagination.tag, decare the isSearchPage attribute and handle the sort option rendering login with the help of this flag.
Something like this
<%# attribute name="isSearchPage" required="false" type="java.lang.Boolean" %>
<c:set var="isSeachPg" value="${empty isSearchPage ? false : isSearchPage}"/>
Let's allow relevance only for the search page.
<select id="sortOptions${top ? '1' : '2'}" name="sort" class="form-control">
<option disabled><spring:theme code="${themeMsgKey}.sortTitle"/></option>
<c:forEach items="${searchPageData.sorts}" var="sort">
<c:if test="${isSeachPg || (not isSeachPg && fn:escapeXml(sort.code) != 'relevance')}">
<option value="${fn:escapeXml(sort.code)}" ${sort.selected? 'selected="selected"' : ''}>
<c:choose>
<c:when test="${not empty sort.name}">
${fn:escapeXml(sort.name)}
</c:when>
<c:otherwise>
<spring:theme code="${themeMsgKey}.sort.${sort.code}"/>
</c:otherwise>
</c:choose>
</option>
</c:if>
</c:forEach>
</select>
I have 8 checkboxes and 1 button, when the user check any of the checkboxes and click the button, I want to check if any of the checkbox is checked and display it in another .jsp
I have already referred to few similar questions with no luck so far. So i tried to manage with my own logic
First.jsp
<c:forEach begin="1" end="8" varStatus="loop">
<input type="checkbox" id="seat" name="seat${loop.index}" value="seat${loop.index}" >
<label for="seat">Seat${loop.index}</label>
</c:forEach> <br> <br>
<input type="submit" value="Save" name="savebtn">
Second.jsp
<c:forEach begin="1" end="8" varStatus="loop">
<c:if test="${not empty param.seat[loop.index]}">
<c:out value="${param.seat1} is booked"/>
</c:of>
</c:forEach>
I have 2 problems regarding the code above :
i can't get loop.index value inside the param $param.seat[loop.index] doesn't work
And even if i try to do it manually, i can only get value from seat1. I can't get value from the rest ( seat2, seat3, etc).
${param.seat[loop.index]} implies that seat is a collection, which it is not (it probably does not even exist). You are after ${param.seatX}, where you can dynamically set X. You can do that by creating a variable containing the parameter name first:
<c:set var="seatVarName" value="seat${loop.index}"/>
Now you can use this variable to get the parameter value from the implicit EL object:
${param[seatVarName]}
See also:
JSP expression language and dynamic attribute names
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>
I have a drop down list and a form with a few textboxes. I would like to populate this form with details of selected item in the drop down list.
I'm doing this in java MVC app (or wannabe) and I have in my jsp page something like this:
<select name="item">
<c:forEach items="${persons}" var="selectedPerson">
<c:set var="person" value="${selectedPerson}" />
<option value="$selectedPerson.id">${selectedPerson.LastName}</option>
</c:forEach>
</select>
Persons is a list of the Person class.
I wonder is it possible to use the variable 'person' directly to fill the form, for example:
<textarea name="name" rows="1" cols="34" >
${selectedPerson.Name}
</textarea>
so that the rest of the form updates when the selectedPerson is changed?
I know how to do this within c#, but I don't have experience with java technologies.
Is it necessary to submit the form to servlet to do this, or it can be done on the client, since I have all my data in the persons list, from the moment of populating the drop down list?
The ${} syntax is JSP syntax, which will only be parsed and run once on the server to generate the HTML, and then sent down the wire as HTML. The changes to the select list then just happen in the client browser: the server doesn't know anything about them.
What you want to do is register a javascript listener on the select box. You should look into using a library ideally to help you do this. JQuery is a very popular solution and is worth reading up on if you're going to be doing this type of development.
If you end up using JQuery, you'll want to do something like the following
<select id="item" name="item">
<c:forEach items="${persons}" var="selectedPerson">
<c:set var="person" value="${selectedPerson}" />
<option value="$selectedPerson.id">${selectedPerson.LastName}</option>
</c:forEach>
</select>
<input name="lastName" id="lastName" type="text"/>
<script type="text/javascript">
$(function() {
$("#item").change(function() {
$("#lastName").val($("option:selected", this).text());
});
});
</script>
This will make more sense once you've read a basic JQuery tutorial, but basically what it does is that each time the select list changes value, it gets the selected option and sets it's content to the lastName input field. Hope this helps.