7

以下は私のコントローラーです

  @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
}

コントローラーABCは @ResponceBody 型であるため、常に文字列として返されますが、それ以外の場合はXYZコントローラーにリダイレクトされ、そこから表示できるjspページが返されるようにしたいと考えています。return "forward:checkPage";を使ってみました。また、 「redirect:checkPage」を返します。 しかし、うまくいきません。どんな助けでも。

ありがとう。

4

3 に答える 3

10

応答を自分でレンダリングするか、何らかの条件に基づいて 1 つのコントローラー メソッドでリダイレクトする場合は、@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";
    }
}

- 編集 -

ajax 経由でコントローラーにアクセスする場合は、要求に datatype パラメーターを含めて、単にテキスト応答を期待していることを示すことをお勧めします。

$.get("/AAA-Web/abc",jQuery.param({})
    ,function(data){ 
        alert(data); 
    }, "text"); 
于 2013-08-07T06:52:56.340 に答える
-1

XYZコントローラーに転送され、そこから次のコードi/eを使用する代わりにjspページが返されます

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

使用する

  @RequestMapping(value ="/checkPage",method = RequestMethod.GET)
public ModelAndView  XYZ(HttpServletRequest req)
{

    ModelAndView m=new ModelAndView();

    m.setViewName("check");
    return m;
}
于 2013-08-07T06:35:19.390 に答える