Note that there are some explanatory texts on larger screens.

plurals
  1. POSpring tag <form:option> not working with Spring Data DomainClassConverter?
    primarykey
    data
    text
    <p>I'm using Spring MVC and Spring Data and also have configured Spring Data's <code>DomainClassConverter</code> to automatically convert String id to the appropriate Domain class.</p> <p>I'm now implementing a Order to Customer reference using the tag using:</p> <pre><code>&lt;form:select path="customer"&gt; &lt;form:option value="" label="Select" /&gt; &lt;form:options items="${customers}" itemValue="id" /&gt; &lt;/form:select&gt; </code></pre> <p>which results in the given HTML:</p> <pre><code>&lt;select id="customer" name="customer" class="span6"&gt; &lt;option value=""&gt;Select&lt;/option&gt; &lt;option value="1"&gt;Customer A&lt;/option&gt; &lt;option value="2"&gt;Customer B&lt;/option&gt; &lt;option value="3"&gt;Customer C&lt;/option&gt; &lt;/select&gt; </code></pre> <p>When submitting a post with e.g. Customer A selected I get a exception like:</p> <pre><code>org.apache.jasper.JasperException: org.springframework.core.convert.ConversionFailedException: Failed to convert from type java.lang.String to type @javax.validation.constraints.NotNull @javax.persistence.ManyToOne nl.kapsalonreflection.domain.Customer for value ''; nested exception is org.springframework.dao.InvalidDataAccessApiUsageException: The given id must not be null!; nested exception is java.lang.IllegalArgumentException: The given id must not be null! </code></pre> <p>Which does not make sense at all as the value received should be 1 and not ''. I also debugged the received request params and it only contained customer=1 (as expected)</p> <p>Note that the <code>InvalidDataAccessApiUsageException</code> is coming from Spring Data's <code>DomainClassConverter</code> and if I remove the this converter the problem does not occur anymore.</p> <p>I also debugged the <code>DomainClassConverter</code> and what I can see it tries to cover twice. First with the String value 1 (as expected) but then another call with an empty String, causing the exception.</p> <p>It get's weirder though...</p> <p>When I replace <code>&lt;form:option value="" label="Select" /&gt;</code> with the plain html element <code>&lt;option value=""&gt;Select&lt;/option&gt;</code> then the exception does not occur, and hence only 1 call is made to the convert method with the String 1. The weird part is that both <code>&lt;form:option value="" label="Select" /&gt;</code> and <code>&lt;option value=""&gt;Select&lt;/option&gt;</code> product the same html output...</p> <p>I cannot explain the behaviour... although I seemm to have a 'workaround' with the plain html element I would like to know what is causing the issue.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload