jstl dropdown selected - spring

So I have a spring binded dropdown similar to shown below. My question I want to
show ALL ID as the selected option whenever getAllID value is true. I know the selected
value is based on the value provided by spring bind element, in this case studentID but is there anyway I can override that anytime my boolean condition is true?
<spring:bind path="student.studentID">
<select name="${status.expression}" value="${status.value}">
<option />
<option value="001">1</option>
<option value="002">2</option>
<option value="003">3</option>
<option value="ALLID"
<c:if test='${student.getAllID eq true}'> Selected </c:if>>ALL ID
</option>
</select>
</spring:bind>

I think I got the fix. Added c:if to Select tag:
<c:if test='${student.getAllID ne true}'>
value="${status.value}"
</c:if>

Related

Spring how to use select with Thymeleaf [duplicate]

I want to create a drop down menu that allows a client to search users by a field specified in the drop down menu. For example, search by state, search by city, etc.
This is what I have so far:
<p>Search options:</p>
<form action="#" th:action="#{/get/{value}" method="get">
<select>
<option th:value="AllUsers">Search all users</option>
<option th:value="ByUsername">Search by user name</option>
<option th:value="ByFirstname">Search by first name</option>
<option th:value="ByLastname">Search by last name</option>
<option th:value="ByAddress">Search by address</option>
<option th:value="ByCity">Search by city</option>
<option th:value="ByState">Search by state</option>
<option th:value="ByZipCode">Search by zip code</option>
<option th:value="ByPhoneNumber">Search by phone number</option>
<option th:value="ByEmail">Search by email</option>
</select>
<input type="text" th:field="value" name="searchField"/>
<input type="submit" value="Search" name="searchButton"/>
</form>
I'm just not sure how to connect the action and the value tag of the currently selected item in the drop down list to specify the URI. If a user selects, search by state, and he enters in "Maryland", how do I specify the corresponding URI tag?
This would be my method in Spring that executes the action:
#RequestMapping(value = "/get/ByState", method = RequestMethod.GET)
public String getByState() {
// ...
}
#RequestMapping(value = "/get/ByCity", method = RequestMethod.GET)
public String getByCity() {
// ...
}
Because the accepted answer is not using Thymeleaf and this question is top in Google I'm adding my solution here.
In my situation statues is a list of Enum values. Model attribute is populated as usually you do in Spring:
mav.addObject("statuses", EnumSet.allOf(Status.class));
Group has a filed of type Status (the enum).
<div class="form-group row">
<select class="form-control" id="status" name="status">
<option th:each="stat : ${statuses}"
th:value="${stat}"
th:text="${stat}"
th:selected="${stat.equals(group.status)}"/>
</select>
</div>
This automatically populates the list and selects value that is selected in my Group instance:
You have just the required answer in this link:
http://fruzenshtein.com/spring-mvc-form-select-tag/

required field is not working in asp dropdownlist

i have an form with some text fields and dropdowns.
I am using bootstrap which users jQuery.
The problem is the required="required" attribute in the ASP dropdown control is not working.
Is working on text fields, but not no in dropdown.
here dont work:
<asp:DropDownList ID="orderTypesDropDownList" runat="server" CssClass="form-control" required="required" DataSourceID="purchaseOrderTypesDS" DataTextField="PurchaseOrderType" DataValueField="TypeID" AppendDataBoundItems="True">
<asp:ListItem Selected="True">-- SELECT --</asp:ListItem>
</asp:DropDownList>
But here works:
How can i make it works in the ASP dropdown like in the ASP textBox?
I was checking the DOM of the page after compile and looks like this:
<select name="ctl00$ContentPlaceHolder1$userBudget$orderTypesDropDownList" id="ctl00_ContentPlaceHolder1_userBudget_orderTypesDropDownList" class="form-control" required="required">
<option selected="selected" value="-- SELECT --">-- SELECT --</option>
<option value="8">AmEx PCard</option>
<option value="14">E-Victor Purchase</option>
<option value="13">In-House Job</option>
<option value="3">Internal Unit Request</option>
<option value="2">Invoice</option>
<option value="4">Journal Entry</option>
<option value="16">Material Move Request</option>
<option value="11">Material Request Tag</option>
<option value="12">Rapid Purchase</option>
<option value="17">Smart Buy Blanket PO</option>
<option value="6">Smart Buy PO</option>
<option value="15">Spare PO</option>
So its should be working, why is not working?
I am using ASP 4.0 framework with web forms.
The first value should be empty when using "required" in a select. (value="")

