- Java 双重比较
- java - 比较器与 Apache BeanComparator
- Objective-C 完成 block 导致额外的方法调用?
- database - RESTful URI 是否应该公开数据库主键?
为简单起见,这些代码片段将被缩短。这样做的目的是获取一个 GET 参数,在 session 中设置它,然后重定向回 GET 并删除 url 参数。基本上,URI 清理。如果有更好/更简单的方法来做到这一点,我会很高兴听到。
我有一个这样定义的 Controller :
@Controller
@RequestMapping("/path/page.xhtml")
@Scope(BeanDefinition.SCOPE_PROTOTYPE)
@SessionAttributes({ "myParam1", "myParam2" })
public class MyController {
@RequestMapping(method = RequestMethod.GET, params = { "urlParam2" })
public String handleUriParam(@RequestParam(value = "urlParam2", required = false)
final Long urlParam2,
final RedirectAttributes redirs) {
// at this point, myParam1 is set on the session.
// now set the param as a flash attrib with the name of the session variable
redirs.addFlashAttribute("myParam2", urlParam2);
return "redirect:/path/page.xhtml";
}
@RequestMapping(method = RequestMethod.GET, params = {})
public String doGetStuff(ModelMap model) {
// do stuff using myParam1 and myParam2.
// problem is, myParam2 is on the session, but myParam1 is not!
}
}
就像代码所说的那样,当重定向发生时,不知何故 myParam1
被取消设置。我可以通过将 ModelMap
传递给 handleUrlParam
方法并手动将 myParam1
添加到 flash 属性来解决这个问题,但这似乎违背了目的我的想法。
为什么 SessionAttribute
myParam1
在重定向后被删除?
有没有更好的方法从 URI 中提取参数并将它们放在 session 中?
更新
因此,无论何时使用 RedirectAttributes
,您都必须确保将要携带的任何 SessionAttribute
放入 FlashAttributes 的重定向中,否则它们会迷路。我想这是因为 SessionAttribute
被拉离了 ModelMap
(使用时被 FlashAttributes 取代)。这是 Spring 中的错误还是故意行为?如果是故意的,有人可以解释为什么吗?我认为 SessionAttribute
应该一直存在,直到 session 结束后被移除。
Similar StackOverflow post here .
附录
鉴于所提供的已接受答案,我仍然对如何在将 URI 参数置于用户 session 中时清除它们感到困惑。我考虑过的一个选择是为我试图存储的半原始对象(java.lang.Integer、java.lang.String)创建一个包装器,因为它们不会被放置在 URI 字符串上,但这似乎很老套我。如果有人有更好的方法来接受 GET 参数,将它们存储在用户的 session 中,并从用户的地址栏中清除这些参数(这将需要重定向),我将很乐意使用它。
最佳答案
所以我四处查看代码和 Internet 以找出它不起作用的原因。
Spring 有两个完全独立的模型映射 - 一个用于标准 View 渲染,另一个在发出重定向时使用。这可以在 ModelAndViewContainer
中观察到.
现在 session 属性持久化完成了based on the result from mavContainer#getModel()
.对于重定向场景,这将返回重定向模型。因此,您在标准 Model
/ModelMap
上设置的任何内容都会丢失。
在谈论标准模型属性时,这是有道理的。模型主要用于将对象传递给 View 。使用重定向时,您正在处理完全不同的情况。您希望通过 HTTP 重定向传输对象 - 因此采用了分离的基于字符串和 flash 的模型。
但是我的感觉是他们在 designing this feature 时忘记了 session 属性.有一些不错discussion in Spring's Jira ,但是它们都没有解决这个具体问题。
所以是的...这可能是 Spring 的 Jira 的主题。它可能被归类为错误,因为这会阻止任何人在使用时设置 session 模型属性重定向。通过 RedirectAttributes#addFlashAttribute
强制 Spring 存储您的 session 属性在我看来是一种 hack,并且本身就是一种错误。
关于java - 为什么 SessionAttributes 在 GET 重定向时被清除?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17086216/
我知道使用 GET 和 SET 函数的公共(public)变量的缺点/私有(private)变量的优点,但目前我正在使用 Ogre3D 开发自己的第一个“真实”游戏(C++)..同时,我有时需要 6-
我正在开发一个 GSM/GPRS 应用程序,它将每 10 秒报告一些值。我必须使用的 SIM 卡每月只有 15MB 可用数据。我使用的是 SIM900 GSM 芯片供您引用。 我到达服务器的方式是通过
这三者有什么区别:gets - 它获取带有 '\n' 的行gets.chomp - 它得到一行,但删除 '\n' 这样对吗? gets.chomp! 怎么样? 最佳答案 gets - 它得到一个末尾带
问题和我现在遇到的问题 脚本 顺便说一句,评论是挪威语的,如果它们看起来很奇怪哈哈 Connect-AzureAD #variabel $Users = Get-AzureADUser -All:$t
我现在面临的问题是获取一个 URL,如下所示: www.example.com/example.php?url=www.google.com 现在的问题是,如果我的网址中有一个 get,如下所示: w
我有一个 queryString 传递给 servlet 的 doGet() 方法,如下所示: count=9&preId0=-99&objId0=-99&preId1=-99&objId1=-99&
这是我在 Django 模板中的代码: {% for tag in tags %} {{ tag }} {% endfor %} 在view.py中: def tag_find(
我正在尝试在express.js中为我的网络应用程序创建一个路由系统,我需要知道是否需要使用app.get/post/put/delete.apply以编程方式设置多个功能对于一条路线。 也是如此 a
我正在通过示例查看 A.Mele Django,第 1 章 def post_list(request, category=None): object_list = Post.publishe
如果我想找到与IIS站点或应用程序关联的目录,我该怎么做? 我似乎无法从Get-Website和Get-WebApplication的对象的任何属性中找到任何允许我这样做的东西。 最佳答案 只需查看一
不知道发生了什么。当我执行以下代码时......它运行良好......但它产生了错误。如果我将以下内容粘贴到我的浏览器地址栏中并点击它,我会得到一个 URL。如果我通过 KRL http:get 输入
Curl 提供了一系列不同的带有 X 前缀的 http 方法调用,但也提供了不带 X 的相同方法。我两种都试过了,但我似乎无法弄清楚其中的区别。有人可以快速向我解释这两种操作有何不同吗? 最佳答案 默
request.GET.get 是什么意思?我在 Django 中看到类似的东西 page = request.GET.get('page', 1) 我认为它与类似的东西有关 « 它们是如
我正在从我的 Angular2 站点查询一些 Elasticsearch 服务器。为了帮助提高安全性,我们希望锁定对 GET 请求的访问权限。 Elasticsearch 支持带主体的 GET,但我在
关闭。这个问题是opinion-based .它目前不接受答案。 想改善这个问题吗?更新问题,以便可以通过 editing this post 用事实和引文回答问题. 4年前关闭。 Improve t
调用 HTable.get(List) 返回的 Result 数组的顺序是什么? ? 我的意思是,假设与输入列表的顺序相同是否正确? 最佳答案 结果数组中的顺序将与输入列表的顺序相同。与批处理方法一样
所以我有一个看起来像这样的 JSON 数组: var myData = { foo : { biz : 'baz', fig : 'tree' } }
我正在学习 Ajax、javascript 和 html,并且有一个应用程序可以触发“get”请求,然后再触发另一个“get”请求。这些请求是用户按下按钮的结果。在我的 servlet 中,我使用 T
关闭。这个问题是opinion-based 。目前不接受答案。 想要改进这个问题吗?更新问题,以便 editing this post 可以用事实和引文来回答它。 . 已关闭 6 年前。 Improv
运行以下 cmdlet 适用于组成员(Amer 域中的组)中的所有用户,无论列出的用户位于哪个域: Get-ADGroupMember -Server amer 但是,当尝试通过管道传输到 Get-
我是一名优秀的程序员,十分优秀!