2013-08-30 16 views
5
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」請求映射。如何在Spring MVC中重定向到POST

回答

9

在春天控制器的方法既可以是這意味着它可以是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接受一些請求參數,然後重新導向必須通過他們 只需通過這種方法所需的所有屬性必須在重定向時發送...

謝謝

+0

bindingResults正在以這種方式丟失先生。任何解決方案。? – masT

+0

沒有綁定結果會丟失。這裏我們只是告訴handle方法接受GET和POST方法。 –

+0

從技術的角度來看,這將起作用,如果您正在編寫一個RESTful應用程序,這將違反GET的「無副作用」期望 - 所以任何追求此解決方案的人都應該意識到這一慣例的突破。我建議要麼找到另一種方式,要麼記錄它真的很好(非常少) – romeara