Setting First Option in <Select>

I'm using Spring Forms to display a list of countries using <form:select /> tag. The list is sorted alphabetically but I'd like to place "United States" as the first option, but not selected by default
The rendered HTML is as follows (the values are the country IDs, & the IDs will never change)
<select id="countries">
<option value="1">Argentina</option>
<option value="2">Columbia</option>
<option value="3">United States</option>
</select>
Could this be accomplished with a Spring Expression? Thanks much!
Try this :)
<form:select path="countries" id="countries">
<c:forEach var="countries" items="${countriesList}">
<form:option <c:if test="${countries.id==3}"> selected="true" </c:if> value="${countries.id}"> ${countries.name}
</form:option>
</c:forEach>
</form:select>

the value of form:select didn't get selected from controller

i set the value of form:select but in the result it didn't get selected.
My controller:
contactBF.setNom(contact.getNom());
contactBF.setQualite("130");
model.put("contact", contactBF);
model.put("qualities", [...]);
My jsp page:
<form:form id="contactform" modelAttribute="contact" action="">
<form:input path="nom" type="text"/>
<form:select path="qualite">
<option value=""> ... </option>
<c:forEach items="${qualities}" var="qualite" >
<option value="${qualite.id}" >${qualite.nom}</option>
</c:forEach>
</form:select>
</form>
the form:input get fill with the right value and form:select is loaded with all item but the right value is not selected !
i know this solution work:
<option value="${qualite.id}" ${(qualite.id == contact.qualite) ? 'selected' : ''}>${qualite.nom}</option>
But it wil be a lot of test.
i replace :
<c:forEach items="${qualities}" var="qualite" >
<option value="${qualite.id}" >${qualite.nom}</option>
</c:forEach>
By :
<form:options items="${qualiteList}" itemValue="id" itemLabel="nom" />
And it's work ;)

jsp spring tag library issue

I have the following mark-up in my jsp:
<form:select id="ddlSkillLevelCoreFrom1" path="aarKpis" multiple="false" class="notSelectable skillsFrom qar_dd war_skill5 validate[required]">
<option value="">Please select a Skill Level From</option>
<c:forEach var="skillLevel" items="${skillLevels}">
<c:if test="${selectedSoftSkill.skillLevelId == skillLevel.skillLevelId}">
<option selected="selected" value="${skillLevel.skillLevelId}">
<c:set scope="request" var="skillLevelFromSelected">${skillLevel.skillLevelId}</c:set>
<c:out value="${skillLevel.name}" />
</option>
</c:if>
<c:if test="${selectedSoftSkill.skillLevelId != skillLevel.skillLevelId}">
<option value="${skillLevel.skillLevelId}">
<c:out value="${skillLevel.name}" />
</option>
</c:if>
</c:forEach>
</form:select>
<form:select id="ddlSkillLevelCoreTo1" path="aarKpis" multiple="false" class="notSelectable skillsTo qar_dd war_skill5 validate[required]">
<option value="">Please select a Skill Level To</option>
<c:forEach var="skillLevel" items="${skillLevels}">
<c:if test="${skillLevel > skillLevelFromSelected}">
<option value="${skillLevel.skillLevelId}">
<c:out value="${skillLevel.name}" />
</option>
</c:if>
</c:forEach>
</form:select>
as you can see in the first form:select based on the previously selected value i set the same value in a variable using c:set. Then I want to use that value to filter and show all elements with bigger Ids than that value in the second form:select. For some odd reason, it shows all elements unfiltered in the second form:select. Can you detect what I am missing.
Thanks for taking the time to answer my question.
here: <c:if test="${skillLevel > skillLevelFromSelected}">
SkillLevelFromSelected is an id, so shouldn't you take skillLevel.skillLevelId instead of skillLevel only?

Resources