0

I encounter a strange issue, when I trying to display html select on jsp page, the value wrapped with "???"

???Male???
???Female???

here is code on jsp page

<form:select path="gender" class="form-control" id="gender">
   <c:forEach var="gd" items="${genders}">
      <c:choose>
         <c:when test="${studentEdit.gender==gd.key}">
            <form:option selected="true" value="${gd.key}">
               <fmt:message key="${gd.value}" />
            </form:option>
         </c:when>
         <c:otherwise>
            <form:option value="${gd.key}">
               <fmt:message key="${gd.value}" />
            </form:option>
         </c:otherwise>
      </c:choose>
   </c:forEach>
</form:select>

controller code

Map<String, String> genders = new LinkedHashMap<String, String>();
genders.put("M", "Male");
genders.put("F", "Female");
model.addObject("genders", genders);

Seems like some encoding/decoding issue ?

EDIT

Thanks to @JB Nizet

I changed the jsp/jstl code to

<c:url value="/Student/Edit" var="editstudenturl"/>
<form:form method="post" action="${editstudenturl}" modelAttribute="studentEdit" class="form-horizontal">
  <form:select path="gender" class="form-control" id="gender" >
    <form:options items="${genders}" />
  </form:select>
</form:form>

1 Answer 1

2

That simply means that your resource bundle (the properties file where you store all the translations) doesn't have an entry for the keys Male and Female.

If you don't want to translate Male and Female, then you shouldn't be using <fmt:message key="${gd.value}" />, since that's its sole purpose. Simply use ${gd.value}.

Note that I also have a hard time understanding why you're using c:choose, given that the code in the two cases is identical.

Sign up to request clarification or add additional context in comments.

8 Comments

c:choose from stackoverflow.com/questions/5935892/if-else-within-jsp-or-jstl, what you recommend to use? c:if ?
I would recommend not using anything. What's the point in doing if (someCondition) { doThis(); } else { doThis(); }? Why not simply do doThis();?
you mean if(M/F) selected=selected ? sorry, can elaborate?
Now it makes more sense. Except that the form:option tag should do that for you. It's responsibility is to generated selected="selected"if the value of the option is equal to the gender value bound to the form:select.
Yes. The form is bound to the studentEdit model attribute, and the select is bound to the path gender in this object.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.