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>
Community
  • 1
  • 1
Timeless
  • 7,338
  • 9
  • 60
  • 94

1 Answers1

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.

JB Nizet
  • 678,734
  • 91
  • 1,224
  • 1,255
  • c:choose from http://stackoverflow.com/questions/5935892/if-else-within-jsp-or-jstl, what you recommend to use? c:if ? – Timeless Apr 18 '15 at 13:40
  • I would recommend not using anything. What's the point in doing `if (someCondition) { doThis(); } else { doThis(); }`? Why not simply do `doThis();`? – JB Nizet Apr 18 '15 at 13:43
  • you mean if(M/F) selected=selected ? sorry, can elaborate? – Timeless Apr 18 '15 at 13:49
  • Look at the code you posted. The code inside the c:when is identical to the code in c:otherwise. So you always do the same thing, whether the condition is true or false. So a c:choose is completely useless. – JB Nizet Apr 18 '15 at 13:52
  • ooohhhh, I fixed that, the first one missed selected="true". – Timeless Apr 18 '15 at 13:53
  • 1
    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. – JB Nizet Apr 18 '15 at 13:57
  • one more question, how does the select know which one is selected? is it because of – Timeless Apr 18 '15 at 14:33
  • 1
    Yes. The form is bound to the `studentEdit` model attribute, and the select is bound to the path `gender` in this object. – JB Nizet Apr 18 '15 at 14:49