使用spring mvc 3将一个控制器重定向到另一个控制器

ASU*_*SUR 7 java spring-mvc

下面是我的控制器

  @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
}
Run Code Online (Sandbox Code Playgroud)

由于控制器ABC是@ResponceBody类型,它将始终作为字符串返回,但我希望在其他情况下它应该被重定向到XYZ控制器,并从中返回一个我可以显示的jsp页面.我尝试使用return"forward:checkPage"; 还有 返回"redirect:checkPage"; 但不起作用.任何帮助.

谢谢.

Sep*_*tem 10

我想如果你想自己渲染响应或者根据某些条件在一个控制器方法中重定向,你必须删除@ResponseBody,试试这个:

@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";
    }
}
Run Code Online (Sandbox Code Playgroud)

- 编辑 -

如果你想通过ajax访问控制器,你最好在你的请求中包含datatype参数,以表明你只是期望一个文本响应:

$.get("/AAA-Web/abc",jQuery.param({})
    ,function(data){ 
        alert(data); 
    }, "text"); 
Run Code Online (Sandbox Code Playgroud)