0

当我尝试访问我创建的 Spring MVC 表单时出现以下错误:

java.lang.IllegalStateException:Bean 名称“roomSelection”的 BindingResult 和普通目标对象都不能用作请求属性

这是控制器:

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

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

谢谢,

史蒂夫

4

1 回答 1

0

@史蒂夫N

我只是把你的代码简化了(意味着删除了那些服务依赖项,只有一个 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;
    }

}

欢迎.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>

我的输出

我的输出:

我在您的代码中唯一怀疑的地方是您的 post 方法,当验证失败时,您将模型视图设置为 WELCOME_VIEW,我想知道在此期间您是否@ModelAttribute会被调用并且它是否会添加roomSelection属性。又是一个猜测。

于 2012-05-17T19:00:51.720 回答