Results 1 to 3 of 3

Thread: Hashmap of form command objects

  1. #1
    Join Date
    May 2009
    Posts
    13

    Default Hashmap of form command objects

    I have a class to store several command objects:

    Code:
    public class FormCommandMap
    {
    	private HashMap<String, Object> commandMap;
    		
    	public FormCommandMap()
    	{
    		commandMap = new HashMap<String, Object>();
    	}
    
    	public void addCommand(String key, Object command)
    	{
    		commandMap.put(key, command);
    	}
    	
    	public void removeCommand(String key)
    	{
    		commandMap.remove(key);
    	}
    	
    	public Object getCommand(String key)
    	{
    		return commandMap.get(key);
    	}
    	
    	public void setCommandMap(HashMap<String, Object> hashMap) 
    	{
    		commandMap = hashMap;
    	}
    
    	public HashMap<String, Object> getCommandMap()
    	{
    		return commandMap;
    	}
    	
    } // End CLass: FormCommandMap
    I have an annotated form controller:

    Code:
    @RequestMapping(value = "/cms/add/page", method = RequestMethod.GET)
    public ModelAndView get()
    {
        	FormCommandMap fcm = new FormCommandMap();   	
        	Page targetPage = new Page();
        	PageController targetController = new PageController();
        	PageProperties targetProperties = new PageProperties();
     
           	fcm.addCommand("target_page", targetPage);
           	fcm.addCommand("target_controller", targetController);
           	fcm.addCommand("target_properties", targetProperties);
        	
    	ModelMap mmap = new ModelMap();
    	mmap.addAttribute("form_command_map", fcm);
    		
    	return new ModelAndView("cms_add_page", mmap);
    }
    
    @RequestMapping(value = "/cms/add/page", method = RequestMethod.POST)
    public ModelAndView post(@ModelAttribute("form_command_map") FormCommandMap form_command_map, BindingResult result, SessionStatus status) 
    {	
        	Page targetPage = (Page) form_command_map.getCommand("target_page");
        	
        	log.info(targetPage.getIdentifier());
        	log.info(targetPage.getClickable());
        	log.info(targetPage.getParent());
        	
    	return new ModelAndView(new RedirectView("/cms/add/page/success", true));
    }
    And I have my view (cms_add_page.ftl):

    Code:
    <form method="post" enctype="multipart/form-data">
      <table>
        <tr>
         <th>
            Identifier:
          </th>
          <td>
          	[@spring.bind "form_command_map.commandMap['target_page'].identifier"/]
          	<input type="text" name="${spring.status.expression}" value="${spring.status.value}"/> ${spring.status}[@spring.showErrors ", " "fieldError" /]
          </td>
        </tr>    
        <tr>
          <td>
         	<p class="submit"><input type="submit" value="Add Page"/></p>
          </td>
        </tr>
      </table>
      
    </form>

    The bind status ${spring.status} is:
    Code:
    BindStatus: expression=[commandMap['target_page'].identifier]; value=[];
    The log output is:

    Code:
    2010-04-19 10:05:31,706 INFO [com.site.web.cms.form.controller.AddPageForm] - <>
    2010-04-19 10:05:31,706 INFO [com.site.web.cms.form.controller.AddPageForm] - <true>
    2010-04-19 10:05:31,706 INFO [com.site.web.cms.form.controller.AddPageForm] - <>
    The problem is, when i submit the form, the bind 'value' is never bound to the string property 'identity' of the target_page command object (which is stored in the hashmap). How can i make the form field 'commandMap['target_page'].identifier' set the identity property of the Page instance target_page.

    Any help is greatly appreciated.

    Note: I'm using freemarker as my template language.

  2. #2
    Join Date
    May 2009
    Posts
    13

    Default

    A very similar issue is described here:

    [URL="http://forum.springsource.org/showthread.php?t=18687"]

  3. #3
    Join Date
    May 2009
    Posts
    13

    Default

    Ok solution was to remove: enctype="multipart/form-data"
    from the form element. All works now. Hope this helps someone.

Posting Permissions

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