Spring MVC 自定义范围 bean
我想创建自己的自定义作用域 bean,它将使用 HTTP 会话(类似于 Flash 作用域)。
根据Spring手册我需要实现org.springframework.beans.factory.config.Scope接口
public class CustomScope implements Scope {
@Override
public Object get(String arg0, ObjectFactory<?> arg1) {
// TODO Auto-generated method stub
return null;
}
@Override
public String getConversationId() {
// TODO Auto-generated method stub
return null;
}
@Override
public void registerDestructionCallback(String arg0, Runnable arg1) {
// TODO Auto-generated method stub
}
@Override
public Object remove(String arg0) {
// TODO Auto-generated method stub
return null;
}
@Override
public Object resolveContextualObject(String arg0) {
// TODO Auto-generated method stub
return null;
}
}
我的问题是如何获取这个bean内部的HTTP会话?我知道如果我在 ServletContext 范围内创建 bean,我将实现 ServletContextAware 接口。
请帮忙:)
I would like to create my own custom scope bean which will use HTTP session (kind of Flash scope).
According to Spring Manual I need to implement org.springframework.beans.factory.config.Scope interface
public class CustomScope implements Scope {
@Override
public Object get(String arg0, ObjectFactory<?> arg1) {
// TODO Auto-generated method stub
return null;
}
@Override
public String getConversationId() {
// TODO Auto-generated method stub
return null;
}
@Override
public void registerDestructionCallback(String arg0, Runnable arg1) {
// TODO Auto-generated method stub
}
@Override
public Object remove(String arg0) {
// TODO Auto-generated method stub
return null;
}
@Override
public Object resolveContextualObject(String arg0) {
// TODO Auto-generated method stub
return null;
}
}
My question is how can I obtain HTTP session inside of this bean? I understand that if I was creating bean in ServletContext scope I would implement ServletContextAware interface.
Please help :)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我希望它对将来的人有用,所以我想分享它。
我对此进行了一些研究,不幸的是,我发现 Spring MVC 无法获取 HTTP Session。
我的目的是使用 PRG 模式为 Spring MVC 控制器实现 Flash Scope。
在 Spring 论坛中进行了更多研究,我找到了使用 HandlerInterceptor 来完成此操作的方法。
现在 MultiScopeModelMap.java
用法:
在 JSP 中,用法非常简单:
另外,您需要将 FlashScopeInterceptor 类配置为拦截器。
I hope it will be useful for someone in the future, so I would like to share it.
I made some reserch on it and have found that unfortunately, it is impossible to get HTTP Session for Spring MVC.
My purpose was Flash Scope implementation for my Spring MVC Controller using PRG pattern.
Making more research in Spring Forum I've found the way to do it using HandlerInterceptor.
Now MultiScopeModelMap.java
Usage:
In JSP the usage is very simple:
In addition you need to configure FlashScopeInterceptor class as interceptor.
我建议查看 org.springframework.web.context.request.SessionScope。这个范围一定以某种方式解决了同样的问题。
看起来他们使用:
RequestContextHolder.currentRequestAttributes().getSessionId()
I recommend to have a look at the source code of org.springframework.web.context.request.SessionScope. This scope must have solved the same problem some how.
It looks like that they use:
RequestContextHolder.currentRequestAttributes().getSessionId()
您可以使用 Spring MVC 中范围类方法中的下一个代码(在 3.2 中工作)来访问会话属性:
RequestAttributes 实现 (ServletRequestAttributes) 在内部将调用当前会话对象上的 set/getAttribute() 方法。
You can get access to session attributes using the next code within your scope class methods in Spring MVC (works in 3.2):
RequestAttributes implementation (ServletRequestAttributes) internally will call set/getAttribute() methods on current session object.