- 浏览: 782909 次
- 性别:
- 来自: 上海
文章分类
- 全部博客 (573)
- Java基础 (76)
- C++基础 (5)
- hibernate (5)
- struts (4)
- spring (1)
- webservice (7)
- AjaX基础 (0)
- JS脚本 (53)
- 正则表达式 (5)
- html脚本 (30)
- 数据库基础 (54)
- 工作相关 (49)
- 其他 (30)
- Linux (9)
- web服务器 (17)
- JSP (13)
- eclipse (6)
- 面试题相关 (20)
- XML (3)
- Apache common (2)
- 生活 (35)
- VMware (1)
- log4j (9)
- BeanUtils (2)
- 设计模式 (3)
- UML (1)
- UNIX (1)
- ibats (5)
- GT-Grid (17)
- ABAP学习 (17)
- ABAP (35)
- ABAP--ALV (11)
- ABAP--WEBDIMPRO (0)
- abap-sample (1)
- BEMS (2)
- flex (33)
- GIS技术 (3)
最新评论
采用filter 监控 MessageBrokerServlet 把context 放入 ThreadLocal 中
然后你就可以从 java 程序的任何 方法中 利用 ThreadLocal 得到 当前的session
这个时候结合aop 我采用spring 的aop 在需要 session 验证的方法前 调用一个 Interceptor 来验证 session 过期或者其他权限等
具体代码:
web.xml 中
[复制到剪贴板]
CODE:
<filter>
<filter-name>AMFContextFilter</filter-name>
<filter-class>flex.context.AMFContextFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>AMFContextFilter</filter-name>
<servlet-name>MessageBrokerServlet</servlet-name>
</filter-mapping>
AMFContextFilter文件
[复制到剪贴板]
CODE:
package flex.context;
import java.io.IOException;
import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class AMFContextFilter implements Filter {
public void doFilter(ServletRequest request, ServletResponse response,
FilterChain chain) throws ServletException, IOException {
AMFContext.setCurrentContext((HttpServletRequest) request,
(HttpServletResponse) response);
chain.doFilter(request, response);
}
public void init(FilterConfig arg0) throws ServletException {
// TODO Auto-generated method stub
}
public void destroy() {
// TODO Auto-generated method stub
}
}
AMFContext文件
[复制到剪贴板]
CODE:
package flex.context;
import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
public class AMFContext {
/**
* ThreadLocal object for storing object in current thread.
*/
private static ThreadLocal tl = new ThreadLocal();
/**
* Set current context
*
* @param request
* The HttpRequest object
* @param response
* The HttpResponses object
*/
@SuppressWarnings(&quot;unchecked&quot;)
static public void setCurrentContext(HttpServletRequest request,
HttpServletResponse response) {
AMFContext c = getCurrentContext();
if (c == null) {
c = new AMFContext(request, response);
tl.set(c);
} else {
c.setRequest(request);
c.setResponse(response);
}
}
/**
* Get current context value
*
* @return The current context
*/
static public AMFContext getCurrentContext() {
return (AMFContext) tl.get();
}
// ----------------------------------------------------------
//
// Class members
//
// ----------------------------------------------------------
/**
* The http request object. The lifecycle of the request object is defined
* as the request scope. It may be reused in another incoming connection, so
* dont use it in another thread.
*/
private HttpServletRequest request;
/**
* The http response object. The lifecycle of the response object is defined
* as the request scope. Dont use it in another thread. Also dont write
* output to the response when it is used in the context, but you may get or
* set some response header when it is safe.
*/
private HttpServletResponse response;
/**
* The constructor is private, to get an instance of the AMFContext, please
* use getCurrentContext() method.
*
* @param request
* @param response
*/
private AMFContext(HttpServletRequest request, HttpServletResponse response) {
this.request = request;
this.response = response;
}
/**
* Get request object
*
* @return Http request object
*/
public HttpServletRequest getRequest() {
return request;
}
/**
* Set request object
*
* @param Http
* request object
*/
public void setRequest(HttpServletRequest request) {
this.request = request;
}
/**
* Get response object
*
* @return Http response object
*/
public HttpServletResponse getResponse() {
return response;
}
/**
* Set response object
*
* @param response
* Http response object
*/
public void setResponse(HttpServletResponse response) {
this.response = response;
}
/**
* Get the servlet context
*
* @return
*/
public ServletContext getServletContext() {
HttpSession session = this.getSession();
return session.getServletContext();
}
/**
* Get the current running session
*
* @return
*/
public HttpSession getSession() {
return request.getSession();
}
/**
* Get an object stored in the session.
*
* @param attr
* Attribute Name
* @return The value stored under the attribute name.
*/
public Object getSessionAttribute(String attr) {
HttpSession session = this.getSession();
return session.getAttribute(attr);
}
/**
* Store an object in the session.
*
* @param attr
* Attribute Name
* @param value
* The value.
*/
public void setSessionAttribute(String attr, Object value) {
HttpSession session = this.getSession();
session.setAttribute(attr, value);
}
/**
* Get an object stored in the servlet context.
*
* @param attr
* Attribute Name
* @return The value stored under the attribute name.
*/
public Object getContextAttribute(String attr) {
ServletContext sc = this.getServletContext();
return sc.getAttribute(attr);
}
/**
* Store an object in the servlet context.
*
* @param attr
* Attribute Name
* @param value
* The value.
*/
public void setContextAttribute(String attr, Object value) {
ServletContext sc = this.getServletContext();
sc.setAttribute(attr, value);
}
/**
* Get an object stored in the current request.
*
* @param attr
* Attribute Name
* @return The value stored under the attribute name.
*/
public Object getRequestAttribute(String attr) {
return request.getAttribute(attr);
}
/**
* Store an object in the current request.
*
* @param attr
* Attribute Name
* @param value
* The value.
*/
public void setRequestAttribute(String attr, Object value) {
request.setAttribute(attr, value);
}
}
MethodInterceptor 文件
[复制到剪贴板]
CODE:
package com.sunwayworld.flex;
import javax.servlet.http.HttpServletRequest;
import org.aopalliance.intercept.MethodInterceptor;
import org.aopalliance.intercept.MethodInvocation;
import com.sunwayworld.common.utils.SessionUtils;
import flex.context.AMFContext;
public class FlexSessionInterceptor implements MethodInterceptor {
public Object invoke(MethodInvocation invocation) throws Throwable {
AMFContext context = AMFContext.getCurrentContext();
HttpServletRequest request = context.getRequest();
if (!SessionUtils.isLogin(request)) {
throw new RuntimeException(&quot;请您重新登陆!&quot;);
}
Object obj = invocation.proceed();
return obj;
}
}
然后 在spring 配置 成调用所有的java 方法前都执行这个 MethodInterceptor 即可
然后你就可以从 java 程序的任何 方法中 利用 ThreadLocal 得到 当前的session
这个时候结合aop 我采用spring 的aop 在需要 session 验证的方法前 调用一个 Interceptor 来验证 session 过期或者其他权限等
具体代码:
web.xml 中
[复制到剪贴板]
CODE:
<filter>
<filter-name>AMFContextFilter</filter-name>
<filter-class>flex.context.AMFContextFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>AMFContextFilter</filter-name>
<servlet-name>MessageBrokerServlet</servlet-name>
</filter-mapping>
AMFContextFilter文件
[复制到剪贴板]
CODE:
package flex.context;
import java.io.IOException;
import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class AMFContextFilter implements Filter {
public void doFilter(ServletRequest request, ServletResponse response,
FilterChain chain) throws ServletException, IOException {
AMFContext.setCurrentContext((HttpServletRequest) request,
(HttpServletResponse) response);
chain.doFilter(request, response);
}
public void init(FilterConfig arg0) throws ServletException {
// TODO Auto-generated method stub
}
public void destroy() {
// TODO Auto-generated method stub
}
}
AMFContext文件
[复制到剪贴板]
CODE:
package flex.context;
import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
public class AMFContext {
/**
* ThreadLocal object for storing object in current thread.
*/
private static ThreadLocal tl = new ThreadLocal();
/**
* Set current context
*
* @param request
* The HttpRequest object
* @param response
* The HttpResponses object
*/
@SuppressWarnings(&quot;unchecked&quot;)
static public void setCurrentContext(HttpServletRequest request,
HttpServletResponse response) {
AMFContext c = getCurrentContext();
if (c == null) {
c = new AMFContext(request, response);
tl.set(c);
} else {
c.setRequest(request);
c.setResponse(response);
}
}
/**
* Get current context value
*
* @return The current context
*/
static public AMFContext getCurrentContext() {
return (AMFContext) tl.get();
}
// ----------------------------------------------------------
//
// Class members
//
// ----------------------------------------------------------
/**
* The http request object. The lifecycle of the request object is defined
* as the request scope. It may be reused in another incoming connection, so
* dont use it in another thread.
*/
private HttpServletRequest request;
/**
* The http response object. The lifecycle of the response object is defined
* as the request scope. Dont use it in another thread. Also dont write
* output to the response when it is used in the context, but you may get or
* set some response header when it is safe.
*/
private HttpServletResponse response;
/**
* The constructor is private, to get an instance of the AMFContext, please
* use getCurrentContext() method.
*
* @param request
* @param response
*/
private AMFContext(HttpServletRequest request, HttpServletResponse response) {
this.request = request;
this.response = response;
}
/**
* Get request object
*
* @return Http request object
*/
public HttpServletRequest getRequest() {
return request;
}
/**
* Set request object
*
* @param Http
* request object
*/
public void setRequest(HttpServletRequest request) {
this.request = request;
}
/**
* Get response object
*
* @return Http response object
*/
public HttpServletResponse getResponse() {
return response;
}
/**
* Set response object
*
* @param response
* Http response object
*/
public void setResponse(HttpServletResponse response) {
this.response = response;
}
/**
* Get the servlet context
*
* @return
*/
public ServletContext getServletContext() {
HttpSession session = this.getSession();
return session.getServletContext();
}
/**
* Get the current running session
*
* @return
*/
public HttpSession getSession() {
return request.getSession();
}
/**
* Get an object stored in the session.
*
* @param attr
* Attribute Name
* @return The value stored under the attribute name.
*/
public Object getSessionAttribute(String attr) {
HttpSession session = this.getSession();
return session.getAttribute(attr);
}
/**
* Store an object in the session.
*
* @param attr
* Attribute Name
* @param value
* The value.
*/
public void setSessionAttribute(String attr, Object value) {
HttpSession session = this.getSession();
session.setAttribute(attr, value);
}
/**
* Get an object stored in the servlet context.
*
* @param attr
* Attribute Name
* @return The value stored under the attribute name.
*/
public Object getContextAttribute(String attr) {
ServletContext sc = this.getServletContext();
return sc.getAttribute(attr);
}
/**
* Store an object in the servlet context.
*
* @param attr
* Attribute Name
* @param value
* The value.
*/
public void setContextAttribute(String attr, Object value) {
ServletContext sc = this.getServletContext();
sc.setAttribute(attr, value);
}
/**
* Get an object stored in the current request.
*
* @param attr
* Attribute Name
* @return The value stored under the attribute name.
*/
public Object getRequestAttribute(String attr) {
return request.getAttribute(attr);
}
/**
* Store an object in the current request.
*
* @param attr
* Attribute Name
* @param value
* The value.
*/
public void setRequestAttribute(String attr, Object value) {
request.setAttribute(attr, value);
}
}
MethodInterceptor 文件
[复制到剪贴板]
CODE:
package com.sunwayworld.flex;
import javax.servlet.http.HttpServletRequest;
import org.aopalliance.intercept.MethodInterceptor;
import org.aopalliance.intercept.MethodInvocation;
import com.sunwayworld.common.utils.SessionUtils;
import flex.context.AMFContext;
public class FlexSessionInterceptor implements MethodInterceptor {
public Object invoke(MethodInvocation invocation) throws Throwable {
AMFContext context = AMFContext.getCurrentContext();
HttpServletRequest request = context.getRequest();
if (!SessionUtils.isLogin(request)) {
throw new RuntimeException(&quot;请您重新登陆!&quot;);
}
Object obj = invocation.proceed();
return obj;
}
}
然后 在spring 配置 成调用所有的java 方法前都执行这个 MethodInterceptor 即可
发表评论
-
Flex 非常实用的资料
2012-05-31 15:38 1223【改变输出swf的尺度,背景颜色或帧频】 在"Na ... -
Flex正则表达式规则
2012-05-31 15:35 17111.Flex正则表达式规则 1.1普通字符 字母、数字、汉 ... -
Flex中[Bindable]的使用心得
2012-05-30 16:55 920在Flex编程中,Bindble使用到最多的元数据。该标签可以 ... -
FLEX元标签_Bindable
2012-05-30 16:46 11061 概述 [Bindable ... -
Flex Bindable
2012-05-30 16:41 828对一个类声明绑定,相当于给这个类里的所有属性都声明了绑定。等同 ... -
Flex [Bindable] 以及使用方法
2012-05-30 16:37 1131绑定: 举个例子: 给下面的public变量加上[Bind ... -
关于flex开发自定义组件需要注意的问题
2012-05-23 14:24 1153一.首先回答,为什么要 ... -
Flex中自定义组件
2012-05-23 14:17 1113如要重写组件,就不得不了解Flex中组件初始化过程。初始化过程 ... -
flex,datagrid多列排序
2012-03-21 15:01 1078<?xml version="1.0" ... -
flex,datagrid 按照列来排序
2012-03-21 13:56 2139排序部分 import mx.controls.DateFie ... -
flex 动态给控件赋值,通过反射遍历MXML中的组件
2012-03-21 13:52 1373flex 动态给控件赋值,通过反射遍历MXML中的组件。当有1 ... -
flex双击不生效问题
2012-03-21 13:50 960今天用到flex的doubleclick的事件,但是不生效,原 ... -
FLEX datagrid 点击列头排序后,行编辑问题
2012-03-21 13:45 1001点击列头排序后。行编辑事件结束后,adobe默认会自己重新将d ... -
flex内存管理机制
2012-03-20 13:44 938一.简述Flex内存释放优 ... -
Flex 内存处理的方法
2012-03-20 11:25 8761.当任何对象unload后,fl ... -
Flex3 Profile的使用
2012-03-13 15:38 954flex的profile就是一个性能监测器,也是adobe向传 ... -
FLEX内存释放优化原则
2012-03-13 15:36 821FLEX内存释放优化原则: 1. 被删除对象在外部的所有引 ... -
flashplayer的缓存目录
2012-03-13 15:34 3061flashplayer的缓存目录是: XP系统下是C:/Do ... -
swf 更新后,客户端不需要清空缓存即可查看最新版本
2012-03-13 14:20 2390我们在用Flex开发应用的时候,每次修改了swf,上传到服务器 ... -
用RSL来为你的flex程序减肥
2012-03-13 14:19 1172用RSL来为你的flex程序减肥 问题:一个只有几个控件的 ...
相关推荐
控制器会调用相应的服务方法,该方法利用Hibernate的Session接口创建新的实体对象并保存到数据库。 2. **删除(Delete)**:用户选择要删除的条目,Flex发送包含ID的请求到Spring的Delete接口。控制器根据ID找到...
1. `<listener>`标签定义了一个监听器`flex.messaging.HttpFlexSession`,用于支持Flex通信的session。 2. `<servlet>`标签定义了名为`MessageBrokerServlet`的servlet,它是Blazeds的核心,处理与Flex客户端的通信...
例如,UserService可能会有一个saveUser(User user)方法,通过Session的save()或saveOrUpdate()方法将用户信息持久化到数据库。 Flex客户端与服务端的交互主要基于AMF(Action Message Format),这是一种高效的...
- 它包括一系列组件,如MessageBrokerServlet、HTTP Flex Session、Remoting服务等,用于实现远程方法调用、消息代理和数据推送。 4. **集成过程**: - 创建一个J2EE Web工程,如`Sample`,作为后端服务的基础。 ...
当涉及到与Hibernate的集成时,Spring提供了对Hibernate的全面支持,可以管理SessionFactory和Session。在配置文件中,我们可以定义数据访问层的bean,比如`SessionFactory`,并利用Spring的事务管理功能来处理...
10. **部署描述符** (`web.xml`):这是Java Web应用程序的核心配置文件,定义了过滤器、监听器、Servlet等元素,控制着应用的启动和运行行为。 通过以上配置,开发者可以创建一个强大的、灵活的Web应用程序,具有...
4. 使用Hibernate的Session接口进行数据库操作,如Session.save()、Session.load()、Session.update()和Session.delete()。 5. 结果封装为XML或JSON格式,通过Struts返回给Flex。 6. Flex接收到响应后更新UI。 深入...
通过定义目的地和指定方法访问权限,我们可以精细控制Flex应用程序与后端Java服务的交互,确保安全性和性能。在实际开发中,根据项目的具体需求,灵活调整`remote-config.xml`的配置,可以实现各种复杂的远程服务...
Spring MVC+BlazeDS+Flex框架实践:HelloWorld篇是一个典型的多层架构示例,它结合了Spring MVC作为后端控制器、BlazeDS作为数据通信中间件,以及Flex作为前端展示技术。本实践旨在帮助开发者了解如何将这些技术集成...
5. **Model-View-Controller(MVC)**:Spring的MVC模式使得前端Flex和后端业务逻辑之间有清晰的分隔,Flex负责视图和用户交互,Spring负责控制流程和业务逻辑。 6. **数据绑定**:Flex与Hibernate整合后,可以利用...
在Flex客户端,我们可以创建RemoteObject,指定其目标为Spring的bean,这样就可以调用后端的方法并处理返回的数据。 安全方面,Spring Security或Acegi(Spring的早期安全模块)可以用来保护应用的资源,提供认证和...
6. DAO层:创建`UserDAO.java`,包含具体的数据访问操作,如`findUserByUsernameAndPassword`方法。 五、Flex客户端开发 1. 使用FlashBuilder创建Flex项目,与后端的Struts2 Action进行通信,通常通过AMF(Action ...
Struts2允许开发者将业务逻辑与视图和控制层分离,提供了一套灵活的拦截器和插件机制。在这个示例中,Flex4发送的HTTP请求会被Struts2拦截并处理,执行相应的登录验证逻辑,如检查用户名和密码是否匹配。 **...
- 支持从Session中获取当前用户和访问级别信息,便于权限控制。 - 提供了方便的方法将请求参数映射到对象属性,简化了数据绑定过程。 **3.2 BaseBusiness的角色与功能** - **设计原则:** - 业务逻辑集中管理,...
- 在Cairngorm基础上进一步改进,使用FrontController模式可以更好地控制请求处理流程,提高系统的灵活性和可维护性。 5. **轮回转世—Mate样例** - Mate框架提供了一种更加轻量级的选择,适用于那些不需要...
6. 控制器层:Spring MVC的Controller类,负责处理Flex发送的HTTP请求,调用服务层方法,并返回响应数据。 7. 测试:可能包含单元测试和集成测试,验证各层的功能和协作。 这个示例项目可以帮助初学者理解如何在...
4. **建立数据访问层**:使用Hibernate的SessionFactory和Session接口,通过注解或XML映射文件来实现对象与数据库表之间的映射。 5. **Spring与Hibernate整合**:将Hibernate的SessionFactory注入到Spring中,通过...
总而言之,Flex 3界面布局教程第二篇深入介绍了Form和Grid这两种布局容器的使用方法和实现示例。通过对这两种布局方式的学习和实践,开发者可以更好地掌握Flex 3在界面布局方面的高级应用。Flex 3布局容器的灵活性和...
`flex-direction`定义主轴方向,`justify-content`调整沿主轴的对齐方式,`align-items`控制跨轴对齐,`align-self`允许单独调整子元素的跨轴对齐,以及`flex-grow`, `flex-shrink`和`flex-basis`用于控制伸缩比例和...