Announcement Announcement Module
Collapse
No announcement yet.
IllegalStateException in MVC form Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • IllegalStateException in MVC form

    I'm getting the following error when I try to access a Spring MVC form I created:

    java.lang.IllegalStateException: Neither BindingResult nor plain target object for bean name 'roomSelection' available as request attribute

    Here's the controller:

    @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;
    }
    }

    And here's my JSP page form section:

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

    This seems to be a common beginner issue but I can't seem to figure out what I'm doing wrong.
    Any ideas?

    Thanks,

    Steve

  • #2
    Hello

    Next time use code tags

    1)
    Code:
    @ModelAttribute("roomSelection")
    protected RoomSelection getRoomSelection() {
    logger.debug("Creating a RoomSelection instance");
    return new RoomSelection();
    }
    Why the method and other methods are protected? Try changing to public

    2) Post the complete error stack trace.

    BTW consider to read the follow
    16.3.3.9 Using @SessionAttributes to store model attributes in the HTTP session between requests

    Comment

    Working...
    X