Announcement Announcement Module
Collapse
No announcement yet.
SimpleForm preload command properties Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • SimpleForm preload command properties

    Hi!

    I've a command form object that contains some informations and that include a list of countries that I wanna render as a combobox.

    My SimpleFormController have a "service" with a getData method that returns a list of Countries and I wanna load this country list in my model (the command object)

    I've tryed to do it this way:
    Code:
    	@Override
    	public ModelAndView handleRequest(HttpServletRequest request,
    			HttpServletResponse response) throws Exception {
    		FormRegistrazioneCommand command = (FormRegistrazioneCommand) getCommand(request);
    		command.setListaPaesi(countryService.getData());
    		command.setPaeseSelezionato((Country)countryService.getDefault());
    		return new ModelAndView(getFormView(),getCommandName(),command);
    	}
    but the object rendered by the view this way:

    Code:
    <form:form method="POST" name="myForm" commandName="formRegistrazioneCommand" action="riepilogo">
    	<form:input path="persona.nome"></form:input>
    	<form:input path="persona.cognome"></form:input>
      	<form:select path="paeseSelezionato.codicePaese">
                <form:option value="0" label="Select" />
                <form:options items="${listaPaesi}" itemValue="codicePaese" itemLabel="nomePaese" />
        </form:select>
    	<input type="submit"/>
    </form:form>
    where FormRegistrazioneCommand is defined as

    Code:
    public class FormRegistrazioneCommand {
    	private Persona persona;
    	private List<Country> listaPaesi;
    	private Country paeseSelezionato;
    //OMISSIS - GETTERS AND SETTERS
    }
    have no "options" inside the combo.

    I've tryed also to override formBackingObject

    Code:
    	@Override
    	protected Object formBackingObject(HttpServletRequest request)
    			throws Exception {
    		FormRegistrazioneCommand command = (FormRegistrazioneCommand) super.formBackingObject(request);
    		command.setListaPaesi(countryService.getData());
    		command.setPaeseSelezionato((Country)countryService.getDefault());
    		return command;
    	}
    with no results.

    What I can't figured out is the lifecycle of the command object behind the form. When is it created? How can I hook into the creation process and how can I load additional as I want?

    I read something about referenceData method but I don't like it so much, I prefere have only one object behind a form contains all model objects needed.

    P.S. I know SimpleFormController is deprecated but at this time I am trying to get my hands dirty with a full xml-oriented and 2.5 spring style small example before start a newer (and better) Spring 3, annotation oriented case of study.

    Thanks!

  • #2
    1. Your list of countries shouldn't be inside your form object, the form object is only there to get the filled in form
    2. Your list of countries should be a seperate list (reference data)
    3. NEVER override handleRequest if you use one of the older Controllers as that breaks the internal flow.

    1. Implement formBackingObject and only let it return the form object with the default set
    2. Implement referenceData and add a entry named listaPaesi to the map which contains your countries
    3. Don't override handleRequest

    Also the fact that you use XML doesn't mean you cannot use @Controller and the likes (why do people think that?!).

    Comment


    • #3
      1. Your list of countries shouldn't be inside your form object, the form object is only there to get the filled in form
      2. Your list of countries should be a seperate list (reference data)
      3. NEVER override handleRequest if you use one of the older Controllers as that breaks the internal flow.

      1. Implement formBackingObject and only let it return the form object with the default set
      2. Implement referenceData and add a entry named listaPaesi to the map which contains your countries
      3. Don't override handleRequest

      Also the fact that you use XML doesn't mean you cannot use @Controller and the likes (why do people think that?!).

      Comment


      • #4
        Thanks Marten,

        with your advices I fixed it up.

        I've just another question. I can't explain myself why, when I set the properties for "persona.nome" and "persona.cognome" in formBackingObject this way:

        Code:
        	@Override
        	protected Object formBackingObject(HttpServletRequest request)
        			throws Exception {
        		FormRegistrazioneCommand command = (FormRegistrazioneCommand) super.formBackingObject(request);
        		Persona p = new Persona();
        		p.setNome("Name");
        		p.setCognome("Surname");
        		command.setPersona(p);
        		return command;
        	}
        Form is filled up correctly but, when I tryed to do the same thing with a list (combobox) it seems not working at all.

        Thanks!

        P.S. I know that I can use @Controller annotation but this is only a didactic full XML based example (I am studying spring myself step by step)




        Originally posted by Marten Deinum View Post
        1. Your list of countries shouldn't be inside your form object, the form object is only there to get the filled in form
        2. Your list of countries should be a seperate list (reference data)
        3. NEVER override handleRequest if you use one of the older Controllers as that breaks the internal flow.

        1. Implement formBackingObject and only let it return the form object with the default set
        2. Implement referenceData and add a entry named listaPaesi to the map which contains your countries
        3. Don't override handleRequest

        Also the fact that you use XML doesn't mean you cannot use @Controller and the likes (why do people think that?!).

        Comment

        Working...
        X