2013-06-27 46 views
0

我試圖創建我的第一個彈簧mvc應用程序。我使用的是架構:Spring MVC-Service-DAO-Persistence Architecture。 我已經得到這個錯誤:如何在Spring 3.0中使用依賴字段?

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> 

我明白爲什麼這會顯示我。但我不知道如何解決它。幫助我解決它或告訴我我做錯了什麼。

+0

繼續之前,在'public String addRoom(@ModelAttribute(「rooms」)房間房間,@RequestParam Long roomTypeId,BindingResult結果...',把'BindingResult'參數放在'Rooms'參數旁邊。 –

+0

非常感謝,這是工作=) – sokolov09

回答

0

以下方法聲明

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

把旁邊Rooms論點BindingResult說法。