- 浏览: 65575 次
- 性别:
- 来自: 成都
文章分类
最新评论
在开发gwt时,有时需要同spring进行结合使用
下面是我根据spring4gwt上给出的相关信息做出来的一个demo
1、使用maven对jar包进行管理
2、自己写一个继承于RemoteServiceServlet的java文件
下面就将spring和gwt结合起来的代码:
主要思想就是
1、将请求解析为spring支持的bean
2、获取请求的方法和参数
3、调用方法和参数,并将结果返回
写道
package org.spring4gwt.server;
import javax.servlet.http.HttpServletRequest;
import org.apache.log4j.Logger;
import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;
import com.google.gwt.user.client.rpc.IncompatibleRemoteServiceException;
import com.google.gwt.user.client.rpc.RemoteService;
import com.google.gwt.user.client.rpc.SerializationException;
import com.google.gwt.user.server.rpc.RPC;
import com.google.gwt.user.server.rpc.RPCRequest;
import com.google.gwt.user.server.rpc.RemoteServiceServlet;
@SuppressWarnings("serial")
public class SpringGwtRemoteServiceServlet extends RemoteServiceServlet {
private static final Logger LOG = Logger.getLogger(SpringGwtRemoteServiceServlet.class);
// servlet的初始化方法
@Override
public void init() {
if (LOG.isDebugEnabled()) {
LOG.debug("Spring GWT service exporter deployed");
}
}
/**
* RemoteServiceServlet 中调用的方法,处理给定的request信息
*/
@Override
public String processCall(String payload) throws SerializationException {
try {
// 取出要被调用的bean实例
Object handler = getBean(getThreadLocalRequest());
// 对请求进行解码, 获取所有的请求信息, 主要是方法和参数
RPCRequest rpcRequest = RPC.decodeRequest(payload, handler.getClass(), this);
onAfterRequestDeserialized(rpcRequest);
if (LOG.isDebugEnabled()) {
LOG.debug("Invoking " + handler.getClass().getName() + "." + rpcRequest.getMethod().getName());
}
// 使用反射方式调用实例对象的方法, 返回调用结果
return RPC.invokeAndEncodeResponse(handler, rpcRequest.getMethod(), rpcRequest.getParameters(), rpcRequest
.getSerializationPolicy());
} catch (IncompatibleRemoteServiceException ex) {
log("An IncompatibleRemoteServiceException was thrown while processing this call.", ex);
return RPC.encodeResponseForFailure(null, ex);
}
}
/**
* 基于request URL进行的获取实例bean的操作
* 比如说:请求结尾时/myService,那么我们就会将spring中的bean实例为myService
*
* @param request
* @return handler bean
*/
protected Object getBean(HttpServletRequest request) {
String service = getService(request);
// 从spring中的applicationContext中取出bean
Object bean = getBean(service);
if (!(bean instanceof RemoteService)) {
throw new IllegalArgumentException("Spring bean is not a GWT RemoteService: " + service + " (" + bean + ")");
}
if (LOG.isDebugEnabled()) {
LOG.debug("Bean for service " + service + " is " + bean);
}
return bean;
}
/**
* 解析bean的名称,通过request来解析
* 如/spring 会被解析为 spring
* @param request
* @return bean name
*/
protected String getService(HttpServletRequest request) {
// 获得url
String url = request.getRequestURI();
// 对取最后一个"/"后面的内容
String service = url.substring(url.lastIndexOf("/") + 1);
if (LOG.isDebugEnabled()) {
LOG.debug("Service for URL " + url + " is " + service);
}
return service;
}
/**
* 在当前webApplicationContext中取出bean的实例
*
* @param name
* bean name
* @return the bean
*/
protected Object getBean(String name) {
// 调用spring自带的utils
WebApplicationContext applicationContext = WebApplicationContextUtils
.getWebApplicationContext(getServletContext());
if (applicationContext == null) {
throw new IllegalStateException("No Spring web application context found");
}
if (!applicationContext.containsBean(name)) {
{
throw new IllegalArgumentException("Spring bean not found: " + name);
}
}
// 获取bean实例
return applicationContext.getBean(name);
}
}
import javax.servlet.http.HttpServletRequest;
import org.apache.log4j.Logger;
import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;
import com.google.gwt.user.client.rpc.IncompatibleRemoteServiceException;
import com.google.gwt.user.client.rpc.RemoteService;
import com.google.gwt.user.client.rpc.SerializationException;
import com.google.gwt.user.server.rpc.RPC;
import com.google.gwt.user.server.rpc.RPCRequest;
import com.google.gwt.user.server.rpc.RemoteServiceServlet;
@SuppressWarnings("serial")
public class SpringGwtRemoteServiceServlet extends RemoteServiceServlet {
private static final Logger LOG = Logger.getLogger(SpringGwtRemoteServiceServlet.class);
// servlet的初始化方法
@Override
public void init() {
if (LOG.isDebugEnabled()) {
LOG.debug("Spring GWT service exporter deployed");
}
}
/**
* RemoteServiceServlet 中调用的方法,处理给定的request信息
*/
@Override
public String processCall(String payload) throws SerializationException {
try {
// 取出要被调用的bean实例
Object handler = getBean(getThreadLocalRequest());
// 对请求进行解码, 获取所有的请求信息, 主要是方法和参数
RPCRequest rpcRequest = RPC.decodeRequest(payload, handler.getClass(), this);
onAfterRequestDeserialized(rpcRequest);
if (LOG.isDebugEnabled()) {
LOG.debug("Invoking " + handler.getClass().getName() + "." + rpcRequest.getMethod().getName());
}
// 使用反射方式调用实例对象的方法, 返回调用结果
return RPC.invokeAndEncodeResponse(handler, rpcRequest.getMethod(), rpcRequest.getParameters(), rpcRequest
.getSerializationPolicy());
} catch (IncompatibleRemoteServiceException ex) {
log("An IncompatibleRemoteServiceException was thrown while processing this call.", ex);
return RPC.encodeResponseForFailure(null, ex);
}
}
/**
* 基于request URL进行的获取实例bean的操作
* 比如说:请求结尾时/myService,那么我们就会将spring中的bean实例为myService
*
* @param request
* @return handler bean
*/
protected Object getBean(HttpServletRequest request) {
String service = getService(request);
// 从spring中的applicationContext中取出bean
Object bean = getBean(service);
if (!(bean instanceof RemoteService)) {
throw new IllegalArgumentException("Spring bean is not a GWT RemoteService: " + service + " (" + bean + ")");
}
if (LOG.isDebugEnabled()) {
LOG.debug("Bean for service " + service + " is " + bean);
}
return bean;
}
/**
* 解析bean的名称,通过request来解析
* 如/spring 会被解析为 spring
* @param request
* @return bean name
*/
protected String getService(HttpServletRequest request) {
// 获得url
String url = request.getRequestURI();
// 对取最后一个"/"后面的内容
String service = url.substring(url.lastIndexOf("/") + 1);
if (LOG.isDebugEnabled()) {
LOG.debug("Service for URL " + url + " is " + service);
}
return service;
}
/**
* 在当前webApplicationContext中取出bean的实例
*
* @param name
* bean name
* @return the bean
*/
protected Object getBean(String name) {
// 调用spring自带的utils
WebApplicationContext applicationContext = WebApplicationContextUtils
.getWebApplicationContext(getServletContext());
if (applicationContext == null) {
throw new IllegalStateException("No Spring web application context found");
}
if (!applicationContext.containsBean(name)) {
{
throw new IllegalArgumentException("Spring bean not found: " + name);
}
}
// 获取bean实例
return applicationContext.getBean(name);
}
}
- simplerpc.rar (11.2 KB)
- 下载次数: 13
发表评论
-
FastJSON使用
2015-06-30 10:25 721fastJSON基本使用: public static S ... -
堆排序
2014-06-07 22:23 704堆排序有以下需要注意的: parent=n/2 l ... -
java
2014-03-20 21:44 703Caused by: org.springframework ... -
guava测试例子,取值和定时重新装载
2014-02-21 17:54 1122package com.taobao.saleengine. ... -
java大顶堆排序
2013-09-11 23:59 1266public class Heap{ // 构造 ... -
java树结构简单实现
2013-09-10 23:46 6312import java.util.Scanner; ... -
java
2012-12-13 16:52 0zookeeper基础:http://www.ibm.com/ ... -
插入排序的实现
2012-05-27 13:54 851插入排序是最简单的排序算法 : 看了看书按照书上说的对插入排 ... -
在web中直接调用Nutch执行JOB
2012-05-27 01:15 4633看了http://zha-zi.iteye.com/blog/ ... -
gxt 主题
2012-01-04 13:15 1561在使用gxt的时候,我们会注意到页面的主题, 如果我们想改变 ... -
gxt 中文
2012-01-03 13:30 5097在使用gxt的时候 发现默认使用的是English 这样的话 ... -
JDBC连接mysql
2011-10-29 17:13 1657个人开发时主要还是使用mysql,所以就顺便把连接mysql的 ... -
java 反射调用类的方法
2011-10-29 00:29 8903最近遇到一个问题,重复的写一些方法,这些方法基本除了调用对象的 ... -
java多线程学习网址
2011-08-15 08:54 0http://lavasoft.blog.51cto.com/ ...
相关推荐
GWT(Google Web Toolkit) 是 Google 最近推出的一个开发 Ajax 应用的框架,它支持用 Java 开发和调试 Ajax 应用,本文主要介绍如何利用 GWT 进行 Ajax 的开发。 GWT特性简介 1.动态,可重用的UI组件 GWT提供的...
通过这本精巧的食谱,深入了解GWT编程世界,其中介绍了GWT Toolkit的常用配方! 它涵盖了广泛的主题,从示例应用程序和面试问题到回调功能和各种小部件。
Google Web toolkit (gwt)的介绍,可以快速进入
GWT(Google Web Toolkit) 是 Google 最近推出的一个开发 Ajax 应用的框架,它支持用 Java 开发和调试 Ajax 应用,本文主要介绍如何利用 GWT 进行 Ajax 的开发。 Ajax技术是当前开发web应用的非常热门的技术,也是...
Google Web Toolkit Applications(GWT)入门参考资料,具有一定参考价值
Google Web Toolkit参考书籍
google web toolkit (GWT)1.5.3,最新版本!
Google Web Toolkit (GWT),可以使用 Java 编程语言编写 AJAX 前端,然后 GWT 会交叉编译到优化的 JavaScript 中,而 JavaScript 可以自动在所有主要浏览器上运行。在开发过程中,您可以用 JavaScript 按照您习惯的...
使用 Google Web Toolkit、Apache Derby 和 Eclipse 构建 Ajax 应用程序 图文教程
Accelerated GWT: Building Enterprise Google Web Toolkit Applications
Google Web Toolkit(GWT)
官网gwt SDK ,通过java 开发前端页面,并且自动解决页面兼容及国际化问题,以MVP为主开发模式
GWT学习资料
Using Google Web Toolkit (GWT) for Developing AJAX-Based Web Applications for the SAP NetWeaver J2EE Framework.pdf
Google Web Toolkit Applications(GWT)中级参考资料,具有一定参考价值
有了 Google Web Toolkit (GWT),可以使用 Java 编程语言编写 AJAX 前端,然后 GWT 会交叉编译到优化的 JavaScript 中,而 JavaScript 可以自动在所有主要浏览器上运行。在开发过程中,您可以用 JavaScript 按照您...
面向 Java 开发人员的 Ajax: Google Web Toolkit 入门 图文教程
gwt api帮助手册 chm格式的 供大家学习用!