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

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

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>

JSTL: html snippet depending on property being null or not. (with not empty)

<c:forEach items="${list }" var="element">
<c:if test='${not empty element.radioelement}'>
<input type="radio" name="${element.name }" id="${element.id }" >
<label for="${element.id }"></label>
</c:if>
<c:if test='${not empty element.divelement}'>
<div> </div>
</c:if>
<c:if test='${not empty element.breakelement}'>
<br />
</c:if>
</c:forEach>
This is the jstl code I'm trying to find out if it works.
The list is filled with the objects of the class "tyle" which I made myself. These contain the following properties:
private String radioelement, divelement, breakelement, name, id;
I made sure they all have setters and getters.
I'm trying to make it work so that depending on which property isn't null, that piece of html will be written. (a radiobutton incase the string radioelement isn't null and so forth untill the list ends)
I'm wondering if this will work this way cus if it does I'll probably have a logical fault in my code. As it is now it only registers the breaks and makes any divelement also into a radio.

passing jslt value to javascript

I trying to a pass a jslt value to javascript but the value is not getting rendered.
<c:forEach items="${requestScope.P.Releases}" var="pr" varStatus="status"> <a href=javascript:popPR('${pr.url}') class="linkPR">
<c:out value="${pr.title}" escapeXml='false' /></a>
<c:foreach>
if directly type the pr.url value the popup window gets opened but if i pass the through jstl it does not call the popup.
Can anyone please suggest how to fix it.
Thanks
Try using <c:out> to output the url :
<c:forEach items="${requestScope.P.Releases}" var="pr" varStatus="status">
<a href=javascript:popPR('<c:out value="${pr.url}"/>') class="linkPR">
<c:out value="${pr.title}" escapeXml='false' /></a>
<c:foreach>

Binding multiple objects in a Spring form

I have problems with getting my JSP view right. What I intend to do is to send a List that contains questions and each question object is a text field and a List with alternatives.
My intention is to be able to edit multiple questions (both to be able to edit the text/name of the question and edit the containing alternatives).
My backing object is now sending an List question.
Here is my JSP which are failing with invalid property of bean class.
<form:form commandName="question">
<form:errors path="*">
<fieldset class="stdframe">
<legend>Question</legend>
</fieldset>
</form:errors>
<div class="stdframe">
<c:forEach var="q" items = "${question}" varStatus = "s">
<p><b>Question:</b></p>
<p><form:input size="67" path="${q.text}"/></p>
<br/>
${q.text}
<ul>
<c:forEach var="alternative" items = "${q.alternatives}" varStatus = "t">
${alternative.text}
<li><form:input path = "${alternative[$t.index].text}" /></li>
</c:forEach>
</ul>
<br/>
</c:forEach>
<input type="submit" class="submit" value="Save" />
<input type="button" class="button" onClick="back()" value="Back"/>
</div>
</form:form>
I have tried both ${q.text} and ${q[$s.index].text}. When I just print ${q.text} it shows the correct text for the question object. Same goes for alternative.
What can I do to correctly bind the form to the objects?
In addition when I store an object which contains a list of other object, will the list be stored itself in the database?
You may need to wrap your List in a simple object with the List as a field:
class MyListWrapper { List questions; } // etc.
If you use that as your command/form object, you should be able to do something like this in the JSP:
<form:form commandName="wrapper">
// ...
<c:forEach var="q" items = "${wrapper.questions}" varStatus = "s">
<p><b>Question:</b></p>
<p><form:input size="67" path="questions[${s.index}].text"/></p>
// ...
<c:forEach var="alternative" items = "${q.alternatives}" varStatus = "t">
${alternative.text}
<li><form:input path = "questions[${s.index}].alternatives[${t.index}].text" /></li>

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