Set value of bean attribute on Drop Down selection: Spring - java

I am using JSTL to populate drop down items, where country is an item having { id, name, code } attributes.
My need is to get name and code for selected country.
for example:
Country{c_id, c_name, c_code} is structure of country bean. when user selects this item I need to retrieve two values c_name, c_code.
What I did yet:
As I know, only one value can be assigned to itemValue either c_name or c_code.
I tried to populate all country and match selected country and then set to another path variable, but this also not working.
My code is as
<form:select path="selectedCountry" id="ddlCountryId">
<c:forEach items="${countries}" var="country">
<option value="${country.countryName}" ${country.countryName == selectedCountry ? 'selected' : ''}>${country.countryName}</option>
</c:forEach>
</form:select>
<input class="login_submit" type="submit" value="Login" id="btnSubmitId">
<!-- Read country code of selected country -->
<c:forEach var="country" items="${countries}">
<c:out value="country"></c:out>
<c:choose>
<c:when test="${country.countryName==loginCreadetials.selectedCountry}">
<input name="countryCode" type="hidden" value="${country.countryCode}"/>
</c:when>
</c:choose>
</c:forEach>
How can I do this?

Set the value of your option tag to a String that you can easily parse, for instance
value="${country.countryName}:${country.countryName}"
In your controller, you can then split the string on the ':' character to retrieve your two values.

Related

How to retain the value of the listbox selected when user login to a form

I am working in an application where i have a list box with some values there ,i am selecting a value from there and then i am submitting it.The value is inserted into the database properly.Now what i want ,i want when the user again login to the from he should be able to see that selected values in the listbox.I am able to pick the value from the database when the user is loggin into the system .But i can not manage to show it selected ,Here is my code
<tr>
<th>What would be the single biggest motivator in a potential new opportunity?</th>
<td>
<select id="motivator" name="motivator" multiple="multiple">
<% while(rs8.next()){
motivatorString = rs8.getString("motivators");
%>
<option value ="<%=motivatorString %>" ><%=motivatorString %> </option>
<%} %>
</select>
</td>
</tr>
How can i achieve that ??
Use attribute selected="selected" for option like:
<option value ="<%=motivatorString %>" selected="selected_only_if__motivatorString_value_equals_value_from_db"><%=motivatorString %> </option>
selected_only_if__motivatorString_value_equals_value_from_db is just a placeholder to tell u that replace it with "selected" when the string matches the value in DB

Associate name and id to an input box

I have a text box in Jsp where I use Spring tags. I want to show the name of a student but associate the id too. At the moment I can only associate the name:
<form:input id="myexample" path="student" value="{student.name}"/>
What I wanna do is similar to what I can do with a SELECT:
<form:select id="myselect" path="student">
<c:forEach items="${studentList}" var="student">
<form:option value="${student.id}" label="${student.name}"></form:option>
</c:forEach>
</form:select>
I would like to send the ID to the "path" but show the name.
Is it possible?
You will need to associate a hidden field to your userId as follows:
<form:input path="student" value="{student.name}"/>
<form:hidden path="student" value="{student.id}"/>
In the first you show the name of the user, and in the second one you hold the userId

How to make an option selected from a dropdown list in jsp?

In my project. I want to populate the drop down list on a jsp from a database.
<select id="names" name="names"> <c:forEach items="${names}" var="names">
<option><c:out value="${names}"/></option>
</c:forEach>
</select>
The ${names} is a list of names from the database. I want to select an option dynamically in the drop down list. Suppose there are three names in the database Rohan, Dean, Justin. If Dean is logged, i want select the option Dean as selected.
I try a code like this but this does not work.
<option value="${names}" ${names == names ? 'selected' : ''}>${names}</option>
Try like this assuming that loggedInUser variable holds the String value of the currently logged in user.
<select id="names" name="names">
<c:forEach items="${names}" var="names">
<c:when test="${loggedInUser eq names}">
<option value ="<c:out value="${names}"/>" selected="selected">${names}</option>
</c:when>
<c:otherwise>
<option><c:out value="${names}"/></option>
</c:otherwise>
</c:forEach>

How to use the index variable of a JSTL forEach loop to access a map entry?

With a forEach loop I'd like to create table cells (for a row) whereas each cell contains an input field of a form. The number of table cells is always fixed (12). That is actually no problem. However, here comes the challenge: the forEach should also enter a variable number of default values into the input fields that have to be obtained from a Map(Long, Double).
This is my (simplified) attempt:
<c:forEach var="number" begin="1" end="12" >
<td>
<input type="text" value="${requestScope.aMapWithData[number]}" />
</td>
</c:forEach>
But this doesn't show any value from the Map in the input fields. I guess the problem is that "number" is of type String and not Long. So I wonder if this problem can be solved without using scriptlets.
What number do you want to show? Is it index number of each map entry?
<c:forEach items="${aMapWithData}" var="item" varStatus="status">
<td>
<c:out value="${status.count}."/>
<input type="text" name="${item.key}" value="${item.value}" />
</td>
</c:forEach>
Try this
<c:forEach items="${aMapWithData}" var="mapEntry">
<c:set var="mapKey" value="${mapEntry.key}"></c:set>
<c:set var="mapValue" value="${mapEntry.value}"></c:set>
</c:forEach>

The function " " must be used with a prefix when a default namespace is not specified

