Announcement Announcement Module
No announcement yet.
Select with null option Page Title Module
Move Remove Collapse
Conversation Detail Module
  • Filter
  • Time
  • Show
Clear All
new posts

  • Select with null option

    Dear all,

    I have a hard time getting a select list, which is optional, to work correctly.

    If I remove the .ttlId from the path, the current title is not selected
    (none of the options is selected in the select list).

    The below code is the best I could figure out. The only problem with it is if you select the empty option. I get an 'Invalid type' (ongeldig type in Dutch) in the page, with the following complete error message:

    Field error in object 'contactpersoonInput' on field 'titelByCpnBeforeTteId.ttlId': rejected value []; codes [typeMismatch.contactpersoonInput.titelByCpnBeforeTteId.ttlId,typeMismatch.titelByCpnBeforeTteId.ttlId,typeMismatch.ttlId,typeMismatch.long,typeMismatch]; arguments [ codes [contactpersoonInput.titelByCpnBeforeTteId.ttlId,titelByCpnBeforeTteId.ttlId]; arguments []; default message [titelByCpnBeforeTteId.ttlId]]; default message [Failed to convert property value of type [java.lang.String] to required type [long] for property 'titelByCpnBeforeTteId.ttlId'; nested exception is java.lang.NumberFormatException: For input string: ""]

    <form:select cssClass="cpn" path="titelByCpnBeforeTteId.ttlId">
      <form:option value=""></form:option>
      <form:options items="${titelsVoor}" 
        itemValue="ttlId" itemLabel="ttlDescription"/>
    	 * Registering several custom property editors
    	protected void initBinder(HttpServletRequest request,
    			ServletRequestDataBinder binder) throws Exception {
    				new NullableTitelEditor());
    	/** Custom property editor for optional Titel fields */
    	class NullableTitelEditor extends PropertyEditorSupport {
    		public String getAsText() {
    			if (getValue() == null) {
    				log.debug("Titel: getAsText: title is null.");
    				return "";
    			Titel titel = (Titel) getValue();
    			log.debug("Titel: getAsText: title is " + titel.getTtlId() + " : " + titel.getTtlDescription());
    			return "" + titel.getTtlId();
    		public void setAsText(String text) throws IllegalArgumentException {
    			Titel titel;
    			if ("".equals(text)) {
    				titel = null;
    			} else {
    				titel = generalService.retrieveTitle(Long
    			log.debug("Titel: setValue(" + (titel == null ? null : titel.getTtlId()) + " : " + 
    					(titel == null ? null : titel.getTtlDescription()) + ")");

  • #2
    You are binding a String to a long value and register a TitlePropertyEditor. So what is it you want? Bind to the title not the id of the title.


    • #3
      Dear Marten,

      Thank you for your reply.
      Your suggestion is to use the below <form:select> right?

      I have also tried that. And it seems values are saved to the database
      correctly. Both if I select the empty option and if I select a non-empty
      However, if I go to the same person again, the database value
      is not selected correctly. None of the select options is selected (and therefore, the first option becomes the selected option).

      That is why I tried to append .ttlId to the path. That makes the right option selected, but runs into the 'invalid type' problem.
      I guess you're right about the path property, but do you also know how to get the right option selected now?

      Thanks very much for your time,

      <form:select cssClass="cpn" path="titelByCpnBeforeTteId">
        <form:option value=""></form:option>
        <form:options items="${titelsVoor}" 
          itemValue="ttlId" itemLabel="ttlDescription"/>


      • #4
        With everything else in place, the below <select> tag does work
        for me. It correctly selects the title initially, and it can save any title,
        including the null value.

        This solves it for me, but if anybody could explain it to me I would still be gratefull.


        				<spring:bind path="titelByCpnBeforeTteId">
        					<select class="cpn" name="${status.expression}">
        						<option value=""></option>
        						<c:forEach var="titel" items="${titelsVoor}">
        								<c:when test="${titel.ttlId == contactpersoonInput.titelByCpnBeforeTteId.ttlId}">
        									<option selected="selected" value="${titel.ttlId}">${titel.ttlDescription}</option>
        									<option value="${titel.ttlId}">${titel.ttlDescription}</option>