Search code examples
springvelocityformview

Nested Velocity template with Spring formView


I have a Spring app that I'd like to add a login feature to. I'd like to put the login form in the header of the site. This means that it'll be included on several pages. When defining the controller that the form submits to, what do I specify as the formView?

Is it possible to specify the login template that's included in header (that's included in each head :-)) as the formView?

Thanks for the help. If anything is unclear than I'm happy to provide more details or show code.


Solution

  • Nevermind. I realized that it doesn't matter whether the Velocity template is included in another file. I added this to the template:

    <form method="POST">
    #springBind("credentials.*")
    

    and my controller looks like this:

    @Controller
    public class SplashController implements Serializable {
    
        protected final Log logger = LogFactory.getLog(getClass());
        private static final long serialVersionUID = 7526471155622776147L;
    
        @ModelAttribute("credentials")
        public LoginCredentials getFormBean() {
            return new LoginCredentials();
        }
    
        @RequestMapping(method = RequestMethod.GET)
        public String showForm() {
            logger.info("In showForm method of SplashController");
            return "splash";
        }
    
        @RequestMapping(method = RequestMethod.POST)
        public ModelAndView onSubmit(LoginCredentials credentials, BindingResult bindingResult) {
    
            logger.info("In onSubmit method of SplashController");
            logger.info("Username = " + credentials.getUsername());
            logger.info("Password = " + credentials.getPassword());
            ModelAndView modelAndView = new ModelAndView("home");
            return modelAndView;
    
        }
    
    }
    

    and it works.