Announcement Announcement Module
Collapse
No announcement yet.
form processing Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • form processing

    Hello everyone,

    I am pretty new to spring framework and i am not sure of doing things the best way.

    I have to process a login form, so i created a Controller:
    Code:
    @Controller
    public class LoginController {
    
    	 protected final Log logger = LogFactory.getLog(getClass());
    	 
    	 @RequestMapping(value = "/index.htm", method = RequestMethod.POST)
    	 public ModelAndView login(@RequestParam("login") String login, @RequestParam("password") String password) {			 
    		 User user = new User();
    		 user.setLogin(login);
    		 user.setPassword(password);
    		 
    		 ModelAndView modelAndView = new ModelAndView("index");
    		 modelAndView.addObject("user", user);
    	 
    		 return modelAndView;
    	 }
    
    }
    I want to follow the annotations way, and now my question. Is this the best way to process a form in my controller?

    Thanks everyone for the help

  • #2
    Hello everyone,

    I have been refactoring my code in order to achive the best way to handle forms.

    Code:
    @Controller
    public class LoginController {
    
    	 protected final Log logger = LogFactory.getLog(getClass());	
    	 
    	 @InitBinder("user")
    	 void initBinder(WebDataBinder binder) {
    	     binder.setValidator(new LoginValidator());
    	 }
    	 
    	 @RequestMapping(value = "/index.htm", method = RequestMethod.POST)
    	 public ModelAndView login(@ModelAttribute("user") User user, BindingResult result) {		 
    		 ModelAndView modelAndView = new ModelAndView("index");
    		 modelAndView.addObject("user", user);
    		 
    		 if (result.hasErrors()) {
    		    //	return to the form
    	            
    		 }
    	 
    		 return modelAndView;
    	 }
    
    }
    Could this be the right way to process my form?

    Thanks again

    Comment


    • #3
      You need to have a method to set up your form first. For details, please read the document and sample sin 2.5 and 3.0.

      HTH

      Comment


      • #4
        Thanks vw729 for your response.

        This is what i have come up with:
        Code:
        @Controller
        @RequestMapping("/index.htm")
        @SessionAttributes("user")
        public class LoginController {
        
        	 protected final Log logger = LogFactory.getLog(getClass());
        	 
        	 @InitBinder
        	 public void setAllowedFields(WebDataBinder binder) {
        		 binder.setDisallowedFields(new String[] {"id"});
        	 }
        	 
        	 @RequestMapping(method = RequestMethod.POST)
        	 public String setupForm(@RequestParam("login") String login, @RequestParam("password") String password, Model model) {	
        		 User user = new User();
        		 user.setLogin(login);
        		 user.setPassword(password);		 
        		 model.addAttribute("user", user);		 
        		 return "petForm";
        	 }
        	    
        	 @RequestMapping(method = RequestMethod.POST)
        	 public String processSubmit(@ModelAttribute("user") User user, BindingResult result, SessionStatus status) {
        		 new LoginValidator().validate(user, result);
        		 if (result.hasErrors()) {
        			 return "petForm";
        		 } else {
        			 status.setComplete();
        			 return "/index.htm";
        		 }
        	 }
        
        }
        I guess now i am much nearer about the best way to process my login form right?

        Thanks again

        Comment


        • #5
          Ok i am not sure about this, i have two more questions...

          1 - wouldnt be better to have my processSubmit method return a ModelAndView?
          Code:
          @RequestMapping(method = RequestMethod.POST)
          	 public ModelAndView processSubmit(@ModelAttribute("user") User user, BindingResult result) {		 
          		 ModelAndView modelAndView = new ModelAndView();
          		 
          		 modelAndView.addObject("user", user);
          		 
          		 if (result.hasErrors()) {
          			 modelAndView.setViewName("login");
          		 } else {
          			 modelAndView.setViewName("index");
          		 }
          	 
          		 return modelAndView;
          	 }
          instead a String with the redirection?
          Code:
          @RequestMapping(method = RequestMethod.POST)
          	 public String processSubmit(@ModelAttribute("user") User user, BindingResult result, SessionStatus status) {
          		 new LoginValidator().validate(user, result);
          		 if (result.hasErrors()) {
          			 return "login";
          		 } else {
          			 status.setComplete();
          			 return "index";
          		 }
          	 }
          2 - I have doing some tests with my controller and if i delete every method but processSubmit, its still working ok
          Code:
          @Controller
          @RequestMapping("/index.htm")
          @SessionAttributes("user")
          public class LoginController {
          
          	 protected final Log logger = LogFactory.getLog(getClass());
          	    
          	 @RequestMapping(method = RequestMethod.POST)
          	 public String processSubmit(@ModelAttribute("user") User user, BindingResult result, SessionStatus status) {
          		 new LoginValidator().validate(user, result);
          		 if (result.hasErrors()) {
          			 return "login";
          		 } else {
          			 status.setComplete();
          			 return "index";
          		 }
          	 }
          
          }
          Please, can someone clarify this? i am really stuck.

          Thanks a lot in advance

          Comment


          • #6
            Just a step back, but have you looked at spring security?

            If you are just not just using this a learning example there are easier ways to provide app security.

            Comment


            • #7
              Hello travisjwarren and thanks,

              This is just a learning example, trying to learn how to use spring.

              Can someone clarify my doubts, please?

              Thanks everyone

              Comment

              Working...
              X