Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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 the Controller ABC is @ResponceBody type it will always return as string, but i want that in else contion it should be redirected to the XYZ controller and from which it return a jsp page which i can show. i tried using return "forward:checkPage"; also with return "redirect:checkPage"; but doesn't work. any help.

Thanks.

like image 529
ASUR Avatar asked Aug 07 '13 06:08

ASUR


1 Answers

I think you have to remove @ResponseBody if you want to either render response yourself or redirect in one controller method based on some condition, try this:

@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 controller via ajax, you'd better include the datatype parameter on you request to indicate that you are simply expecting a text response:

$.get("/AAA-Web/abc",jQuery.param({})
    ,function(data){ 
        alert(data); 
    }, "text"); 
like image 80
Septem Avatar answered Sep 23 '22 00:09

Septem