如何在Spring MVC中重定向到POST

时间:2013-08-30 06:33:56

标签: spring-mvc

I have written code as given below-


@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”请求映射。

1 个答案:

答案 0 :(得分:12)

在Spring中,Controller方法可以是Both这意味着它可以是GET以及POST ... 在您的场景中,

@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的某些请求参数,那么在重定向时您必须通过它们 简单地说,此方法所需的所有属性必须在重定向时发送...

谢谢