Spring mvc java.lang.IllegalStateException:bean名称既不是BindingResult也不是普通目标对象
当我尝试访问我创建的Spring MVC表单时,出现以下错误: java.lang.IllegalStateException:bean名称“roomSelection”的BindingResult和普通目标对象都不能作为请求属性使用 这是控制器:Spring mvc java.lang.IllegalStateException:bean名称既不是BindingResult也不是普通目标对象,spring-mvc,Spring Mvc,当我尝试访问我创建的Spring MVC表单时,出现以下错误: java.lang.IllegalStateException:bean名称“roomSelection”的BindingResult和普通目标对象都不能作为请求属性使用 这是控制器: @Controller @RequestMapping("/welcome") public class RoomSelectionListController { final private static String WELCOME_VIEW
@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;
}
}
@控制器
@请求映射(“/welcome”)
公共类RoomSelectionListController{
最终私有静态字符串WELCOME\u VIEW=“WELCOME”;
最终私有静态字符串成功\u VIEW=“聊天室”;
//价值将被注入。
私有聊天室注册服务聊天室注册服务=null;
private RoomSelectionValidator RoomSelectionValidator=null;
私有静态记录器=Logger.getLogger(RoomSelectionListController.class);
公共聊天室注册服务getChatRoomRegistryService(){
返回聊天室注册服务;
}
@自动连线
public void setChatRoomRegistryService(聊天室注册服务聊天室注册服务){
this.chatRoomRegistryService=chatRoomRegistryService;
}
@自动连线
公共RoomSelectionListController(RoomSelectionValidator RoomSelectionValidator){
this.roomSelectionValidator=roomSelectionValidator;
}
@ModelAttribute(“roomSelection”)
受保护的RoomSelection getRoomSelection(){
debug(“创建RoomSelection实例”);
返回新的RoomSelection();
}
@ModelAttribute(“聊天室列表”)
受保护列表PopulateCharToomList(HttpServletRequest请求)引发异常{
debug(“创建聊天室列表”);
User=(User)request.getSession().getAttribute(“userLoggedIn”);
List chatRoomsForUser=chatRoomRegistryService.getChatRoomsForUser(用户);
返回聊天室;
}
@RequestMapping(method=RequestMethod.POST)
受保护的字符串processSubmit(@ModelAttribute(“roomSelection”)roomSelection roomSelection、BindingResult结果、ModelMap模型、HttpServletRequest请求、HttpServletResponse响应)引发异常{
roomSelectionValidator.validate(roomSelection,结果);
if(result.hasErrors()){
返回您的视图;
}否则{
model.addAttribute(“聊天室”,roomSelection.getChatRoom());
User=(User)request.getSession().getAttribute(“userLoggedIn”);
model.addAttribute(“userLoggedIn”,user);
返回成功视图;
}
}
@RequestMapping(method=RequestMethod.GET)
受保护的字符串initForm(ModelMap模型)引发异常{
debug(“Inside RoomSelectionListController.initForm()”;
RoomSelection RoomSelection=新建RoomSelection();
model.addAttribute(“roomSelection”,roomSelection);
返回您的视图;
}
}
这是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>
这似乎是一个常见的初学者问题,但我似乎不知道我做错了什么。
有什么想法吗
谢谢
史蒂夫@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;
}
}
菜豆
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>
房间名称:
我的输出
我唯一怀疑您的代码的地方是您的post方法,当验证失败时,您将模型视图设置为欢迎视图,我想知道在此期间是否会调用您的
@modeldattribute
,是否会添加roomSelection
属性。这也是猜测。是在提交或获取过程中发生的错误。此外,我还发现您正在混合使用@modeldattribute和方法,并在ModelMap中添加属性。坚持一个。事实上,问题在于视图的调用方式。在调用此视图的控制器中,我添加了“redirect:”,它似乎工作正常。不过,这仍然令人困惑。为什么我需要准确地重定向?感谢您对坚持使用ModelMap或@ModelAttribute的评论。