完善GWT官网提出的Spring集成方案(获取HttpServletRequest,session)

 我就不讲  官网是怎么集成的了 具体看这地址 :http://code.google.com/p/spring4gwt/wiki/SimpleRPCExample

 

在集成中 其中有一个叫 spring4gwt-0.1.jar的JAR包

 

你打开这JAR包 你会惊奇的发现 这个包里面只一个类..!

这就好办了  将这类 反编译出来

 

自己重新命名一个叫BaseServiceServlet.java类

然后在其原来的代码上增加如下代码:

 

InterfaceUtil util = new InterfaceUtil();

			boolean flag = util.isInterface(handler.getClass(),
					IRequestAware.class.getName()); //判断是否实现了IRequestAware接口
			if (flag) {//如果实现了IRequestAware就进行HttpServletRequest注入
				Method method = null;
				try {
					method = handler.getClass().getDeclaredMethod(
							"setServletRequest",
							new Class[] { HttpServletRequest.class });
					method.invoke(handler, getThreadLocalRequest());
				} catch (NoSuchMethodException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				} catch (SecurityException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				} catch (IllegalAccessException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				} catch (IllegalArgumentException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				} catch (InvocationTargetException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}


如图:

IRequestAware接口:

package com.fjw.tt.sp.share;

import javax.servlet.http.HttpServletRequest;

/**
 * 实现此接口 即可获取页面中的HttpServletRequest
 * @author Administrator
 *
 */
public interface IRequestAware {

 
 void setServletRequest(HttpServletRequest request);
}


InterfaceUtil类:

package com.fjw.tt.sp.share;

/* 判断对象o实现的所有接口中是否有szInterface 
 * 2008-08-07 修正多继承中判断接口的功能,
 * 以及修正接口继承后的判断功能
 * package test;
 * 
 * public interface ITest extends Serializable
 * public class Test1 implements ITest
 * public class Test2 extends Test1
 * public class Test3 extends Test2 
 * 
 * isInterface(Test3.class, "java.io.Serializable") = true
 * isInterface(Test3.class, "test.ITest") = true
 * @param c
 * @param szInterface
 * @return
 */

public class InterfaceUtil {
	public boolean isInterface(Class c, String szInterface) {
		Class[] face = c.getInterfaces();
		for (int i = 0, j = face.length; i < j; i++) {
			if (face[i].getName().equals(szInterface)) {
				return true;
			} else {
				Class[] face1 = face[i].getInterfaces();
				for (int x = 0; x < face1.length; x++) {
					if (face1[x].getName().equals(szInterface)) {
						return true;
					} else if (isInterface(face1[x], szInterface)) {
						return true;
					}
				}
			}
		}
		if (null != c.getSuperclass()) {
			return isInterface(c.getSuperclass(), szInterface);
		}
		return false;
	}
}

原来的WEB配置是这样的:

 

现在改为:

 

<servlet>
  <servlet-name>springGwtRemoteServiceServlet</servlet-name>
  <servlet-class>com.fjw.tt.sp.share.BaseServiceServlet</servlet-class>
 </servlet>

 <servlet-mapping>
  <servlet-name>springGwtRemoteServiceServlet</servlet-name>
  <url-pattern>/sp/springGwtServices/*</url-pattern>
 </servlet-mapping>

将org.spring4gwt.server.SpringGwtRemoteServiceServlet 改为 com.fjw.tt.sp.share.BaseServiceServlet

 

 

使用的是否如果你想获取HttServletRequest的时候 只要实现IRequestAware接口 即可获取到

如图:


后记:

官网有人提出来了  如果你试图去获取HttpSerlvetRequest或Session什么的  这就要重新思考你项目是否设计有问题,

但是我们项目中确实要用到这些 好吧 我的项目设计可能真有的问题。

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值