Results 1 to 8 of 8

Thread: Best way to bind data to command object and return to the same page

  1. #1

    Default Best way to bind data to command object and return to the same page

    Can anyone point me in the direction of some documentation that explains how to do this please.

  2. #2
    Join Date
    Oct 2005
    Location
    Mobile, AL
    Posts
    345

    Default

    What version of Spring are you using? Typically you define a Command object and then define a controller that you want to use to do the binding on the Command object.

  3. #3

    Default

    Hi

    Just to clarify, I'm using a form controller, and succesfully loading data in the "formView" from the command object that is being populated by the form controller. What I want to do is submit the data and have the same view refreshed with my changes. When you do this you get errors about the command object being unavailable (which makes sense I guess if you look at the way the controller works). I'm using Spring 2.5.

    My question mainly is - what is the best way to use the command object concept (so that you can use the Spring tags and bind data really easily etc), and update your data, then return to the same page with the data refreshed?
    Or do I just use a simple conroller and submit the form old school style?

  4. #4
    Join Date
    Oct 2004
    Posts
    151

    Default

    Hi!

    If you subclass some of the Spring superclasses for formControllers you can try to call

    Code:
    return showForm(request, response, errors);
    in onSubmit.

    Then you should get back to the formView.


    -Kaj

  5. #5
    Join Date
    May 2005
    Posts
    288

    Default

    If you are using SimpleFormController, you might want to think about overriding isFormChangeRequest and onFormChange. These methods are there for exactly the use case you described.

  6. #6

    Default

    Ok! I just did this.

    Code:
    protected void onFormChange(ActionRequest request, ActionResponse response, Object command) {
    
    //NB No code!
    	   
    	   
       }
    and removed the onSubmitAction method entirely

    Now it just calls formBackingObject again when you submit the form. However, my formBackingObject doesn't include a call to personDao.savePerson. Yet the data is saved!!! How please??? Don't get me wrong, it does exactly what I want, but how?

    And I didn't implement isFormChangeRequest(PortletRequest request) either. Is that bad?

  7. #7
    Join Date
    May 2005
    Posts
    288

    Default

    Are you sure, that you invoked the most recent version of your controller?
    If you don't implement isFormChangeRequest(), onFormChange will never be called. The default implementation always returns false.
    That's of course an assumption, as you didn't post your code.

  8. #8

    Default

    Here's the code

    Code:
    package com.example.web;
    
    import java.util.ArrayList;
    import java.util.List;
    
    import javax.portlet.ActionRequest;
    import javax.portlet.ActionResponse;
    import javax.servlet.http.HttpServletRequest;
    
    import org.apache.commons.logging.Log;
    import org.apache.commons.logging.LogFactory;
    import org.springframework.stereotype.Controller;
    import org.springframework.validation.BindException;
    import org.springframework.web.servlet.ModelAndView;
    import org.springframework.web.servlet.mvc.SimpleFormController;
    
    import com.example.dao.DogDao;
    import com.example.dao.PersonDao;
    import com.example.model.Dog;
    import com.example.model.Person;
    
    
    
    public class PersonDogsFormController extends SimpleFormController
    {
        public PersonDogsFormController() {
        	this.setCommandClass(Person.class);
        	this.setCommandName("person");
        }
    	
        private DogDao dogDao;
        private PersonDao personDao;
        String str2;
        
    	protected final Log logger = LogFactory.getLog(getClass());
       
       public Object formBackingObject(HttpServletRequest request) throws Exception
       {
    	   
    	  System.out.println("This logging appears each time update is pressed ");
    	  Person p =  (Person) personDao.find(Person.class, Long.parseLong(request.getParameter("id")));
    	  	  
    	  List dogList = new ArrayList();
    	  
    	  dogList = dogDao.findQuery("from Dog as d where d.person_id = "+ p.getId() + " order by d.name ");
    	  
    	  request.setAttribute("theDogs", dogList);
    	  
    	  return p;
    	      
          
       }
       
       protected void onFormChange(ActionRequest request, ActionResponse response, Object command) {
    	   
    	   
       }
       
       // ModelAndView onSubmit(Object command, BindException bindException)
       //{
          //Person givenData = (Person) command;
          
          /* The givenData object now contains the successfully validated data from the
           * form, so can be written to a database, or whatever.
           */
          
          /*
          
          personDao.savePerson(givenData);
          
          Person p = (Person) personDao.find(Person.class, givenData.getId());
          
          return new ModelAndView(getSuccessView(), "model", p);  
          */
      // }
    
    public DogDao getDogDao() {
    	return dogDao;
    }
    
    public void setDogDao(DogDao DogDao) {
    	this.dogDao = DogDao;
    }
    
    public PersonDao getPersonDao() {
    	return personDao;
    }
    
    public void setPersonDao(PersonDao personDao) {
    	this.personDao = personDao;
    }
    }

    Code:
    <%@ page import="java.util.*,com.example.model.*" %>
    <%@ taglib uri="http://java.sun.com/jstl/core_rt" prefix="c" %>
    <%@ taglib uri="http://www.springframework.org/tags/form" prefix="form" %>
    
    <html>
    <head>
    <style type="text/css">
    div#test {position:absolute; right:0; top:0; repeat:y; background:#193b8b; width:180px; height:1000px}
    div#test2 {position:absolute; left:0; top:0; repeat:y; background:#193b8b; width:180px; height:1000px}
    div#container {position:absolute; padding-left:50px; left:180; right:180; top:0; repeat:y; background:gray; height:1000px}
    div#header {text-align:center}
    
    </style>
    </head>
    
    <body>
    
    <div id="test">
    </div>
    
    <div id="container">
    <div id="header">
    <h1>Test Page</h1>
    </div>
    
    
    <h1 align="center">Person's Dogs</h1>
    <br>
    
    <form:form method="post" action="persondogs.htm" commandName="person">
          
             
          Name </br><form:input path="name" />
            <form:hidden path="id"/>
             
          
          
    
    
    
       
    </br>
    <p>
    
    
    Dogs
    </br>
    <c:if test="${!empty person.dogs}">
    
    
    </c:if> 
    </br>
    
    <c:forEach items="${theDogs}" var="i" varStatus="itemsRow">
       
       <form:input path="dogs[${i.id}].name"/><form:input path="dogs[${i.id}].type"/><form:input path="dogs[${i.id}].id"/></br>
    </c:forEach>
    </br>
    
    <input type="submit" value="Update" />
    
    
    </form:form>
    
    
    
    
    
    
    </div>
    <div id="test2">
    </div>
    
    
    
    <
    </body>
    </html>
    Code:
    May 26, 2010 5:48:32 PM org.apache.catalina.startup.HostConfig checkResources
    INFO: Reloading context [/HelloWorld]
    log4j:WARN No appenders could be found for logger (org.springframework.web.context.ContextLoader).
    log4j:WARN Please initialize the log4j system properly.
    This logging appears each time update is pressed 
    DEBUG in dao
    This logging appears each time update is pressed 
    DEBUG in dao
    This logging appears each time update is pressed 
    DEBUG in dao
    This logging appears each time update is pressed 
    DEBUG in dao
    This logging appears each time update is pressed 
    DEBUG in dao
    This logging appears each time update is pressed 
    DEBUG in dao
    This logging appears each time update is pressed 
    DEBUG in dao
    This logging appears each time update is pressed 
    DEBUG in dao
    This logging appears each time update is pressed 
    DEBUG in dao
    This logging appears each time update is pressed 
    DEBUG in dao

Posting Permissions

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