5

私は次のコードを書きました:

@Controller
    @RequestMapping("something")
    public class somethingController {
       @RequestMapping(value="/someUrl",method=RequestMethod.POST)
       public String myFunc(HttpServletRequest request,HttpServletResponse response,Map model){
        //do sume stuffs
         return "redirect:/anotherUrl"; //gets redirected to the url '/anotherUrl'
       }

      @RequestMapping(value="/anotherUrl",method=RequestMethod.POST)
      public String myAnotherFunc(HttpServletRequest request,HttpServletResponse response){
        //do sume stuffs
         return "someView"; 
      }
    }

リクエスト メソッドが POST である "anotherUrl" リクエスト マッピングにリダイレクトするにはどうすればよいですか?

4

1 に答える 1

12

Spring Controller メソッドは、POST リクエストと GET リクエストの両方にすることができます。

あなたのシナリオでは:

@RequestMapping(value="/anotherUrl",method=RequestMethod.POST)
  public String myAnotherFunc(HttpServletRequest request,HttpServletResponse response){
    //do sume stuffs
     return "someView"; 
  }

リダイレクトしているため、この GET が必要です。したがって、あなたの解決策は

  @RequestMapping(value="/anotherUrl", method = { RequestMethod.POST, RequestMethod.GET })
      public String myAnotherFunc(HttpServletRequest request,HttpServletResponse response){
        //do sume stuffs
         return "someView"; 
      }

注意 : ここで、メソッドが @requestParam によっていくつかのリクエスト パラメータを受け入れる場合、リダイレクト中にそれらを渡す必要があります。

このメソッドに必要なすべての属性は、リダイレクト中に送信する必要があります...

ありがとうございました。

于 2013-09-02T05:55:03.673 に答える