Spring重定向:前缀问题
我有一个使用 Spring 3 的应用程序。我有一个视图解析器,它基于字符串构建我的视图。所以在我的控制器中我有这样的方法。
@RequestMapping(...)
public String method(){
//Some proccessing
return "tiles:tileName"
}
我需要返回一个RedirectView来解决由于浏览器中更新页面导致的重复提交,所以我想到了使用Spring的redirect:前缀。问题是,只有当我使用 URL 更改前缀(而不是解析器可以理解的名称)时,它才会重定向。我想做这样的事情:
@RequestMapping(...)
public String method(){
//Some proccessing
return "redirect:tiles:tileName"
}
有没有办法将 RedirectView 与我从每个控制器方法获得的字符串(可解析的视图名称)一起使用?
谢谢
I have an application which uses Spring 3. I have a view resolver which builds my views based on a String. So in my controllers I have methods like this one.
@RequestMapping(...)
public String method(){
//Some proccessing
return "tiles:tileName"
}
I need to return a RedirectView to solve the duplicate submission due to updating the page in the browser, so I have thought to use Spring redirect: prefix. The problem is that it only redirects when I user a URL alter the prefix (not with a name a resolver can understand). I wanted to do something like this:
@RequestMapping(...)
public String method(){
//Some proccessing
return "redirect:tiles:tileName"
}
Is there any way to use RedirectView with the String (the resolvable view name) I get from the every controller method?
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
以“redirect:”为前缀的调用是一个 url,以标准浏览器 302 重定向发送。您无法重定向到视图,因为视图不是 URL。相反,您需要一个新的servlet映射到“成功”视图,然后重定向到该视图,而
当您只需要显示“谢谢”页面时,这种情况可以正常工作,这不需要处理阶段的任何特定数据。但是,如果您的成功页面需要显示处理过程中的一些信息,有两种方法可以实现。
1) 将 url 中的信息作为 get post 传递(“redirect:success.htm?message=hi”)。这是非常容易被破解的,因此非常不推荐。
2)更好的方法是在http会话中存储信息,使用@SessionAttributes和@ModelAttribute
the call prefixed by redirect: is a url, which is sent in a standard browser 302 redirect. you can't redirect to a view, because a view isn't a url. instead you'll need a new servelet mapping to a 'success' view and then redirect to that instead
this case works fine when you just need to show a 'thank you' page, which requires no specific data from the processing stage. however, if your success page needs to show some information from the processing, there are 2 ways to do it.
1) pass the information in the url as a get post ("redirect:success.htm?message=hi"). this is incredibly hackable, and thus highly unrecommended.
2) the better way is to store information in the http session, using
@SessionAttributes
and@ModelAttribute