0

我试图创建我的第一个 spring mvc 应用程序。我正在使用架构:Spring MVC-Service-DAO-Persistence Architecture。我收到了这个错误:

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 回答