Announcement Announcement Module
No announcement yet.
command object values doubling Page Title Module
Move Remove Collapse
Conversation Detail Module
  • Filter
  • Time
  • Show
Clear All
new posts

  • command object values doubling


    I have a the following situation:

    1. outer.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
    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


    • #3

      The Controller:
      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();	
      		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:
      <jsp:include page="/springpath/myURL" flush="true"/>
      spring config xml:
      <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 id="myHelper" class="com.mypackage.MyHelper" >
      	<property name="dao" ref="myDAO" />
      <bean id="myDAO" class="com.mypackage.MyDAO">
      <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" />
      <form:form action="" 
      		<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 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:checkbox path="check1" id="check1" />
      		<form:checkbox path="check2" id="check2" />
      		<input type="submit" value="Go" />
      		<form:hidden path="myParam" />

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


      • #4
        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
          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
            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.