Spring MVC - Reference Data - spring

Here is the scenario: I have something like this..
<form:select path="somePath" .....>
<form:option value="" label="Please Select"/>
<form:options items="${students}" itemValue="id" itemLabel="name"/>
</form:select>
This dropdown list works fine.
But how can I display name of a particular student? I wan to do something like this:
<c:out value="${students[id].name}"/>
Can any one help me with the syntax?
Thanks

I assume that ${students} is an array or list of student objects. As such, it's not indexed by id and can't be directly accessed that way.
Options include:
1) Include your collection of students as a map from id to student object; your items attribute then becomes ${students.values}, and you can then look up an individual student as ${students[id]}.
2) Or, keep it as a list and then iterate through your list and find the one where the id matches:
<c:forEach var="student" items="${students}">
<c:if test="${student.id==id}">
<c:out value="${student.name}" />
</c:if>
</c:forEach>
3) Or, lastly, if you know from the beginning which student you care about, include that student separately in the reference data.

Related

How to hide sort option for a specific page in the Hybris

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>

How to get values for dynamic form field names using JSTL and EL

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

Spring form option does not select item, options does

${products} contains a List<Product>. Product is a #Entity, has an equals method that compares by id. There is no converter or formatter registered for Product (other than Spring Data's DomainClassConverter but that doesn't seem to kick in for this case):
This works:
<form:select path="productFrom">
<form:option value="" label="-" />
<form:options items="${products}" itemValue="id" itemLabel="name"/>
</form:select>
This (needed for optgroup-ing, but simplified here) does not select the correct value:
<form:select path="productFrom">
<form:option value="" label="-" />
<c:forEach items="${products}" var="product">
<form:option value="${product.id}">${product.name}</form:option>
</c:forEach>
</form:select>
After debugging SelectedValueComparator I found that it tries to compare a candidateValue of type Long to a boundValue of String. I could work this around by creating a toString() method in product that returns the id as String. (Or I could have modified the equals() method to handle Long.)
Still, I have a bad feeling that I'm doing something wrong here.
In the end I solved this by adding a new method to Product:
public String getIdString() {
return id == null ? "" : id.toString();
}
and changing the option definition:
<form:option value="${product.idString}">${product.name}</form:option>
Still not sure I'm doing this the right way, any tips are appreciated.

Spring form tags. Enum i18

I'm using Spring form tag library to automatically bind values from enum in my Model to FORM fields:
<form:select path="status">
<form:options/>"
</form:select>
status is enum field in my form-backing object:
public enum Status {ON, OFF}
But in <select> tag I get labels like ON and OFF. Is there any way to localize this labels?
I know this is an old post but I found myself in this situation today. I didn't find any built-in solutions. A quick solutions is the following:
<form:select path="my.field">
<c:forEach items="${enumValues}" var="type" >
<form:option value="${type}">
<spring:message code="some.key.${type.name.toLowerCase()}" />
</form:option>
</c:forEach>
</form:select>
If you need this more than once you will need to create your own tag.

JSP drop down list - using selected item

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.

Resources