[英]Spring MVC Controller redirect using URL parameters instead of in response
[英]using spring mvc redirect a url pattern to a specific controller
我想重定向這樣的請求
localhost:8080 / firstSpringProject / {uniqueusername}
到名為'profile'的特定控制器:
@RequestMapping(value="/profile")
public String profiles(Model model){
based on the uniqueusername i would like to render a profile page
return "profile";
}
我正在使用spring mvc; 我怎么能解決這種情況還有其他辦法嗎?
Spring文檔在重定向視圖中說:
請注意,擴展重定向URL時,當前請求中的URI模板變量會自動變為可用,並且不需要通過Model或RedirectAttributes顯式添加。 例如:
@RequestMapping(value = "/files/{path}", method = RequestMethod.POST)
public String upload(...) {
// ...
return "redirect:files/{path}";
}
請記住, 由於緩存重定向視圖 ,低於3.1.4的版本會受到內存泄漏的影響。
如果您使用的是Spring 3.1.3或更低版本,那么您正在使用它
return "redirect : profile?username="+username;
你有時會看到OutOfMemoryError 。
我想你可以在這里使用spring path變量。 您必須創建一個控制器方法,該方法將根據您的URL要求使用用戶名,並將使用username參數重定向到profile方法。
@RequestMapping(value="/{username}")
public String getUserName(Model model,@PathVariable("username") String username){
//process username here and then redirect to ur profile method
return "redirect : profile?username="+username;
}
@RequestMapping(value="/profile")
public String profiles(Model model,String username){
//have a username and render a profile page
return "profile";
}
謝謝
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.