Redirecting one to another controller using spring mvc 3

below is my controller

@RequestMapping(method = RequestMethod.GET) @ResponseBody public String ABC(Registratio registration, ModelMap modelMap, HttpServletRequest request,HttpServletResponse response){ if(somecondition=="false"){ return "notok"; // here iam returning only the string } else{ // here i want to redirect to another controller shown below } } @RequestMapping(value="/checkPage",method = RequestMethod.GET,) public String XYZ(ModelMap modelMap, HttpServletRequest request,HttpServletResponse response){ return "check"; // this will return check.jsp page } 

since Controller ABC is of type @ResponceBody, it will always be returned as a string, but I want it to be redirected to the XYZ controller otherwise and from which it will return a jsp page that I can show. I tried using return "forward: checkPage"; also with return "redirect: checkPage"; but does not work. any help.

Thanks.

+7
java spring-mvc
source share
3 answers

I think you need to delete @ResponseBody, if you want to either edit the answer yourself, or redirect to a single controller method based on some condition, try the following:

 @RequestMapping(method = RequestMethod.GET) //remove @ResponseBody public String ABC(Registratio registration, ModelMap modelMap, HttpServletRequest request,HttpServletResponse response){ if(somecondition=="false"){ // here i am returning only the string // in this case, render response yourself and just return null response.getWriter().write("notok"); return null; }else{ // redirect return "redirect:checkPage"; } } 

- edit -

if you want to access the controller via ajax, you'd better specify the data type parameter in the request to indicate that you are just waiting for a text response:

 $.get("/AAA-Web/abc",jQuery.param({}) ,function(data){ alert(data); }, "text"); 
+10
source share
 return new ModelAndView("redirect:/admin/index"); 

The code above works for me. I was redirected from the current controller to the index in the AdminController.

0
source share

redirects to the XYZ controller and from which it returns the jsp page instead of using the following i / e code

  @RequestMapping(value="/checkPage",method = RequestMethod.GET,) public String XYZ(ModelMap modelMap, HttpServletRequest request,HttpServletResponse response){ return "check"; // this will return check.jsp page } 

using

  @RequestMapping(value ="/checkPage",method = RequestMethod.GET) public ModelAndView XYZ(HttpServletRequest req) { ModelAndView m=new ModelAndView(); m.setViewName("check"); return m; } 
-one
source share

All Articles