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>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With