当前位置: 代码迷 >> Web前端 >> 筹建简单的EXT-GWT(GXT)的开发环境(二)添加spring进行servlet拦截
  详细解决方案

筹建简单的EXT-GWT(GXT)的开发环境(二)添加spring进行servlet拦截

热度:603   发布时间:2012-08-27 21:21:57.0
搭建简单的EXT-GWT(GXT)的开发环境(二)添加spring进行servlet拦截
随便写着玩玩.不要太认真哦^^

关于一大堆的理论就不讲了,什么MVC之类的,老人不削一看,新人又看不懂

总结一下其实是我也不清楚.^^哈哈.

再我看来,添加spring进来就是为了简化RPC的服务端代码,然后在其中注入dao比较方便..没其他的了~

1.首先把spring2.5中的spring.jar,spring-webmvc.jar,aspectjrt.jar等等jar包添加到项目中.

2.创建一个包,然后添加一个Controller类进来.这个类要代替RPC服务的继承父类,所以我们把这个Controller继承RemoteServiceServlet,这样,以后RPC的服务就可以直接继承这个Controller了
然后让这个Controller实现几个口:Controller,ServletConfigAware,ServletContextAware.这里就不说为什么要继承这几个接口了.下面就是这个类的所有代码.
package com.yx.map.spring;

import javax.servlet.ServletConfig;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.web.context.ServletConfigAware;
import org.springframework.web.context.ServletContextAware;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.Controller;

import com.google.gwt.user.server.rpc.RemoteServiceServlet;

/**
 * 
 * @author yx200404
 *
 * 2011-8-22
 */
public class GWTController extends RemoteServiceServlet implements Controller,
		ServletConfigAware,ServletContextAware {

	/**
	 * 
	 */
	private static final long serialVersionUID = 2275269119904991518L;
	/*  */
	private ServletContext servletContext;

	@Override
	public void setServletConfig(ServletConfig arg0) {
		try {
			super.init(arg0);
		} catch (ServletException e) {
			e.printStackTrace();
		}
	}
	
	@Override
	public void init(ServletConfig config) throws ServletException {
		super.init(config);
	}
	
	public ModelAndView handleRequest(HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		doPost(request, response);
		return null;
	}

	@Override
	public void setServletContext(ServletContext servletContext) {
		this.servletContext = servletContext;
	}

	@Override
	public ServletContext getServletContext() {
		return servletContext;
	}
}



3.改造GreetingService让它处于spring的监管下
  打开GreetingService,然后去掉 @RemoteServiceRelativePath("greet") 注解.
  打开GreetingServiceImpl,修改父类为我们刚刚写的Controller
public class GreetingServiceImpl extends GWTController implements
		GreetingService 

然后给它添加一个注解 @Service("greetingService"),这个service是org.springframework.stereotype此包下的.
@Service("greetingService")
public class GreetingServiceImpl extends GWTController implements
		GreetingService


4.修改web.xml
<web-app>
	<context-param>
		<param-name>contextConfigLocation</param-name>
		<param-value>classpath*:spring/*.xml</param-value>
	</context-param>

	<listener>
		<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
	</listener>

	<servlet>
		<servlet-name>gwtrpc</servlet-name>
		<servlet-class> org.springframework.web.servlet.DispatcherServlet</servlet-class>
		<load-on-startup>1</load-on-startup>
	</servlet>
	<servlet-mapping>
		<servlet-name>gwtrpc</servlet-name>
		<url-pattern>*.gwt</url-pattern>
	</servlet-mapping>

	<!-- Default page to serve -->
	<welcome-file-list>
		<welcome-file>GoogleMapV3.html</welcome-file>
	</welcome-file-list>

</web-app>


5.修改GoogleMapV3.gwt.xml,添加servlet
<servlet class="com.yx.map.server.GreetingServiceImpl"
		path="/greetingService.gwt" />


6.给项目添加一个spring配置文件.具体做法是在项目中添加一个源包resources,然后在里面添加一个包spring,然后添加applicationContext.xml到其中
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop"
	xmlns:tx="http://www.springframework.org/schema/tx" xmlns:context="http://www.springframework.org/schema/context"
	xmlns:jee="http://www.springframework.org/schema/jee"
	xsi:schemaLocation="
    http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd
    http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
    http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd
    http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
     http://www.springframework.org/schema/jee
       http://www.springframework.org/schema/jee/spring-jee-2.5.xsd"
	default-autowire="byName">

	<context:component-scan base-package="com.yx" />

</beans>


7.为GXT注册新的servlet地址,添加以下代码到你自己的代码中就可以了
public static final String Greeting_SERVICE = "/greetingService.gwt";

public void onModuleLoad() {

		ServiceDefTarget endpoint = (ServiceDefTarget) greetingService;
		String moduleRelativeURL = Greeting_SERVICE;
		endpoint.setServiceEntryPoint(moduleRelativeURL);
		Registry.register(Greeting_SERVICE, greetingService);

		// 设置样式
		GXT.setDefaultTheme(Theme.GRAY, true);
                //.....下面的代码不变



好了...重新启动下,看看效果是不是一样的呢?^^

过会再发个gxt配合Hibernate使用的方案.
1 楼 200720072007 2012-04-01  
org.springframework.beans.factory.BeanDefinitionStoreException: IOException parsing XML document from ServletContext resource [/WEB-INF/gwtrpc-servlet.xml]; nested exception is java.io.FileNotFoundException: Could not open ServletContext resource [/WEB-INF/gwtrpc-servlet.xml]

这里是不是少文件了
  相关解决方案