1. 我们这里整合spring主要是用到了一个组件gwt-widgets-server 可以在
http://jaist.dl.sourceforge.net/sourceforge/gwt-widget/gwt-widgets-server-0.1.2-bin.zip下载。
2. 我们使用gwt的命令(projectCreator applicationCreator )生成gwt的eclipse项目,参见: http://code.google.com/webtoolkit/gettingstarted.html
3. 导入eclipse中,导入后请看附件1.
4. 分析代码
首先看web.xml
可以看到我们增加了段GWTSpring的servlet配置,熟悉spring-web的朋友一眼就可以看出。gwt-widgets-server 正是使用了spring-web的control来做gwt的remoteServiceServlet的。他是使用了cglib或者javassist 来动态使我们的Sevice class继承RemoteServiceServlet。细心的朋友可以看到代码中的粗体部分,本来直接生成的gwt模版是"/*"的url- pattern。但是我这里为什么要在前面加上"/static"部分呢?请让我在后面为你解释。下面看看GWTSpring-servlet.xml的配置。
<beans><bean depends-on="userServiceTarget" class="org.gwtwidgets.server.spring.GWTHandler" id="urlMapping"> <property value="false" name="usingInstrumentation"><bean class="org.springframework.aop.framework.ProxyFactoryBean" id="userService">
</bean></property> </bean></beans><beans><bean depends-on="userServiceTarget" class="org.gwtwidgets.server.spring.GWTHandler" id="urlMapping"> <property value="false" name="usingInstrumentation"><bean class="org.springframework.aop.framework.ProxyFactoryBean" id="userService"> <property ref="userServiceTarget" name="target"><bean id="requestSetter"> </bean></property> </bean></property> </bean></beans> <beans><bean depends-on="userServiceTarget" class="org.gwtwidgets.server.spring.GWTHandler" id="urlMapping"> <property value="false" name="usingInstrumentation"><bean class="org.springframework.aop.framework.ProxyFactoryBean" id="userService"> <property ref="userServiceTarget" name="target"><bean id="requestSetter">我们配置了一个 “/userService.rpc”的servlet mapping。你应该可以想到这个url应该就是gwt的RemoteServiceServlet的url。我不想多解释配置里面的内容,因为我也是抄的,如果仔细看的话应该可以明白,如果不明白的话可以看看gwt-widgets-server的源码。这段配置里面主要就是初始化了一个gwt的RemoteServiceServlet。他可以是任何的object。不需要显示的继承RemoteServiceServlet,一切都让 org.gwtwidgets.server.spring.GWTHandler帮你解决吧。他还帮你在RemoteServiceServlet注入了httpServletRequest 和 httpServletResponse。你不需要使用通过RemoteServiceServlet.getThreadLocalRequest()去取。下面看看 UserServiceImpl
这里面有个checkUser方法。在这里我发现了一个问题。gwt-widgets-server中的例子上面setRequest和setResponse方法里面的参数的类型是HttpServletRequest和HttpServletResponse但是这样的话,requst和response不能被注入。清看 gwt-widgets-server中的RequestInjection class
请看上面的粗体部分他这里取的参数类型是ServletRequest和ServletResponse。所以我把UserServiceImpl中的setRequest和setResponse类型改成了ServletRequest和ServletResponse。在getRequest中cast成HttpServletRequest。ok,看看在gwt的client中怎么调用吧。
ok。到此配置结束。对了好像还忘了解释为什么要在web.xml中加上/static这段。原因是如果shell的url-pattern是/*的话,就会handler了gwt的RemoteServiceServlet的路径,这样的话remote rpc调用就总是Server error了。这时我们访问地址需要变成http://localhost:8888/static/com.javaeye.dengyin2000.truck.TruckApp/TruckApp.html 这样了。我提交了整个eclipse gwt项目所有代码但是把require lib去掉了。 需要的lib是spring-1.2.8.jar log4j-1.2.13.jar javassist-3.0.jar gwt-widgets-server-0.1.2.jar gwt-servlet.jar commons-logging-1.1.jar cglib-nodep-2.1_3.jar。请自行下载然后放到eclipse项目中的tomcat\webapps\ROOT\WEB-INF\lib\下面。你还需要把项目中引用到gwt lib的路径换成你自己机子上的。
:)
参考:http://g.georgovassilis.googlepages.com/usingthegwthandler
</bean></property> </bean></property> </bean></beans>
2. 我们使用gwt的命令(projectCreator applicationCreator )生成gwt的eclipse项目,参见: http://code.google.com/webtoolkit/gettingstarted.html
3. 导入eclipse中,导入后请看附件1.
4. 分析代码
首先看web.xml
- xml version="1.0" encoding="UTF-8"?>
- <web-app>
- <servlet>
- <servlet-name>GWTSpringservlet-name>
- <servlet-class>
- org.springframework.web.servlet.DispatcherServlet
- servlet-class>
- <load-on-startup>1load-on-startup>
- servlet>
- <servlet-mapping>
- <servlet-name>GWTSpringservlet-name>
- <url-pattern>*.rpcurl-pattern>
- servlet-mapping>
- <servlet>
- <servlet-name>shellservlet-name>
- <servlet-class>com.google.gwt.dev.shell.GWTShellServletservlet-class>
- servlet>
- <servlet-mapping>
- <servlet-name>shellservlet-name>
- <url-pattern>/static/*url-pattern>
- servlet-mapping>
- web-app>
可以看到我们增加了段GWTSpring的servlet配置,熟悉spring-web的朋友一眼就可以看出。gwt-widgets-server 正是使用了spring-web的control来做gwt的remoteServiceServlet的。他是使用了cglib或者javassist 来动态使我们的Sevice class继承RemoteServiceServlet。细心的朋友可以看到代码中的粗体部分,本来直接生成的gwt模版是"/*"的url- pattern。但是我这里为什么要在前面加上"/static"部分呢?请让我在后面为你解释。下面看看GWTSpring-servlet.xml的配置。
- xml version="1.0" encoding="UTF-8"?>
- >
- <beans>
- <bean id="urlMapping" class="org.gwtwidgets.server.spring.GWTHandler" depends-on="userServiceTarget">
- <property name="mapping">
- <map>
- <entry key="/userService.rpc" value-ref="userService" />
- map>
- property>
- <property name="usingInstrumentation" value="false" />
- <property name="unwrappingGWTExceptions" value="true" />
- You can specify your own class enhancer here
- <property name="classEnhancer">
- <bean class="org.gwtwidgets.server.spring.enhancer.JavassistEnhancer"/>
- property>
- -->
- bean>
- <bean id="userServiceTarget" class="com.javaeye.dengyin2000.truck.server.impl.UserServiceImpl">
- bean>
- <bean id="userService" class="org.springframework.aop.framework.ProxyFactoryBean">
- <property name="target" ref="userServiceTarget" />
- <property name="autodetectInterfaces" value="true" />
- <property name="interceptorNames">
- <list>
- <value>requestSettervalue>
- list>
- property>
- bean>
- <bean id="requestSetter"
- class="org.gwtwidgets.server.spring.RequestInjection">
- <property name="requestSetterName" value="setRequest" />
- <property name="responseSetterName" value="setResponse" />
- bean>
- beans>
<beans><bean depends-on="userServiceTarget" class="org.gwtwidgets.server.spring.GWTHandler" id="urlMapping"> <property value="false" name="usingInstrumentation"><bean class="org.springframework.aop.framework.ProxyFactoryBean" id="userService">
</bean></property> </bean></beans><beans><bean depends-on="userServiceTarget" class="org.gwtwidgets.server.spring.GWTHandler" id="urlMapping"> <property value="false" name="usingInstrumentation"><bean class="org.springframework.aop.framework.ProxyFactoryBean" id="userService"> <property ref="userServiceTarget" name="target"><bean id="requestSetter"> </bean></property> </bean></property> </bean></beans> <beans><bean depends-on="userServiceTarget" class="org.gwtwidgets.server.spring.GWTHandler" id="urlMapping"> <property value="false" name="usingInstrumentation"><bean class="org.springframework.aop.framework.ProxyFactoryBean" id="userService"> <property ref="userServiceTarget" name="target"><bean id="requestSetter">我们配置了一个 “/userService.rpc”的servlet mapping。你应该可以想到这个url应该就是gwt的RemoteServiceServlet的url。我不想多解释配置里面的内容,因为我也是抄的,如果仔细看的话应该可以明白,如果不明白的话可以看看gwt-widgets-server的源码。这段配置里面主要就是初始化了一个gwt的RemoteServiceServlet。他可以是任何的object。不需要显示的继承RemoteServiceServlet,一切都让 org.gwtwidgets.server.spring.GWTHandler帮你解决吧。他还帮你在RemoteServiceServlet注入了httpServletRequest 和 httpServletResponse。你不需要使用通过RemoteServiceServlet.getThreadLocalRequest()去取。下面看看 UserServiceImpl
java 代码
- package com.javaeye.dengyin2000.truck.server.impl;
- import javax.servlet.ServletRequest;
- import javax.servlet.ServletResponse;
- import javax.servlet.http.HttpServletRequest;
- import com.javaeye.dengyin2000.truck.client.server.UserService;
- public class UserServiceImpl implements UserService {
- private static ThreadLocal<servletrequest> servletRequest = </servletrequest>new ThreadLocal<servletrequest>(); </servletrequest>
- private static ThreadLocal<servletresponse> servletResponse = </servletresponse>new ThreadLocal<servletresponse>(); </servletresponse>
- public void setRequest(ServletRequest request) {
- servletRequest.set(request);
- }
- public void setResponse(ServletResponse request) {
- servletResponse.set(request);
- }
- protected HttpServletRequest getRequest() {
- return (HttpServletRequest) servletRequest.get();
- }
- public boolean checkUser(String userName, String password) {
- if ("denny".equals(userName) && "123456".equals(password)) {
- getRequest().getSession().setAttribute("user", userName);
- return Boolean.TRUE;
- }
- return Boolean.FALSE;
- }
- }
这里面有个checkUser方法。在这里我发现了一个问题。gwt-widgets-server中的例子上面setRequest和setResponse方法里面的参数的类型是HttpServletRequest和HttpServletResponse但是这样的话,requst和response不能被注入。清看 gwt-widgets-server中的RequestInjection class
java 代码
- package org.gwtwidgets.server.spring;
- import java.lang.reflect.Method;
- import javax.servlet.ServletRequest;
- import javax.servlet.ServletResponse;
- import javax.servlet.http.HttpServletRequest;
- import javax.servlet.http.HttpServletResponse;
- import org.aopalliance.intercept.MethodInterceptor;
- import org.aopalliance.intercept.MethodInvocation;
- /**
- * Method interceptor that injects the current {@link HttpServletRequest} and
- * the current {@link HttpServletResponse} via reflection to a target. You must specify
- * the setter names via {@link #setRequestSetterName(String)} and {@link #setResponseSetterName(String)}
- * otherwise they will fail silently. These setters can and must have only a single argument
- * namely {@link ServletRequest} and {@link ServletResponse} respectively.
- *
- * @author George Georgovassilis
- *
- */
- public class RequestInjection implements MethodInterceptor {
- protected String requestSetterName;
- protected String responseSetterName;
- private void setRequestOnTarget(HttpServletRequest request, HttpServletResponse response, Object target)
- throws Exception {
- if (requestSetterName != null)
- try {
- Method method = target.getClass().getMethod(requestSetterName,
- new Class[] { ServletRequest.class });
- method.invoke(target, new Object[] { request });
- } catch (NoSuchMethodException e) {
- }
- if (responseSetterName != null)
- try {
- Method method = target.getClass().getMethod(responseSetterName,
- new Class[] { ServletResponse.class });
- method.invoke(target, new Object[] { response });
- } catch (NoSuchMethodException e) {
- }
- }
- public Object invoke(MethodInvocation invocation) throws Throwable {
- Object target = invocation.getThis();
- setRequestOnTarget(GWTSpringController.getRequest(), GWTSpringController.getResponse(), target);
- return invocation.proceed();
- }
- /**
- * Specify the name of the setter method that can be invoked to set the
- * current request and response on the target. If the method does not
- * exist on the target, it is silently discarded.
- *
- * @param setterName
- */
- public void setRequestSetterName(String setterName) {
- this.requestSetterName = setterName;
- }
- /**
- * Specify the name of the setter method that can be invoked to set the
- * current response on the target. If the method does not exist on the
- * target, it is silently discarded.
- *
- * @param setterName
- */
- public void setResponseSetterName(String setterName) {
- this.responseSetterName = setterName;
- }
- }
请看上面的粗体部分他这里取的参数类型是ServletRequest和ServletResponse。所以我把UserServiceImpl中的setRequest和setResponse类型改成了ServletRequest和ServletResponse。在getRequest中cast成HttpServletRequest。ok,看看在gwt的client中怎么调用吧。
java 代码
- package com.javaeye.dengyin2000.truck.client;
- import com.google.gwt.core.client.EntryPoint;
- import com.google.gwt.core.client.GWT;
- import com.google.gwt.user.client.Window;
- import com.google.gwt.user.client.rpc.AsyncCallback;
- import com.google.gwt.user.client.rpc.ServiceDefTarget;
- import com.google.gwt.user.client.ui.Button;
- import com.google.gwt.user.client.ui.ClickListener;
- import com.google.gwt.user.client.ui.DockPanel;
- import com.google.gwt.user.client.ui.FlexTable;
- import com.google.gwt.user.client.ui.HTML;
- import com.google.gwt.user.client.ui.PasswordTextBox;
- import com.google.gwt.user.client.ui.RootPanel;
- import com.google.gwt.user.client.ui.TextBox;
- import com.google.gwt.user.client.ui.Widget;
- import com.javaeye.dengyin2000.truck.client.server.UserService;
- import com.javaeye.dengyin2000.truck.client.server.UserServiceAsync;
- /**
- * Entry point classes define
onModuleLoad()
. - */
- public class TruckApp implements EntryPoint {
- private TextBox userName;
- private PasswordTextBox password;
- private Button submit;
- private FlexTable loginTable;
- public static final HTML HEAD_TITLE = new HTML(");
- private UserServiceAsync userService;
- /**
- * This is the entry point method.
- */
- public void onModuleLoad() {
- DockPanel panel = new DockPanel();
- panel.setVerticalAlignment(DockPanel.ALIGN_MIDDLE);
- panel.setHorizontalAlignment(DockPanel.ALIGN_CENTER);
- submit = new Button("提交");
- submit.addClickListener(new ClickListener(){
- public void onClick(Widget sender) {
- // TODO check the user for validate
- getUserClientService().checkUser(userName.getText(), password.getText(), new AsyncCallback(){
- public void onFailure(Throwable caught) {
- Window.alert("Server error!");
- }
- public void onSuccess(Object result) {
- if (((Boolean)result).booleanValue() == true){
- Window.alert("Login successfully!");
- }else{
- Window.alert("Login failed!");
- }
- }
- });
- }
- });
- userName = new TextBox();
- password = new PasswordTextBox();
- loginTable = createLoginTable();
- panel.setWidth("100%");
- panel.setHeight("100%");
- loginTable.setWidget(2, 0, submit);
- panel.add(loginTable, DockPanel.CENTER);
- panel.add(HEAD_TITLE, DockPanel.NORTH);
- RootPanel.get().add(panel);
- }
- private FlexTable createLoginTable() {
- FlexTable table = new FlexTable();
- table.setText(0, 0, "用户名:");
- table.setWidget(0, 1, userName);
- table.setText(1, 0, "密 码:");
- table.setWidget(1, 1, password);
- table.setStyleName("ks-layouts-Label");
- return table;
- }
- private UserServiceAsync getUserClientService(){
- if (userService == null){
- userService = (UserServiceAsync) GWT.create(UserService.class);
- ServiceDefTarget endPoint = (ServiceDefTarget) userService;
- String moduleRelativeURL = "/userService.rpc";
- endPoint.setServiceEntryPoint(moduleRelativeURL);
- }
- return userService;
- }
- }
java 代码
- package com.javaeye.dengyin2000.truck.client.server;
- import com.google.gwt.user.client.rpc.RemoteService;
- public interface UserService extends RemoteService {
- boolean checkUser(String userName, String password);
- }
java 代码
- package com.javaeye.dengyin2000.truck.client.server;
- import com.google.gwt.user.client.rpc.AsyncCallback;
- public interface UserServiceAsync {
- void checkUser(String userName, String password, AsyncCallback callback);
- }
ok。到此配置结束。对了好像还忘了解释为什么要在web.xml中加上/static这段。原因是如果shell的url-pattern是/*的话,就会handler了gwt的RemoteServiceServlet的路径,这样的话remote rpc调用就总是Server error了。这时我们访问地址需要变成http://localhost:8888/static/com.javaeye.dengyin2000.truck.TruckApp/TruckApp.html 这样了。我提交了整个eclipse gwt项目所有代码但是把require lib去掉了。 需要的lib是spring-1.2.8.jar log4j-1.2.13.jar javassist-3.0.jar gwt-widgets-server-0.1.2.jar gwt-servlet.jar commons-logging-1.1.jar cglib-nodep-2.1_3.jar。请自行下载然后放到eclipse项目中的tomcat\webapps\ROOT\WEB-INF\lib\下面。你还需要把项目中引用到gwt lib的路径换成你自己机子上的。
:)
参考:http://g.georgovassilis.googlepages.com/usingthegwthandler
</bean></property> </bean></property> </bean></beans>