2

私はSpring MVCとSpring Dataを使用しており、Spring Dataを構成DomainClassConverterして、文字列IDを適切なドメインクラスに自動的に変換します。

私は今、次を使用してタグを使用して Order to Customer 参照を実装しています。

<form:select path="customer">
    <form:option value="" label="Select" />
    <form:options items="${customers}" itemValue="id" />
</form:select>

これにより、指定された HTML が生成されます。

<select id="customer" name="customer" class="span6">
    <option value="">Select</option>
    <option value="1">Customer A</option>
    <option value="2">Customer B</option>
    <option value="3">Customer C</option>
</select>

たとえば顧客 A を選択して投稿を送信すると、次のような例外が発生します。

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!

受け取る値は '' ではなく 1 である必要があるため、これはまったく意味がありません。受信したリクエスト パラメータもデバッグしましたが、customer=1 しか含まれていませんでした (予想どおり)。

InvalidDataAccessApiUsageExceptionは Spring Data から来ていることに注意してくださいDomainClassConverter。このコンバーターを削除すると、問題は発生しなくなります。

DomainClassConverter私はまた、それが2回カバーしようとしているのを見ることができるものをデバッグしました。最初は文字列値 1 (予想どおり) でしたが、次に空の文字列で別の呼び出しを行ったため、例外が発生しました。

それはもっと奇妙になりますが...

<form:option value="" label="Select" />プレーンな html 要素に置き換えると<option value="">Select</option>、例外は発生しないため、文字列 1 を使用して convert メソッドが呼び出されるのは 1 回だけです。奇妙な点は、両方が同じ html 出力<form:option value="" label="Select" />を生成することです...<option value="">Select</option>

動作を説明できません...プレーンなhtml要素を使用した「回避策」があるようですが、問題の原因を知りたいです。

4

2 に答える 2

6

Note that org.apache.jasper.JasperException indicates that exception occurs during JSP rendering, not during data binding. It's consistent with the fact that exception depends on <form:option value="" label="Select" /> - it's thrown when this tag is being processed when rendering the form after postback.

Behaviour you observe can be explained as follows: in order to determine its selected state <form:option> tries to compare its value with value of a field bound to <form:select>. If value of the bound field is null, <form:option> simply compares its value with null, that's why you don't get this exception during initial form rendering. Otherwise, <form:option> tries to convert its <value> to type of the bound field, and value = "" causes an exception at this step.

So, you should use null instead of empty string for "no option selected" value:

<form:option value="${null}" label="Select" /> 
于 2012-09-14T23:08:04.613 に答える
0

この種のシナリオでは、適切なエディターをアタッチすることを検討する必要があるかもしれません。コードが表示された場合<form:options items="${customers}" itemValue="id" />、顧客リストをオプションに直接アタッチしているためです。自動的に toString を取得し、「Csutomer A」、「Customer B」などを表示すると思います。理想的には、itemLabel を form:options タグで使用する必要があるか、プロパティ エディターを登録して initBinder にアタッチする必要がある場合があります。http://static.springsource.org/spring/docs/2.5.x/reference/validation.html#beans-beans-conversion

于 2012-09-14T22:18:03.863 に答える