Spring 3.0 Form Controller

To create a form controller in Spring 2.0 have a look at this article Simple Form Controller

In Spring 3.0, its much simpler to create form controllers. There is no need to extend any class (like SimpleFormController) to create a controller in Spring.

Have a look at the following Spring controller.

package in.techdive.web;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

@Controller
@RequestMapping("usermanagement/newUser")
public class UserFController
{
        @RequestMapping(method = RequestMethod.POST)
        public String onSubmit(User user, BindingResult result, HttpServletRequest request, HttpServletResponse response)
                        throws Exception
        {
                // add code to insert the user record in to DB
                return "redirect:/usermgmt/viewUser.htm";
        }
       
        @RequestMapping(method = RequestMethod.GET)
        protected User showForm(HttpServletRequest request, HttpServletResponse response) throws Exception
        {
                return new User();
        }
}

Since the controller is annotation based, add the following beans in Spring configuration file.

  <context:component-scan base-package="in.techdive.web"/>
Technology: 

Search