- dengyin2000
- 等级:
- 性别:
- 文章: 1467
- 积分: 1725
- 来自: 广州
|
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
- xml version="1.0" encoding="UTF-8"?>
- <web-app>
-
- <servlet>
- <servlet-name>GWTSpring<!---->servlet-name>
- <servlet-class>
- org.springframework.web.servlet.DispatcherServlet
- <!---->servlet-class>
- <load-on-startup>1<!---->load-on-startup>
- <!---->servlet>
-
- <servlet-mapping>
- <servlet-name>GWTSpring<!---->servlet-name>
- <url-pattern>*.rpc<!---->url-pattern>
- <!---->servlet-mapping>
- <servlet>
- <servlet-name>shell<!---->servlet-name>
- <servlet-class>com.google.gwt.dev.shell.GWTShellServlet<!---->servlet-class>
- <!---->servlet>
-
- <servlet-mapping>
- <servlet-name>shell<!---->servlet-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>requestSetter<!---->value>
- <!---->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;
-
-
-
-
-
-
-
-
-
-
-
- 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();
- }
-
-
-
-
-
-
-
-
- public void setRequestSetterName(String setterName) {
- this.requestSetterName = 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;
-
-
-
-
- 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;
-
-
-
-
- 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) {
-
- 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>
- 大小: 13.4 KB
声明:ITeye文章版权属于作者,受法律保护。没有作者书面许可不得转载。
|
返回顶楼 |
|
|
- dengyin2000
- 等级:
- 性别:
- 文章: 1467
- 积分: 1725
- 来自: 广州
|
xml格式不正确,浪费了很多时间呀。本以为java格式是正确的,想用java格式。可是出来更吓人。
|
返回顶楼 |
|
|
- dengyin2000
- 等级:
- 性别:
- 文章: 1467
- 积分: 1725
- 来自: 广州
|
调整格式花了一个小时,基本上每种格式都试过了。 还是不行。请看附件里面的文件吧。java代码的格式没有问题。
|
返回顶楼 |
|
|
- dengyin2000
- 等级:
- 性别:
- 文章: 1467
- 积分: 1725
- 来自: 广州
|
dengyin2000 写道 <?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd">
<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>requestSetter</value>
</list>
</property>
</bean>
<bean id="requestSetter"
class="org.gwtwidgets.server.spring.RequestInjection">
<property name="requestSetterName" value="setRequest" />
<property name="responseSetterName" value="setResponse" />
</bean>
</beans>
|
返回顶楼 |
|
|
- adablue77
- 等级:
- 文章: 43
- 积分: 126
|
可惜还是初级会员不能投票,呵呵
早点看到这篇文章就好了,我是参考GwtPetstore那个项目,我水平太低,分析了好久才模仿成功的.
兄台有没有关于Gwt和报表结合使用相关的文章?
|
返回顶楼 |
|
|
- dengyin2000
- 等级:
- 性别:
- 文章: 1467
- 积分: 1725
- 来自: 广州
|
报表是server端的事情吧。
|
返回顶楼 |
|
|
- dearwolf
- 等级:
- 性别:
- 文章: 621
- 积分: 729
- 来自: 北京
|
xml格式的编辑器有bug,你第一次粘贴进去以后,以后再改文章的时候,基本上都要重新粘贴一边才行
|
返回顶楼 |
|
|
- dengyin2000
- 等级:
- 性别:
- 文章: 1467
- 积分: 1725
- 来自: 广州
|
重新粘贴还是有问题。 在写的时候没有问题, 提交后就乱了。 回家后, 我改成bbcode排版吧。 听说bbcode没有问题。
|
返回顶楼 |
|
|
- dearwolf
- 等级:
- 性别:
- 文章: 621
- 积分: 729
- 来自: 北京
|
bbcode排版我还没学会....但是我重新粘帖以后就没问题了的说,你可以看看我这个帖子:
http://www.iteye.com/topic/58265
|
返回顶楼 |
|
|
- dengyin2000
- 等级:
- 性别:
- 文章: 1467
- 积分: 1725
- 来自: 广州
|
还是没有搞定, 可以用了feditor后不能用bbcode了
|
返回顶楼 |
|
|