Page 1 of 3 123 LastLast
Results 1 to 10 of 21

Thread: Having a problem with SimpleFormController

  1. #1
    Join Date
    Mar 2008
    Posts
    11

    Default Having a problem with SimpleFormController

    Hey there, noob here trying to figure out Spring MVC. Been doing a lot of searching here and closely examining the step by step Spring MVC tutorial and can't find the answer to my problem.

    I'm working on a site where the user logs in and then is directed to the home page. I've got the log in part working fine, but the re-direction to the home page is where I'm having problems.

    The home page is a form, so I'm using a SimpleFormController bean:

    Code:
    public class CalendarController extends SimpleFormController {
    	private CalendarManager calendarManager;
    	private WorkoutManager workoutManager;
    	public CalendarController() {
    		super();
    		setSupportedMethods(new String[] {METHOD_GET, METHOD_POST});
    		setCommandClass(Month.class);
    	}
    	 
    	private ModelAndView handleWorkout(Month m) {
    		return new ModelAndView("weightsWorkout");
    	}
    	
    	private ModelAndView handleCardio(Month m) {
    
    
    		return new ModelAndView("cardioCalendar", "month", m);
    	}
    	
    	private ModelAndView handleDiet(Month m) {
    
    
    		return new ModelAndView("dietCalendar", "month", m);
    	}
    	
    	public ModelAndView onSubmit(HttpServletRequest request, HttpServletResponse response, Object command, BindException errors)
    			throws Exception {
    		Month m = (Month)command;
    		if(m.getActionType().equals("cardio")) {
    			return handleCardio(m);
    		} else if(m.getActionType().equals("diet")) {
    			return handleDiet(m);
    		} else {
    			request.getSession().setAttribute("month", m);
    			return handleWorkout(m);
    		}
    	}
    	
    	
    	protected Object formBackingObject(HttpServletRequest request) throws ServletException {
    		Month m = this.getCalendarManager().initializeCalendar();
    		System.out.println("formBackingObject");
    		return m;
    
        }
    	
    
    	public CalendarManager getCalendarManager() {
    		return calendarManager;
    	}
    
    	public void setCalendarManager(CalendarManager calendarManager) {
    		this.calendarManager = calendarManager;
    	}
    
    	public WorkoutManager getWorkoutManager() {
    		return workoutManager;
    	}
    
    	public void setWorkoutManager(WorkoutManager workoutManager) {
    		this.workoutManager = workoutManager;
    	}
    	
    }
    here's my servlet xml:

    Code:
    <bean id="calendarController" class="com.thegymjournal.controller.CalendarController">
    		<property name="sessionForm"><value>true</value></property>
        	<property name="commandName"><value>month</value></property>
            <property name="commandClass"><value>com.thegymjournal.model.Month</value></property>
            <property name="calendarManager"><ref bean="calendarManager"/></property>
            <property name="workoutManager"><ref bean="workoutManager"/></property>
    	</bean>
    and finally my jsp:

    Code:
    <form:form method="post" action="home.htm" commandName="month">
    		<form:hidden path="selectedDate" />
    		<form:hidden path="actionType" />
    		<form:hidden path="action" />
    		<div id="calendarContainer" align="center">
    			<div class="week" align="center">
    				<input type="image" name="previousMonth"  src="./images/resultset_previous.png" onclick="javascript:document.forms[0].submit();"/>
    				<c:out value="${month.month}"/><c:out value=" ${month.intYear}"/>
    				<input type="image" name="nextMonth" src="./images/resultset_next.png" onclick="javascript:document.forms[0].submit();"/>
    			</div>
    			<div class="week">
    				<div class="dayTitleHeader">Sun</div>
    				<div class="dayTitleHeader">Mon</div>
    				<div class="dayTitleHeader">Tues</div>
    				<div class="dayTitleHeader">Wed</div>
    				<div class="dayTitleHeader">Thur</div>
    				<div class="dayTitleHeader">Fri</div>
    				<div class="dayTitleHeader">Sat</div>
    			</div>
    			<c:forEach var="day" items="${month.days}" varStatus="status">
    				<div>						
    					<div class="day">																
    						<div class="dayHeader">		
    								
    							<c:out value="${day.date}"/>
    							
    						</div>
    						<div align="left">
    							<c:if test="${day.date >= 1}">
    								<c:choose>
    									<c:when test="${(day.cardio == true) || (day.diet == true) || (day.weight == true)}">
    										<c:if test="${day.cardio == true}">
    											<input type="image" src="img/sneakers_32x32.gif" name="viewCardio" onclick="doSubmit('<c:out value="${day.calendarDate}"/>', 'view', 'cardio')"/>
    										</c:if>
    										<c:if test="${day.diet == true}">
    											<input type="image" src="img/grapes_32x32.gif" name="viewDiet" onclick="doSubmit('<c:out value="${day.calendarDate}"/>', 'view', 'diet')"/>
    										</c:if>
    										<c:if test="${day.weight == true}">
    											<input type="image" src="img/dumbbell_32x32.gif" name="viewWeights" onclick="doSubmit('<c:out value="${day.calendarDate}"/>', 'view', 'weights')"/>
    										</c:if>
    									</c:when>
    									<c:otherwise>
    										<input type="image" src="img/btn/icon_add_16x16.gif" name="addCardio" onclick="doSubmit('<c:out value="${day.calendarDate}"/>', 'add', 'cardio')"><c:out value="Cardio"/>
    										</br>
    										<input type="image" src="img/btn/icon_add_16x16.gif" name="addDiet" onclick="doSubmit('<c:out value="${day.calendarDate}"/>', 'add', 'diet')"><c:out value="Diet"/>
    										</br>
    										<input type="image" src="img/btn/icon_add_16x16.gif" name="addWeights" onclick="doSubmit('<c:out value="${day.calendarDate}"/>', 'add', 'weights')"><c:out value="Weights"/>
    									</c:otherwise>
    								</c:choose>
    							</c:if>
    						</div>																		
    					</div>	
    				</div>		
    			</c:forEach>
    		</div>
    	</form:form>
    I keep getting this error :

    org.apache.jasper.JasperException: java.lang.IllegalStateException: Neither BindingResult nor plain target object for bean name 'month' available as request attribute


    but I know why - my month object isn't getting bound to the form. I have over-ridden the formBackingObject method, but for some reason this method is never called - thus resulting in the error above.

    Any one see any reason why my formBackingObject method isn't being called? I know it's gotta be something simple and stupid on my part.

    TIA

    Matt

  2. #2
    Join Date
    Jan 2008
    Posts
    29

    Default

    Hi Matt,

    I am trying to figure out your issues. I think you are fowarding the another page which is JSP so it is not binding the command. So what is my suggestion is forward or redirect to a controller.

    For example instead of saying
    Solution One

    return new ModelAndView("dietCalendar", "month", m);
    use
    return new ModelAndView("calendarController");

    this will work..

    Solution Two
    otherwise set <property name="successView" value="calendarController"/> in the servlet xml and in the controller call like this

    return new ModelAndView(new RedirectView(getSuccessView()));


    Lets try

  3. #3
    Join Date
    Mar 2008
    Posts
    11

    Default

    hmmm I see what you're saying, but that didn't seem to work.

    maybe this can offer some insight - here is the login controller that gets called first. User supplies info, is validated and then directed to my home page.

    Code:
    public class LoginController extends SimpleFormController {
    
    	private LoginManager loginManger;
    	
    	public LoginController() {
    		super();
    		setSupportedMethods(new String[] {METHOD_GET, METHOD_POST});
    		setCommandClass(RegistrationTO.class);
    	}
    	
    
    	protected ModelAndView onSubmit(HttpServletRequest request, 
    			HttpServletResponse response, Object command,
    			BindException errors) throws Exception {
    
    		ModelAndView modelAndView = null;
    		RegistrationTO cmd = (RegistrationTO) command;
    		
    		RegistrationTO registration = getLoginManager().findByEmailAddressAndPassword(cmd.getEmailAddress(),cmd.getPassword());
    		if (registration != null) {
    			request.getSession(true).setAttribute("registration", registration);
    			modelAndView = new ModelAndView("home");
    		} else {
    			errors.reject("error.login.incorrect");
    			modelAndView = showForm(request, response, errors);
    		}
    		return modelAndView;
    
    	}
    	
    	protected Object formBackingObject(HttpServletRequest request) throws ServletException {
    		RegistrationTO registration = new RegistrationTO();
    		
    		System.out.println("LoginController.formBackingObject");
    		return registration;
    
        }
    
    	public LoginManager getLoginManager() {
    		return loginManger;
    	}
    
    	public void setLoginManager(LoginManager loginManger) {
    		this.loginManger = loginManger;
    	}
    
    }
    code in red is the call to the new controller that's not calling the formBackingObject method - maybe that's where I'm going wrong?

    thanks again!

    Matt




    Quote Originally Posted by sgowtham View Post
    Hi Matt,

    I am trying to figure out your issues. I think you are fowarding the another page which is JSP so it is not binding the command. So what is my suggestion is forward or redirect to a controller.

    For example instead of saying
    Solution One

    return new ModelAndView("dietCalendar", "month", m);
    use
    return new ModelAndView("calendarController");

    this will work..

    Solution Two
    otherwise set <property name="successView" value="calendarController"/> in the servlet xml and in the controller call like this

    return new ModelAndView(new RedirectView(getSuccessView()));


    Lets try

  4. #4
    Join Date
    Mar 2008
    Posts
    11

    Default

    let me add a little more to my last post.

    Here's the URL mapping for

    Code:
    modelAndView = new ModelAndView("home");
    in my servlet.xml:

    Code:
    <prop key="/home.htm">calendarController</prop>
    It's in the CalendarController bean that isn't getting the formBackingObject method called.

    Hopefully that makes sense and I didn't over complicate things.

    -Matt

  5. #5
    Join Date
    Jan 2008
    Posts
    29

    Default

    Ok. I guess You are getting the login data from a database, and validating whether the username and password is matching. The problem could be

    1. First try to execute the query findByEmailAddressAndPassword() in the DB directly. Is it returning any values for atleast happy path testing.

    2. "home" is mapped to what? A "JSP" or a "controller".

  6. #6
    Join Date
    Mar 2008
    Posts
    11

    Default

    in order to help isolate the problem, I've removed the log in validation and all the extra stuff.

    I *think* "home" is mapped to a controller, but the errors I'm seeing leads me to believe that's not the case.

    here is my entire servlet.xml

    Code:
    <?xml version="1.0" encoding="UTF-8"?>
    <!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN 2.0//EN"
    	"http://www.springframework.org/dtd/spring-beans-2.0.dtd">
    <beans>	
    	<bean id="loginController" class="com.thegymjournal.controller.LoginController">
    		<property name="sessionForm"><value>true</value></property>
            <property name="commandName"><value>command</value></property>
            <property name="commandClass"><value>com.thegymjournal.model.RegistrationTO</value></property>        
            <property name="loginManager"><ref bean="loginManager"/></property>
    	</bean>    
        <bean id="loginManager" class="com.thegymjournal.manager.LoginManager" />
        <bean id="loginValidator" class="com.thegymjournal.validation.LoginValidator" />
    	
    	<bean id="newRegistrationController" class="com.thegymjournal.controller.NewRegistrationController">
            <property name="registrationManager"><ref bean="registrationManager"/></property>
            <property name="calendarManager"><ref bean="calendarManager"/></property>
    	</bean>
    	<bean id="registrationManager" class="com.thegymjournal.manager.RegistrationManager" />  
        
        <bean id="menuController" class="com.thegymjournal.controller.MenuController">
          	<property name="calendarManager"><ref bean="calendarManager"/></property>
    	</bean>  
    	
    	<bean id="calendarController" class="com.thegymjournal.controller.CalendarController">
    		<property name="sessionForm"><value>true</value></property>
            <property name="commandName"><value>month</value></property>
            <property name="commandClass"><value>com.thegymjournal.model.Month</value></property>
            <property name="calendarManager"><ref bean="calendarManager"/></property>
            <property name="workoutManager"><ref bean="workoutManager"/></property>
    	</bean>    
    	<bean id="calendarManager" class="com.thegymjournal.manager.CalendarManager" />
        
    	
    	<bean id="workoutController" class="com.thegymjournal.controller.WorkoutController">
        	<property name="commandName"><value>day</value></property>
            <property name="commandClass"><value>com.thegymjournal.model.Day</value></property>
            <property name="formView"><value>workout</value></property>
            <property name="workoutManager"><ref bean="workoutManager"/></property>
    	</bean>    
    	<bean id="workoutManager" class="com.thegymjournal.manager.WorkoutManager" />
    	
    	<bean id="exerciseController" class="com.thegymjournal.controller.ExerciseController">
        	<property name="commandName"><value>command</value></property>
            <property name="commandClass"><value>com.thegymjournal.model.Exercise</value></property>
            <property name="exerciseManager"><ref bean="exerciseManager"/></property>
    	</bean>    
    	<bean id="exerciseManager" class="com.thegymjournal.manager.ExerciseManager" />
    	
    	<bean id="workoutTemplateController" class="com.thegymjournal.controller.WorkoutTemplateController">
        	<property name="commandName"><value>command</value></property>
            <property name="commandClass"><value>com.thegymjournal.model.Template</value></property>
            <!-- <property name="workoutTemplateManager"><ref bean="workoutTemplateManager"/></property> -->
    	</bean>    
    	<bean id="workoutTemplateManager" class="com.thegymjournal.manager.WorkoutTemplateManager" />
    
    	
    	
    	<bean id="urlFilenameViewController"
    		class="org.springframework.web.servlet.mvc.UrlFilenameViewController">
    	</bean>
    	
    	
    
    	<bean id="handlerMapping"
    		class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
    		<property name="mappings">
    			<props>				
    				<prop key="/index.htm">loginController</prop>
    				<prop key="/home.htm">calendarController</prop>
    				<prop key="/registration.htm">newRegistrationController</prop>
    				<prop key="/menu*.htm">menuController</prop>
    				<prop key="/weightsCalendar.htm">calendarController</prop>
    				<prop key="/weightsWorkout.htm">workoutController</prop>
    				<prop key="/weightsExercise.htm">exerciseController</prop>
    				<prop key="/weightsTemplate.htm">workoutTemplateController</prop>
    				<prop key="/cardioCalendar.htm">calendarController</prop>
    				<prop key="/*.htm">urlFilenameViewController</prop>
    			</props>
    		</property>
    	</bean>
    	
    	<bean id="messageSource" class="org.springframework.context.support.ResourceBundleMessageSource">
    		<property name="basename" value="message"></property>
    	</bean>
    
    	<bean id="exceptionResolver"
    		class="org.springframework.web.servlet.handler.SimpleMappingExceptionResolver">
    		<property name="exceptionMappings">
    			<props>
    				<prop key="java.lang.Exception">ErrorPage</prop>
    			</props>
    		</property>
    	</bean>
    	
    	<bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
            <property name="viewClass"><value>org.springframework.web.servlet.view.JstlView</value></property>
            <property name="prefix"><value></value></property>
            <property name="suffix"><value>.jsp</value></property>
        </bean>
    </beans>

  7. #7
    Join Date
    Jan 2008
    Posts
    29

    Default

    I am trying to fix it in "my way"


    Add this


    <bean id="loginController" class="com.thegymjournal.controller.LoginControlle r">
    <property name="sessionForm"><value>true</value></property>
    <property name="commandName"><value>command</value></property>
    <property name="commandClass"><value>com.thegymjournal.model .RegistrationTO</value></property>
    <property name="successView" value="calendarController"/>
    <property name="loginManager"><ref bean="loginManager"/></property>
    </bean>


    and in the controller do this

    return new ModelAndView(new RedirectView(getSuccessView()));

    and from servlet.xml remove this


    <prop key="/home.htm">calendarController</pro
    Last edited by sgowtham; Mar 9th, 2008 at 03:39 PM.

  8. #8
    Join Date
    Mar 2008
    Posts
    11

    Default

    getting closer I think.

    If I drop <prop key="/index.htm">loginController</prop> the index page won't work, I need that for the formBackingObject in the loginController.

    I did make the other changes you suggested and am now getting a 404 error 'The requested resource (/theGymJournal/calendarController) is not available.'

  9. #9
    Join Date
    Jan 2008
    Posts
    29

    Default

    Ok ,

    I am sorry dont remove the <prop key="/index.htm">. But remove the other one and do this modifcation in the in the servlet.xml


    <bean id="/theGymJournal/calendarController" class="com.thegymjournal.controller.CalendarContro ller">
    <property name="sessionForm"><value>true</value></property>
    <property name="commandName"><value>month</value></property>
    <property name="commandClass"><value>com.thegymjournal.model .Month</value></property>
    <property name="calendarManager"><ref bean="calendarManager"/></property>
    <property name="workoutManager"><ref bean="workoutManager"/></property>
    </bean>




    I hope this will definitly work..

  10. #10
    Join Date
    Mar 2008
    Posts
    11

    Default

    the other was removed and I added the suggestion above and got a new exception:
    org.xml.sax.SAXParseException: Attribute value "/theGymJournal/calendarController" of type ID must be a name.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •