java.lang.IllegalStateException:既不是BindingResult,也不是bean名称的普通目标对象

时间:2012-05-16 18:06:45

标签: spring-mvc

当我尝试访问我创建的Spring MVC表单时,我收到以下错误:

java.lang.IllegalStateException:BindingResult和bean名称'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>

这似乎是一个常见的初学者问题,但我似乎无法弄清楚我做错了什么。 有什么想法吗?

谢谢,

史蒂夫

1 个答案:

答案 0 :(得分: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 bean

public class RoomSelection {
    private String roomName;

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

    public String getRoomName() {
        return roomName;
    }

}

的welcome.jsp

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

我的输出

My Output:

我的代码中唯一有嫌疑人的地方是你的post方法,当验证失败时你将模型视图设置为WELCOME_VIEW,我想知道在这段时间你是否会调用@ModelAttribute并且会它是否添加roomSelection属性。再一次猜测。