we are doing some weird handling of our form variables. Anyway I have managed to get the variables from the request so i can do some database stuff. Now i want to post back to the from so the select boxes are can be populated with the original selections.
Here is an example of a select field:
JSP:
Condition Code:
<select size="1" name="filterCriteria('CONDITION_CODE').values">
<option value=""> </option>
<c:forEach var="bean" items="${conditions}">
<option value="'${bean.code}'"<c:if test="${bean.code == form.filterCriteria('CONDITION_CODE').values}"> selected="selected"</c:if>>${bean.code}: ${bean.description}</option>
</c:forEach>
</select>
<input type="hidden" name="filterCriteria('CONDITION_CODE').fieldName" value="CONDITION_CODE"/>
<input type="hidden" name="filterCriteria('CONDITION_CODE').operation" value="="/>
As you can see the name is a function in the form: name="filterCriteria('CONDITION_CODE').values
Here is the form:
private String[] fieldNames;
private Map<String, FilterCriteriaForm> filters =
new HashMap<String, FilterCriteriaForm>();
public String[] getFieldNames() { return this.fieldNames; }
public Map<String, FilterCriteriaForm> getFilters() { return this.filters; }
public FilterCriteriaForm getFilterCriteria(String fieldName)
throws ServletException
{
FilterCriteriaForm filter = (FilterCriteriaForm)filters.get(fieldName);
if (filter == null)
{
filter = new DetFilterCriteriaForm( requestAction );
filters.put( fieldName, filter );
}
return filter;
}
public void setFilters(Map<String, FilterCriteriaForm> val) { this.filters = val; }
}
Anyway normally I do something like this on the jsp to set the field back to what is in the form: "<c:if test="${bean.code == form.filterCriteria('CONDITION_CODE').values}"> selected="selected"</c:if>
When I do this...I get this error:
The function filterCriteria must be used with a prefix when a default namespace is not specified
edit:
Condition Code: <select size="1" name="filterCriteria('CONDITION_CODE').values">
<c:set var="condition" value="filterCriteria('CONDITION_CODE').values" />
<option value=""> </option>
<c:forEach var="bean" items="${conditions}">
<option value="'${bean.code}'" <c:if test="${bean.code == param[condition]}">selected="selected"</c:if>>${bean.code}: ${bean.description}</option>
</c:forEach>
</select>
<input type="hidden" name="filterCriteria('CONDITION_CODE').fieldName" value="CONDITION_CODE"/>
<input type="hidden" name="filterCriteria('CONDITION_CODE').operation" value="="/>
<br/></br>
THIS IS WHAT WORKED....I looked at the form again closer...took out the single quotes and used the getFilters():
<select size="1" name="filterCriteria(CONDITION_CODE).values">
<option value=""> </option>
<c:forEach var="bean" items="${conditions}">
<c:set var="code" value="'${bean.code}'" />
<option value="${code}" <c:if test='${code == form.filters["CONDITION_CODE"].values[0]}'>selected="selected"</c:if>>${bean.code}: ${bean.description}</option>
</c:forEach>
</select>
Why are you giving your form and input elements invalid names like that?
Anyway, your concrete problem is that you're not comparing to a plain string, but to an EL object. As you have now, it expects a bean ${form} with a method filterCriteria(String) which returns some object which has a getValues() method. That's not what you want. You want it to be a plain string.
Fix it as follows, you need to quote it:
<c:if test="${bean.code == 'form.filterCriteria(\'CONDITION_CODE\').values'}">
But this won't let you achieve the functional requirement. It look like that you're confusing Java/JSP with JavaScript and expecting them to run in sync. This is not true, Java/JSP runs in webserver and generates HTML. JavaScript is part of HTML and runs in webbrowser only. The form variable is only available in JavaScript, not in a JSTL tag.
You actually need to grab the submitted value as a request parameter by ${param}. The values of all input elements are avaiable by their name in the request parameter map. It would look like follows:
<c:if test="${bean.code == param['filterCriteria(\'CONDITION_CODE\').values']}">
I only don't guarantee if that would work, I have never used names with invalid characters, you perhaps need to URL-encode the odd characters. It would be much easier if you give your form and input elements a valid name which matches the rules as per HTML specification.
<select name="condition">
<option value=""> </option>
<c:forEach items="${conditions}" var="condition">
<option value="${condition.code}" ${condition.code == param.condition ? 'selected' : ''}>${condition.code}: ${condition.description}</option>
</c:forEach>
</select>
(note that you had an invalid value in <option value>, those singlequotes doesn't belong there, I've removed them)
Update: as per the comments, you seem to have a EL syntax error problem when accessing the illegal parameter name in EL. The escape characters \ seems to be completely swallowed by the EL parser. Your best bet is probably to alias it with <c:set>:
<c:set var="condition" value="filterCriteria('CONDITION_CODE').values" />
...
<c:if test="${bean.code == param[condition]}">
Update 2: those singlequotes around the option value seem to be absolutely necessary. In that case, you need to add another alias on that. The full code would look like this:
<select size="1" name="filterCriteria('CONDITION_CODE').values">
<c:set var="condition" value="filterCriteria('CONDITION_CODE').values" />
<option value=""> </option>
<c:forEach var="bean" items="${conditions}">
<c:set var="code" value="'${bean.code}'" />
<option value="${code}" <c:if test="${code == param[condition]}">selected="selected"</c:if>>${bean.code}: ${bean.description}</option>
</c:forEach>
</select>

Categories