Reputation: 7658
I got error "BindingResult nor plain target object for bean name 'EmpPersonalBean' available as request attribute" when the logged successfully in the application using Spring MVC,JDBC(Version 2.5). I forward loginpage into jsp/UserPage.jsp
My folder struture is
WEB-ROOT
|__JSP (folder)
|__user (sub folder)
|_userdashboardpage.jsp
|__loginpage.jsp (In Webroot)
|__web.xml
Controller :UserController Class
In this controller called onsubmit method after successfully i redirected the page.check the below code snippet
public ModelAndView onSubmit(HttpServletRequest request, HttpServletResponse response,Object command,BindException errors) throws Exception {
String username="",password="";
username=request.getParameter("username");
password=request.getParameter("password");
UserBean ubean=null;
HttpSession session=request.getSession(true);
try{
ubean=userservice.chkUsername(username,password);
System.out.println("Information"+ubean.getUsername());
}catch(DataException ex){
ex.printStackTrace();
//throw ex;
}
session.setAttribute("User",ubean);
ModelAndView mv=new ModelAndView(("forward:jsp/UserPage.jsp"));
return mv;
}
In the UserPage.jsp
<%@ page language="java" import="com.aims.bean.*" contentType="text/html;charset=utf-8" pageEncoding="UTF-8"%>
<%@taglib uri="http://www.springframework.org/tags/form" prefix="form" %>
<%@taglib uri="http://www.springframework.org/tags" prefix="spring" %>
<%@taglib uri="/WEB-INF/tld/c.tld" prefix="c" %>
<html>
<head>
<title>AAI</title>
</head>
<body>
<form:form method="post" modelAttribute="EmpPersonalBean" action="userpage.htm">
<table>
<tr>
<td>Welcome <%=((UserBean)session.getAttribute("User")).getUsername()%></td>
</tr>
<tr>
<tr>
<td>Department</td>
<td><form:select path="deparment">
<form:option value="NONE" label="--- Select ---" />
<form:options items="${deparmentList}" />
</form:select>
</td>
</tr>
</tr>
</table>
</form:form>
</body>
</html>
In the userpage, trying to loaded deparment list using referenceData method from UserDBBoardController.
public class UserDBBoardController extends SimpleFormController{
private static final Log log=new Log(UserDBBoardController.class);
private UserService userservice;
public void setUserservice(UserService userservice) {
this.userservice = userservice;
}
public UserDBBoardController(){
setCommandClass(EmpPersonalBean.class);
setCommandName("EmpPersonalBean");
}
protected Map referenceData(HttpServletRequest request) throws Exception {
log.info("UserDBBoardController======================referenceData");
Map referenceData = new HashMap();
Map deparementList=new HashMap();
deparementList=userservice.getDeparmentList();
referenceData.put("deparmentList",deparementList);
return referenceData;
}
}
Dispatcher Servlet.xml:-
<?xml version="1.0" encoding="UTF-8"?>
<bean id="urlMapping" class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
<property name="mappings">
<props><prop key="/loginpage.htm">UserController</prop>
<prop key="/userpage.htm">UserDBBoardController</prop></props>
</property>
</bean>
<import resource="application-context.xml"/>
<bean id="User" class="com.aims.bean.UserBean" scope="session" />
<bean id="UserController" class="com.epis.controllers.UserController" >
<property name="userservice" ref="userservice"/>
<property name="sessionForm"><value>true</value></property>
<property name="commandName"><value>UserBean</value></property>
<property name="commandClass"><value>com.aims.bean.UserBean</value></property>
<property name="validator"><ref bean="userformValidator"/></property>
<property name="formView"><value>loginpage</value></property>
<property name="successView"><value>UserPage</value></property>
</bean>
<bean id="UserDBBoardController" class="com.epis.controllers.UserDBBoardController" >
<property name="userservice" ref="userservice"/>
<property name="sessionForm"><value>true</value></property>
<property name="commandName"><value>EmpPersonalBean</value></property>
<property name="commandClass"><value>com.aims.bean.EmpPersonalBean</value></property>
</bean>
This error message came from loading of deparment list in userpage?.How to resolve this issue?.Please help.
Another doubt is Why spring MVC provides too many types of controllers?
Upvotes: 0
Views: 805
Reputation: 3411
Try using This constructor for ModelAndView that also accepts Model Name and Model Object. http://static.springsource.org/spring/docs/2.5.x/api/org/springframework/web/servlet/ModelAndView.html#ModelAndView(java.lang.String, java.lang.String, java.lang.Object)
Upvotes: 1