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

来源:互联网 发布:沈阳酒店业2017数据 编辑:程序博客网 时间:2024/06/07 14:50

 我就不讲  官网是怎么集成的了 具体看这地址 :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 blocke.printStackTrace();} catch (SecurityException e) {// TODO Auto-generated catch blocke.printStackTrace();} catch (IllegalAccessException e) {// TODO Auto-generated catch blocke.printStackTrace();} catch (IllegalArgumentException e) {// TODO Auto-generated catch blocke.printStackTrace();} catch (InvocationTargetException e) {// TODO Auto-generated catch blocke.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什么的  这就要重新思考你项目是否设计有问题,

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

 

原创粉丝点击