Reputation: 31
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 form section of 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>
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
Upvotes: 0
Views: 1654
Reputation: 1866
@SteveN
I just took your code and made it simple (meaning removed those service dependencies and had just a get method alone to figure out the issue). It works like charm. May be you can also make your code simple and then keep adding things once you have a working thing in place. Here is my code
Controller
@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
The only place which i have a suspect in your code is in your post method, when the validation fails you are setting the model view as WELCOME_VIEW, i wonder whether during this time you @ModelAttribute
will be called and will it add the roomSelection
attribute or not. Again its a guess.
Upvotes: 0