Results 1 to 6 of 6

Thread: command object values doubling

  1. #1

    Default command object values doubling

    Hi,

    I have a the following situation:

    1. outer.jsp
    inner.jsp.
    Outer.jsp includes inner.jsp.

    2. inner.jsp has a form which is bound with <spring:form> tags to a command object - formPOJO

    3. formPOJO has a parameter - "myParam".

    4. There is a controller MyController which extends SimpleFormController and has 3 methods
    a) onSubmit()
    b) formBackingObject()
    c) showForm() which calls formBackingObjects to return a command object, and then uses this command as argument to onSubmit(), and returns the MAV from the onSubmit().

    5. Initially, when I access my page from another page
    a) formBackingObject() is called and formPOJO (the command object) is initialized from request parameters and myParam=123.

    b) showForm() is called, which calls formBackingObject() again and then calls onSubmit(), and myParam is still = 123.

    6. Then I get my page displayed. I check some checkboxes and hit the submit button, which submits the form to the same URL - outer.jsp, with inner url passed as parameter used in the <jsp:include>.
    7. Then the code trace is as follows:
    a) formBackingObject() is called. myParam = 123
    b) onSubmit() is called: AND HERE IS THE PROBLEM. myParam in the command object is now = "123, 123"

    I can't figure out why it doubles...

  2. #2
    Join Date
    Aug 2006
    Location
    Arequipa-Peru / South America
    Posts
    2,796

    Default

    AND HERE IS THE PROBLEM. myParam in the command object is now = "123, 123"
    I recall this problem, procede to post source code inside of code tags
    - Manuel Jordan

    Kill Your Pride, Share Your Knowledge With All
    The Fear Of The LORD Is The Beginning Of Knowledge, But Fools Despise Wisdom And Discipline. Proverbs 1:7

    Blog


    Technical Reviewer of Apress

    • Pro SpringSource dm Server
    • Spring Enterprise Recipes: A Problem-Solution Approach
    • Spring Recipes: A Problem-Solution Approach, 2nd Edition
    • Pro Spring Integration
    • Pro Spring Batch
    • Pro Spring 3
    • Pro Spring MVC: With Web Flow
    • Pro Spring Security

  3. #3

    Default Code

    The Controller:
    Code:
    public class MyController extends SimpleFormController
    {	
    	
    	private MyHelper helper;
    	
    	public MyHelper getHelper() 
    	{
    		return helper;
    	}
    
    	public void setHelper(MyHelper helper) 
    	{
    		this.helper = helper;
    	}
    
    	protected ModelAndView onSubmit(HttpServletRequest request, HttpServletResponse response, Object command, BindException errors)
    	{
    		MyFormPOJO myRequestPOJO = (MyFormPOJO)command;
    		
    		MyDataPOJO detailsData = helper.getMyModel(myRequestPOJO);
    		
    		ModelAndView modelAndView = new ModelAndView(getSuccessView(), "detailsData", detailsData);
    		modelAndView.addObject("myRequestPOJO", myRequestPOJO);
    
    		return modelAndView ;
    	}
    	
    	protected Object formBackingObject(HttpServletRequest request)
    	{
    		MyFormPOJO reportRequestPOJO = new MyFormPOJO();	
    	myRequestPOJO.myParam(request.getParameter("myParam").trim());
    
    ...
    		
    		
    		return myRequestPOJO;
    	}
    	
    	protected ModelAndView showForm(HttpServletRequest request, HttpServletResponse response, BindException errors, Map controlModel)
    	{
    		MyFormPOJO myRequestPOJO = (MyFormPOJO)formBackingObject(request);
    		return onSubmit(request,response,myRequestPOJO,errors);
    	}
    JSP Code:
    Code:
    //outer.jsp
    
    <jsp:include page="/springpath/myURL" flush="true"/>
    spring config xml:
    Code:
    <bean name="/myURL" class="com.mypackage.MyController">
    		<property name="sessionForm" value="true" />
    		<property name="commandName" value="myRequestPOJO" />
    		<property name="commandClass" value="com.mypackage.pojo.MyFormPOJO" />
    		<property name="formView" value="inner" />
    		<property name="successView" value="inner" />
    		<property name="helper" ref="myHelper" />
    </bean>
    
    <bean id="myHelper" class="com.mypackage.MyHelper" >
    	<property name="dao" ref="myDAO" />
    </bean>
    	
    <bean id="myDAO" class="com.mypackage.MyDAO">
    </bean>
    	
    <bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
    	<property name="viewClass" value="org.springframework.web.servlet.view.JstlView" />
    		<property name="prefix" value="/jsp/" />
    		<property name="suffix" value=".jsp" />
    	</bean>
    inner.jsp:
    Code:
    <form:form action="" 
    		method="post" 
    		commandName="myRequestPOJO" 
    		name="myForm" 
    		id="myForm" 
    		cssClass="myForm">
    
    		<form:select path="select1" id="select1">
    		        <form:option value="1">1</form:option>
    			<form:option value="2">2</form:option>
    			<form:option value="3">3</form:option>
    		</form:select>
    
    		<form:select path="select2" id="select2">
    			<form:option value="a">a</form:option>
    			<form:option value="b">b</form:option>
    			<form:option value="c">c</form:option>
    		</form:select>
    
    		<form:checkbox path="check1" id="check1" />
    		<form:checkbox path="check2" id="check2" />
    
    		<input type="submit" value="Go" />
    
    		<form:hidden path="myParam" />
    		...				
    </form:form>
    MyFormPOJO:

    Code:
    public class MyFormPOJO 
    {
    	private String myParam;
    
    
    
    	public String getMyParamID() 
    	{
    		return myParam;
    	}
    	
    	public void setMyParam(String myParam) 
    	{
    		this.myParam= null;
    		this.myParam= myParam;
    	}
    
    ...
    
    }

  4. #4

    Default

    I am getting some interesting results:

    When I output some variables from the various methods, the output is double as well.
    Looks like the request hits the Controller twice.
    Is it possible that it happens because the form gets submitted to the outer.jsp, while the form itself is <jsp:include>'ed in the outer jsp?

  5. #5

    Default

    So basically, I am getting beans that are stored in memory and their fields get appended to instead of reset.

    Could anyone please tell me why?

  6. #6
    Join Date
    Mar 2006
    Posts
    312

    Default

    You're not explicitly setting the action in your form so it's posting to the current url which has the query string param myParam=123. Your form also has a hidden field for myParam with a value of 123. As a result of having two values for "mayParam" in the request they will be submitted as a list of comma separated values.

Posting Permissions

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