0

初めての spring mvc アプリケーションを作成しようとしました。私はアーキテクチャを使用しています: Spring MVC-Service-DAO-Persistence アーキテクチャ。このエラーが発生しました:

Field error in object 'rooms' on field 'roomTypeId': rejected value [7]; codes    [typeMismatch.rooms.roomTypeId,typeMismatch.roomTypeId,typeMismatch.mypackage.domain.RoomType,typeMismatch]; arguments 
[org.springframework.context.support.DefaultMessageSourceResolvable: codes   [rooms.roomTypeId,roomTypeId]; arguments []; default message [roomTypeId]]; 
default message [Failed to convert property value of type 'java.lang.String' to required type   'mypackage.domain.RoomType' 
for property 'roomTypeId'; nested exception is java.lang.IllegalStateException: 
Cannot convert value of type [java.lang.String] to required  type [mypackage.domain.RoomType] for property 
'roomTypeId': no matching editors or  conversion strategy found]

ドメイン:

@Entity
@Table(name = "rooms")
@XmlRootElement
@NamedQueries({
@NamedQuery(name = "Rooms.findAll", query = "SELECT r FROM Rooms r")})
public class Rooms implements Serializable {
...
@JoinColumn(name = "RoomTypeId", referencedColumnName = "RoomTypeId")
@ManyToOne
private RoomType roomTypeId;
...
public RoomType getRoomTypeId() {
    return roomTypeId;
}
public void setRoomTypeId(RoomType roomTypeId) {
    this.roomTypeId = roomTypeId;
}     
} 

コントローラ:

@RequestMapping(value = "/room/add", method = RequestMethod.POST)
public String addRoom(@ModelAttribute("rooms") Rooms room,
        BindingResult result) {
    roomService.addRoom(room);
    return "redirect:/rooms";
}

私は試した:

@RequestMapping(value = "/room/add", method = RequestMethod.POST)
public String addRoom(@ModelAttribute("rooms") Rooms room, @RequestParam Long roomTypeId,
        BindingResult result) {
    room.setRoomTypeId(typeService.getType(roomTypeId.intValue()));
    roomService.addRoom(room);
    return "redirect:/rooms";
}

jsp:

<form:form method="post" action="room/add" commandName="room">
<table>
    <tr>
        <td><form:label path="name">
                <spring:message code="label.roomname" />
            </form:label></td>
        <td><form:input path="name" /></td>

    </tr>
    <tr>
        <td><form:label path="roomTypeId">
                <spring:message code="label.typeroom" />
            </form:label>

        </td>
        <td>
            <form:select path="roomTypeId" name="roomTypeId"> 
                <c:forEach items="${typeList}" var="type">
                    <form:option value="${type.roomTypeId}" label="${type.name}"/>
                </c:forEach>
            </form:select>
        </td>
    </tr>
    <tr>
        <td colspan="2"><input type="submit"
                               value="<spring:message code="label.addroom"/>" /></td>
    </tr>
</table>
</form:form>

これが私を示している理由を理解しています。しかし、私はそれを修正する方法がわかりません。私がそれを修正するのを手伝うか、私が間違っていることを教えてください。

4

1 に答える 1

0

次のメソッド宣言で

public String addRoom(@ModelAttribute("rooms") Rooms room, @RequestParam Long roomTypeId, BindingResult result...,

BindingResult引数の隣に引数を置きRoomsます。

于 2013-06-27T19:53:20.387 に答える