1

The below is my controller.

@Controller
@RequestMapping("/userRegistration.htm")
@SessionAttributes("user")
public class UserController {

    private UserService userService;

    @Autowired
    public void setUserService(UserService userService) {
        this.userService = userService;
    }

    @RequestMapping(method = RequestMethod.GET)
    public String showUserForm(ModelMap model)
    {
        User user = new User();
        model.addAttribute(user);
        return "userForm";
    }

    @RequestMapping(method = RequestMethod.POST)
    public void onSubmit(@ModelAttribute("user") User user, HttpServletResponse response) {
        userService.add(user);
        //return "redirect:userSuccess.htm";
    }

}

The problem in the above code is, in the method 'onSubmit' I am not returning anything but the webpage in the browser is lost, as well as it is not redirected to any new URL, the same URL displayed in the browser.

Please let us know, what is the issue?

Petar Minchev
  • 46,889
  • 11
  • 103
  • 119
sokid
  • 813
  • 3
  • 10
  • 16
  • So the browser does a submit with the POST method, and shows the response of server for the request. You do not return any view name in your onSumbit method so nothing good happens. – Boris Treukhov Dec 09 '12 at 10:16
  • How to solve this issue ? pls help me... – sokid Dec 09 '12 at 10:18
  • You need return a new view name in the controller `POST` handler method just like you do in `GET` method handler - something like ` return "userSuccess";` assuming that you have a `userSuccess.jsp`. It is the nature of HTTP that when you do a POST request you get the result of the request i.e. request and response are connected. – Boris Treukhov Dec 09 '12 at 10:20
  • Sorry, I dont have any response jsp file, for this 'POST' I just want to do some business logic, no display change in the webpage, so how to do? pls help me – sokid Dec 09 '12 at 10:22
  • Then you should use AJAX to call onSubmit method, so the browser won't change the content of current page. – Boris Treukhov Dec 09 '12 at 10:23
  • What makes things more complicated is that you are using Spring MVC form binding - so you should serialize the form and pass the it with AJAX to MVC controller. – Boris Treukhov Dec 09 '12 at 10:31
  • Are you sure? is there any other trick to resolve this issue? The reason is I have to achieve the data binding feature using
    tag in JAVA Spring framework, but I can't return any response JSP file, because I have loaded everything already, nothing required to return from the server side (display related).
    – sokid Dec 09 '12 at 10:33
  • You can serialize the form with jQuery ajax form plugin http://stackoverflow.com/questions/1960240/jquery-ajax-submit-form – Boris Treukhov Dec 09 '12 at 10:38
  • 1
    The modern approach btw is to serialize everything to JSON http://blog.springsource.org/2010/01/25/ajax-simplifications-in-spring-3-0/ but I'm not sure how it relates to the classic Spring MVC binding which simply uses request parameters to populate fields. – Boris Treukhov Dec 09 '12 at 10:44

1 Answers1

0

there is nothing to action after busness logic. in your code. if you want redirect after business logic. use response object

response.sendRedirect("/userSuccess.htm");
namxee
  • 233
  • 1
  • 9