gwt spring整合

gwt+spring整合

 

目的:让gwt与spring整合

思路:建立一个实现了RemoteServiceServlet的servlet,根据传递的参数调用spring WebApplicationContext容器里注册的服务,返回结果!

 

实现:

1.建立一个实现了RemoteServiceServlet的servlet

 

public class GwtSpring extends RemoteServiceServlet{

	/**
	 * spring容器上下文
	 */
	private WebApplicationContext springContext;

	/**
	 * 覆盖servlet.init方法 
	 * 获取spring WebApplicationContext容器上下文
	 */
	@Override
	public void init(ServletConfig config) throws ServletException {
		super.init(config);
		springContext = (WebApplicationContext) config.getServletContext().getAttribute( 
		WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE); 

		if (springContext == null) { 
			throw new RuntimeException("Check Your Web.Xml Setting, No Spring Context Configured"); 
		} 
	}
	
	/**
	 * 覆盖RemoteServiceServlet方法
	 * 获取调用参数,取得容器里的服务对象进行处理,返回处理结果
	 */
	@Override
	public String processCall(String payload) throws SerializationException {
		//获取HttpServletRequest对象
		HttpServletRequest req = getThreadLocalRequest();
		//获取服务对象,此处采用url比对,与servlet url-pattern配合
		//url样式:/gwtcrud/spring/*
		//*号代表容器里服务对象ID
		String requestURI = req.getRequestURI();
		String beanname = requestURI.substring(requestURI.lastIndexOf("/")+1);
		RemoteService service = (RemoteService) springContext.getBean(beanname);
		
		//调用服务对象,返回结果,参考:RemoteServiceServlet.processCall
		try {
			RPCRequest rpcRequest = RPC.decodeRequest(payload, service.getClass(),
					this);
			return RPC.invokeAndEncodeResponse(service, rpcRequest.getMethod(),
					rpcRequest.getParameters(), rpcRequest
							.getSerializationPolicy());
		} catch (IncompatibleRemoteServiceException ex) {
			getServletContext()
				.log("An IncompatibleRemoteServiceException was thrown while processing this call.",ex);
			return RPC.encodeResponseForFailure(null, ex);
		}
	}
	
	
}

 

 

2.配置web.xml

 

<servlet>
		<servlet-name>GwtSpring</servlet-name>
		<servlet-class>com.gwtcrud.server.GwtSpring</servlet-class>
</servlet>

<servlet-mapping>
		<servlet-name>GwtSpring</servlet-name>
		<url-pattern>/gwtcrud/spring/*</url-pattern>
</servlet-mapping>
 

 

3.写远程接口

 

/**
 * 调用spring容器中id为carService的对象
 * @author Administrator
 *
 */
@RemoteServiceRelativePath("spring/carService")
public interface CarService extends RemoteService{
	public Car getCar();
	public Car saveCar(Car car);
	public List<Car> getAllCars();
}
 

 

功能: 实现数据分页查询显示 特点: 全是免费工具及开源技术 开发框架: gwt1.4.6 + spring2.5 + mysql5.0 开发工具: Eclipse3.4 Cypal Studio for GWT (Eclipse 的一个插件) 实现原理:通过GWT的RPC来调用Spring 传过来的服务器端数据 注意:需要的jar包 * gwt-user.jar * gwt-servlet.jar * mysql-connectot-java-3.1.12.jar * spring.jar * commons-dbcp.1.2.1.jar * coommons-pool-1.4.jar Sql语句: create database booksearch; create table book ( id int not null auto_increment primary key, title varchar(100), isbn varchar(100), edition varchar(100), msrp varchar(100) ) insert into book(title,isbn,edition,msrp) values('title_1','i-sbn-1','editon-1','msrp-1'); insert into book(title,isbn,edition,msrp) values('title_2','i-sbn-2','editon-2','msrp-2'); insert into book(title,isbn,edition,msrp) values('title_3','i-sbn-3','editon-3','msrp-3'); insert into book(title,isbn,edition,msrp) values('title_4','i-sbn-4','editon-4','msrp-4'); insert into book(title,isbn,edition,msrp) values('title_5','i-sbn-5','editon-5','msrp-5'); insert into book(title,isbn,edition,msrp) values('title_6','i-sbn-6','editon-6','msrp-6'); insert into book(title,isbn,edition,msrp) values('title_7','i-sbn-7','editon-7','msrp-7'); insert into book(title,isbn,edition,msrp) values('title_8','i-sbn-8','editon-8','msrp-8'); insert into book(title,isbn,edition,msrp) values('title_9','i-sbn-9','editon-9','msrp-9'); insert into book(title,isbn,edition,msrp) values('title_10','i-sbn-10','editon-10','msrp-10');
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值