[英]Spring 3.2 mvc, how to rewrite url within controller as part of redirectview with permanent status code sent
@RequestMapping(value = "/Foo/{id}/{friendlyUrl:.*}", method = RequestMethod.GET)
public ModelAndView getFoo(@PathVariable final Long id, @PathVariable final String friendlyUrl) {
因此它匹配ID和任何字符串。 但是我希望用戶看到我指定的字符串。
foo = fooService.get(id); //use id from pathvariable
redirectView = new RedirectView(foo.getCorrectUrl()); //set url to correct url, not that in path
redirectView.setStatusCode(HttpStatus.MOVED_PERMANENTLY); //moved permanently
modelAndView = new ModelAndView(redirectView);
modelAndView.setViewName("myFoo.jsp");
return modelAndView;
一切正常,除了用戶看到的網址不正確。
(假定)與在stackoverflow網站上的現有問題上更改問題標題時具有相同的功能。
編輯,現在做下面的工作差不多了
return new ModelAndView("redirect:/Foo/"+id+"/"+foo.getUrl());
但這返回一個臨時移動的狀態代碼,我希望永久為301。
使用spring-mvc控制器獲取重寫的URL和永久移動的狀態代碼的方法是什么?
在您的代碼中
foo = fooService.get(id); //use id from pathvariable
redirectView = new RedirectView(foo.getCorrectUrl()); //set url to correct url, not that in path
redirectView.setStatusCode(HttpStatus.MOVED_PERMANENTLY); //moved permanently
modelAndView = new ModelAndView(redirectView);
modelAndView.setViewName("myFoo.jsp");
return modelAndView;
調用modelAndView.setViewName("myFoo.jsp");
有效替換傳遞給ModelAndView構造函數的View的值(redirectView引用)。 因此,在這種情況下,您不應調用setViewName。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.