Announcement Announcement Module
Collapse
No announcement yet.
sessionForm data not updating with request data Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • sessionForm data not updating with request data

    The formBackingObject() method is getting called once, when the form is first displayed. The data is getting set correctly and making it back to the form and getting displayed. But when I edit the form data and submit the form, the new data is not being reflected in the command object that is passed to the doSubmitAction() method. I have sessionForm set to true and the debug I added to formBackingObject is only printing once, so I don't think it is getting reinitialized, but rather just never getting updated. I'm baffled. The controller code, xml, and jsp are included below.

    Thanks, Ben

    Code:
    package recipes.web.controllers;
    
    import javax.servlet.http.HttpServletRequest;
    
    import org.springframework.web.servlet.mvc.SimpleFormController;
    
    import recipes.data.Recipe;
    import recipes.services.RecipeService;
    
    public class SaveRecipeController extends SimpleFormController {
    	protected RecipeService recipeService;
    
    	public RecipeService getRecipeService() {
    		return recipeService;
    	}
    
    	public void setRecipeService(RecipeService recipeService) {
    		this.recipeService = recipeService;
    	}
    
    	protected Object formBackingObject(HttpServletRequest request)
    	throws Exception {
    		System.out.println("Called formBackingObject");
    		String id_str = request.getParameter("id");
    		if (id_str == null) {
    			return new Recipe();
    		} else {
    			Long id = Long.valueOf(id_str);
    			return this.recipeService.get(id);
    		}
    	}
    
    	protected void doSubmitAction(Object command) 
    	throws Exception {
    		Recipe recipe = (Recipe) command;
    		System.out.println("Saving recipe: " + recipe);
    		this.recipeService.save(recipe);
    	}
    }
    Code:
    <?xml version="1.0" encoding="UTF-8"?>
    <!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN"
    	"http&#58;//www.springframework.org/dtd/spring-beans.dtd">
    	
    <beans>
    
    	<bean name="/viewRecipes.htm"
    			class="recipes.web.controllers.ViewRecipesController">
    		<property name="recipeService">
    			<ref bean="recipeService"/>
    		</property>
    	</bean>
    
    	<bean name="/viewRecipe.htm"
    			class="recipes.web.controllers.ViewRecipeController">
    		<property name="recipeService">
    			<ref bean="recipeService"/>
    		</property>
    	</bean>
    
    	<bean name="/saveRecipe.htm"
    			class="recipes.web.controllers.SaveRecipeController">
    		<property name="recipeService">
    			<ref bean="recipeService"/>
    		</property>
    		<property name="formView">
    			<value>recipeForm</value>
    		</property>
    		<property name="successView">
    			<value>viewRecipe</value>
    		</property>
    		<property name="validator">
    			<bean class="recipes.web.validation.RecipeValidator"/>
    		</property>
    		<property name="bindOnNewForm">
    			<value>true</value>
    		</property>
    		<property name="sessionForm">
    			<value>true</value>
    		</property>
    		<property name="commandName">
    			<value>recipe</value>
    		</property>
    		<property name="commandClass">
    			<value>recipes.data.Recipe</value>
    		</property>
    	</bean>
    
    	<bean id="viewResolver"
    			class="org.springframework.web.servlet.view.InternalResourceViewResolver">
    		<property name="prefix">
    			<value>/WEB-INF/views/</value>
    		</property>
    		<property name="suffix">
    			<value>.jsp</value>
    		</property>
    		<property name="viewClass">
    			<value>org.springframework.web.servlet.view.JstlView</value>
    		</property>
    	</bean>
    
    	<bean id="recipeService"
    			class="recipes.services.RecipeServiceImpl">
    		<constructor-arg>
    			<ref bean="recipeDao"/>
    		</constructor-arg>
    	</bean>
    
    	<bean id="dataSource"
    			class="org.springframework.jndi.JndiObjectFactoryBean">
    		<property name="jndiName">
    			<value>java&#58;comp/env/jdbc/RecipesDB</value>
    		</property>
    	</bean>
    	
    	<bean id="sessionFactory"
    			class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
    		<property name="dataSource">
    			<ref bean="dataSource"/>
    		</property>
    		<property name="hibernateProperties">
    			<props>
    				<prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
    				<prop key="show_sql">true</prop>
    			</props>
    		</property>
    		<property name="mappingDirectoryLocations">
    			<value>classpath&#58;/recipes/data/hibernate</value>
    		</property>
    	</bean>
    
    	<bean id="hibernateTemplate"
    			class="org.springframework.orm.hibernate3.HibernateTemplate">
    		<property name="sessionFactory">
    			<ref bean="sessionFactory"/>
    		</property>
    	</bean>
    	
    	<bean id="recipeDao"
    			class="recipes.data.hibernate.RecipeDaoImpl">
    		<property name="hibernateTemplate">
    			<ref bean="hibernateTemplate"/>
    		</property>
    	</bean>
    
    </beans>
    Code:
    <%@ page contentType="text/html;charset=UTF-8" language="java" %>
    
    <%@ taglib prefix="core" uri="http&#58;//java.sun.com/jsp/jstl/core" %>
    <%@ taglib prefix="spring" uri="http&#58;//www.springframework.org/tags" %>
    
    <form action="saveRecipe.htm" method="POST">
    
    <spring&#58;hasBindErrors name="recipe">
    	<core&#58;forEach var="err" items="$&#123;errors.globalErrors&#125;">
    		<span class="error"><core&#58;out value="$&#123;err.defaultMessage&#125;"/></span><br />
        </core&#58;forEach>
    </spring&#58;hasBindErrors>
    
    <core&#58;if test="$&#123;!empty recipe.id&#125;">
    	<input type="hidden" name="id" value="$&#123;recipe.id&#125;"/>
    </core&#58;if>
    
    <spring&#58;bind path="recipe.name">
    	Name&#58;
    	<input type="text" name="firstname" value="$&#123;status.value&#125;">
        <span class="error">$&#123;status.errorMessage&#125;</span>
    </spring&#58;bind>
    <br />
    
    <input type="Submit" value="Save"/>
    </form>

  • #2
    But when I edit the form data and submit the form, the new data is not being reflected in the command object that is passed to the doSubmitAction()
    This looks like a binding issue. But not sure where. :?

    I have sessionForm set to true and the debug I added to formBackingObject is only printing once, so I don't think it is getting reinitialized, but rather just never getting updated
    Thats OK. Take a look at the workflow.

    Comment


    • #3
      Thanks for looking at this. Turns out I just named the input wrong in the form html. I had to keep overriding things and printing debugs all the way down to when it first examines the request. Thats when I saw that the parameter name was wrong. Ouch! Thanks again for your help.

      -Ben

      Comment

      Working...
      X