2012-05-16 4 views
0

나는 스프링 MVC의 내가 만든 양식에 액세스하려고 할 때 다음과 같은 오류를 받고 있어요에 대한 일반 대상 객체 어느 :java.lang.IllegalStateException : BindingResult도 빈 이름

java.lang.IllegalStateException를 : 어느 BindingResult도 'roomSelection'을 사용할 수 요청 속성 여기

같은 콩 이름에 대한 일반 대상 객체는 컨트롤러의 :

@Controller 
@RequestMapping("/welcome") 
public class RoomSelectionListController { 

final private static String WELCOME_VIEW  = "welcome"; 
final private static String SUCCESS_VIEW  = "chatroom"; 

// Value will be injected. 
private ChatRoomRegistryService chatRoomRegistryService = null; 
private RoomSelectionValidator roomSelectionValidator = null; 

private static Logger logger = Logger.getLogger(RoomSelectionListController.class); 

public ChatRoomRegistryService getChatRoomRegistryService() { 
    return chatRoomRegistryService; 
} 

@Autowired 
public void setChatRoomRegistryService(ChatRoomRegistryService chatRoomRegistryService) { 
    this.chatRoomRegistryService = chatRoomRegistryService; 
} 

@Autowired 
public RoomSelectionListController(RoomSelectionValidator roomSelectionValidator) { 
    this.roomSelectionValidator = roomSelectionValidator; 
} 

@ModelAttribute("roomSelection") 
protected RoomSelection getRoomSelection() { 
    logger.debug("Creating a RoomSelection instance"); 
    return new RoomSelection(); 
} 

@ModelAttribute("chatRoomList") 
protected List<ChatRoom> populateChatRoomList(HttpServletRequest request) throws Exception{ 
    logger.debug("Creating a chatRoomList"); 
    User user = (User) request.getSession().getAttribute("userLoggedIn"); 
    List<ChatRoom> chatRoomsForUser = chatRoomRegistryService.getChatRoomsForUser(user); 

    return chatRoomsForUser; 
} 

@RequestMapping(method = RequestMethod.POST) 
protected String processSubmit(@ModelAttribute("roomSelection") RoomSelection roomSelection, BindingResult result, ModelMap model, HttpServletRequest request, HttpServletResponse response) throws Exception { 

    roomSelectionValidator.validate(roomSelection, result); 
    if (result.hasErrors()) { 
     return WELCOME_VIEW; 
    } else { 
     model.addAttribute("chatroom", roomSelection.getChatRoom()); 
     User user = (User) request.getSession().getAttribute("userLoggedIn"); 
     model.addAttribute("userLoggedIn", user); 

     return SUCCESS_VIEW; 
    } 
} 

@RequestMapping(method = RequestMethod.GET) 
protected String initForm(ModelMap model) throws Exception { 
    logger.debug("Inside RoomSelectionListController.initForm()"); 
    RoomSelection roomSelection = new RoomSelection(); 
    model.addAttribute("roomSelection", roomSelection); 

    return WELCOME_VIEW; 
} 
} 

을 그리고 여기에 JSP의 내 양식 섹션의 :

<form:form id="joinForm" modelAttribute="roomSelection" method="POST"> 
    <table> 
     <legend><spring:message code="welcome.chatroom.list.lbl" /></legend> 
     <tr> 
      <td> 
       <form:select id="selectChatRoomList" path="chatRoomId" size="7" cssClass="chatRoomList"> 
        <form:options items="${chatRoomList}" itemValue="chatRoomId" 
         itemLabel="chatRoomName" /> 
       </form:select> 
      </td> 
     </tr> 
     <tr> 
      <td><form:errors path="chatRoomId" cssClass="advchatError" /></td> 
     </tr> 
     <tr> 
      <td> 
       <a id="submitJoinChatRoom" href="javascript:void(0)" class="green-button" onclick="$('#joinForm').submit();"> 
        <span class="green-button-outer"> 
         <span class="green-button-inner"> 
          <div class="joinRoomButtonWidth"><spring:message code="welcome.joinchat.lbl" /></div> 
         </span> 
        </span> 
       </a> 
      </td> 
     </tr> 
    </table> 
</form:form> 

이것은 일반적인 초보자 문제인 것처럼 보이지만 내가 잘못하고있는 것을 파악할 수는 없습니다. 아이디어가 있으십니까?

감사합니다,

스티브

+0

제출 또는 가져 오는 중에 오류가 발생합니까? 또한 나는 당신이 @ModelAttribute를 메서드와 함께 사용하고 ModelMap에 속성을 추가하는 것을 혼동하고있다. 하나에 붙어. – raddykrish

+0

실제로보기가 호출 된 방식으로 문제가 판명되었습니다. 이보기를 호출하는 컨트롤러에서 "redirect :"를 추가하면 정상적으로 작동하는 것 같습니다. 하지만 여전히 혼란 스럽다. 정확히 리디렉션해야하는 이유는 무엇입니까? ModelMap 또는 @ModelAttribute를 고수해 주셔서 감사합니다. – SteveN

답변

0

@SteveN

난 그냥 코드를 가져다가 그 간단한 (제거하는 의미 그 서비스 종속성을하고 문제를 파악하기 위해 혼자 단지 get 메소드를했다)했다. 그것은 매력처럼 작동합니다. 코드를 간단하게 만들 수 있고, 작업을 완료하면 코드를 추가 할 수 있습니다. 여기

컨트롤러

@Controller 
@RequestMapping("/welcome") 
public class RoomSelectionListController { 
    final private static String WELCOME_VIEW  = "welcome"; 

    @RequestMapping(method = RequestMethod.GET) 
    protected String initForm(ModelMap model) throws Exception { 
     RoomSelection roomSelection = new RoomSelection(); 
     roomSelection.setRoomName("MyRoom"); 
     model.addAttribute("roomSelection", roomSelection); 
     return WELCOME_VIEW; 
    } 
} 

RoomSelection 콩

public class RoomSelection { 
    private String roomName; 

    public void setRoomName(String roomName) { 
     this.roomName = roomName; 
    } 

    public String getRoomName() { 
     return roomName; 
    } 

} 

내 출력

<form:form id="joinForm" modelAttribute="roomSelection" method="POST"> 
    <table> 
     <tr> 
      <td>Room Name : </td> 
      <td> 
       <form:input path="roomName"/> 
      </td> 
     </tr> 
    </table> 
</form:form> 

위해 welcome.jsp 내 코드입니다

My Output:

귀하의 코드에서 용의자가있는 유일한 장소는 유효성 검사가 실패했을 때 WELCOME_VIEW로 모델보기를 설정하면 게시자가 @ModelAttribute이 호출 될지 궁금합니다. roomSelection 속성. 다시 한번 그 추측.

관련 문제