`
macrabbit
  • 浏览: 231929 次
  • 性别: Icon_minigender_1
  • 来自: 成都
社区版块
存档分类
最新评论

Struts 2 + GWT

    博客分类:
  • GWT
阅读更多

Struts 2 + GWT
Struts GWT Plugin is the recommended way to integrate Struts 2 and GWT.Use this tutorial if you cannot change your actions, or you just don't like the way (GWT way) that you have to implement them to be used with the plugin.
These tutorial will demonstrate how to call an Struts 2 action, using GWT to submit a form and use the returned data.
Write your action.
Example action:
"Hello.java"

package example; 
  
import com.opensymphony.xwork2.Action; 
  
public class Hello { 
    private String firstName; 
    private String lastName; 
  
    public String execute() { 
        return Action.SUCCESS; 
    } 
  
    public String getFirstName() { 
        return firstName; 
    } 
    public void setFirstName(String firstName) { 
        this.firstName = firstName; 
    } 
    public String getLastName() { 
        return lastName; 
    } 
    public void setLastName(String lastName) { 
        this.lastName = lastName; 
    } 
} 

 
Write the mapping for your action.
In this example the mapping is in struts.xml:
struts.xml

<?xml version="1.0" encoding="UTF-8" ?> 
<!DOCTYPE struts PUBLIC 
    "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN" 
    "http://struts.apache.org/dtds/struts-2.0.dtd"> 
  
<struts> 
  
  <package name="example"  extends="struts-default"> 
    <action name="Hello" class="example.Hello"> 
      <result type="json" /> 
    </action> 
  
    <action name="Main" class="com.opensymphony.xwork2.ActionSupport"> 
      <result>org.apache.struts.gwt.Main/Main.jsp</result> 
    </action> 
  </package> 
  
</struts> 

 The "Hello" action has a result of type "json", which will serialize the action object into a JSON string. If "firstName" is set to "John" and "lastName" is set to "Galt", the output of the "Hello" action will be:
{
   "firstName" : "John",
   "lastName"  : "Galt"
}
The "Main" action points to the "Main.jsp" page which is the page that uses GWT. The path of the result is "org.apache.struts.gwt.Main/Main.jsp". That means that the files generated by GWT must go under a folder named "org.apache.struts.gwt.Main" under the root of your application. See "Deployment structure" for more details.
Write "Main.jsp" page.
This is the page that is generated by GWT's "applicationCreator". It has been renamed to .jsp because we have modified it to be a jsp page, instead of a plain html page.
Main.jsp

 

<%@ taglib prefix="s" uri="/struts-tags" %> 
<html> 
  <head> 
    <s:head theme="ajax" debug="true"/> 
    <meta name='gwt:module' content='org.apache.struts.gwt.Main/org.apache.struts.gwt.Main'> 
  </head> 
  
  <body> 
    <script language="javascript" src="${pageContext.request.contextPath}/org.apache.struts.gwt.Main/gwt.js"/> 
  
      <form id="form1"> 
         <input type="text" name="firstName"> 
          <br/> 
          <input type="text" name="lastName"> 
          <span id="slot1"></span> 
       </form> 
  
       <br/> 
       <span id="slot2"></span> 
  </body> 
</html> 

  


We set head's tag attribute "theme" to ajax to use Dojo, don't panic, you won't have to use it directly. Note that we have changed a few things from the original html page generated by GWT, we set "content" to "org.apache.struts.gwt.Main/org.apache.struts.gwt.Main" because the GWT generated files will be under "AppRoot/org.apache.struts.gwt.Main" instead of beneath root, and we set "src" to "${pageContext.request.contextPath}/org.apache.struts.gwt.Main/gwt.js" for the same reason. Without these two changes the GWT files wouldn't be loaded.
Struts2GWTHelper
This class will take care of making the request. Why? Why do I need this class? Couple of reasons, first, there is a bug on bug on HTTPRequest.asyncPost which doesn't encode the payload properly, second, if you want to submit a form, you have to encode it yourself, and this class will help you do that. Optionally you can download a jar containing this class (with more methods) from here , add this to your GWT application file (i.e Main.gwt.xml):
<inherits name='struts2gwt.Struts2GWT'/>
and add the jar to the classpath in your compile script (i.e Main-compile.cmd) and the compile script (i.e Main-shell.cmd).
Struts2GWTHelper .java

 

package org.apache.struts.gwt.client; 
  
import com.google.gwt.user.client.ResponseTextHandler; 
  
public class Struts2GWTHelper { 
  
    /** 
     * Make asynchronous post 
     * @param url Action url 
     * @param formId id of form that will be posted 
     * @param handler callback function 
     */ 
    public static native void asyncPost(String url, String formId, ResponseTextHandler handler) /*-{ 
        dojo = $wnd.dojo; 
        //don't use the dojo.io.bind({...}) shortcut, it doesn't work here 
        var request = new dojo.io.Request(url); 
        request.load = function(type, data, request) { 
            handler.@com.google.gwt.user.client.ResponseTextHandler::onCompletion(Ljava/lang/String;)(data); 
        }; 
        request.formNode = dojo.byId(formId); 
        request.method = "POST"; 
        $wnd.dojo.io.bind(request); 
    }-*/; 
  
    /** 
     * Make asynchronous post 
     * @param url Action url 
     * @param handler callback function 
     */ 
    public static void asyncPost(String url, ResponseTextHandler handler) { 
        Struts2GWTHelper.asyncPost(url, handler); 
    } 
} 

  


See? It wasn't that bad.
Write your GWT entry point
Main.java

 

package org.apache.struts.gwt.client; 
  
import com.google.gwt.core.client.EntryPoint; 
import com.google.gwt.json.client.JSONObject; 
import com.google.gwt.json.client.JSONParser; 
import com.google.gwt.json.client.JSONString; 
import com.google.gwt.user.client.ResponseTextHandler; 
import com.google.gwt.user.client.ui.Button; 
import com.google.gwt.user.client.ui.ClickListener; 
import com.google.gwt.user.client.ui.Label; 
import com.google.gwt.user.client.ui.RootPanel; 
import com.google.gwt.user.client.ui.Widget; 
  
/** 
 * Entry point classes define <code>onModuleLoad()</code>. 
 */ 
public class Main implements EntryPoint { 
    final Label label = new Label(); 
  
    /** 
     * This is the entry point method. 
     */ 
    public void onModuleLoad() { 
        final Button button = new Button("Click me"); 
  
        button.addClickListener(new ClickListener() { 
            public void onClick(Widget sender) { 
                makeRequest(); 
            } 
        }); 
  
        RootPanel.get("slot1").add(button); 
        RootPanel.get("slot2").add(label); 
    } 
  
    private void makeRequest() { 
        Struts2GWTHelper.asyncPost("Hello.action", "form1", new ResponseTextHandler() { 
            public void onCompletion(String responseText) { 
                JSONObject obj = (JSONObject)JSONParser.parse(responseText); 
                JSONString firstName = (JSONString)obj.get("firstName"); 
                JSONString lastName = (JSONString)obj.get("lastName"); 
                label.setText("Welcome " + firstName.stringValue() + " " + lastName.stringValue()); 
            } 
        }); 
    } 
} 

 

The makeRequest() method will make a request to the "Hello" action that we created before, and will pass the fields of the form "form1" as parameters. Which will be populated on the action, and serialized back as JSON. Using JSONParser the JSON string is parsed into a JSON object. It is definitely not type safe as GWT Remoting, but it works.
Create Main.gwt.xml
Nothing new on this file, just as reference:
Main.gwt.xml

 

<module> 
    <!-- Inherit the core Web Toolkit stuff.                  --> 
    <inherits name='com.google.gwt.user.User'/> 
    <inherits name='com.google.gwt.json.JSON'/> 

    <!-- Specify the app entry point class.                   --> 
    <entry-point class='org.apache.struts.gwt.client.Main'/> 

</module> 

 Deployment structure


Folder                                                              Notes
AppRoot                                                          The application deployment folder
AppRoot/index.html                                         Welcome Page
AppRoot/org.apache.struts.gwt.Main              Content generated by GWT                                                                                                               (usually it gets generated into a folder named 'www')
AppRoot/WEB-INF                                            Regular webapp files and classes

分享到:
评论

相关推荐

    EJB3+Struts2+GWT Demo

    《EJB3+Struts2+GWT Demo:构建企业级电子商务应用的综合实践》 在现代企业级应用开发中,EJB(Enterprise JavaBeans)、Struts2 和 GWT(Google Web Toolkit)是三个关键的技术组件,它们共同构建了一个强大且灵活...

    GWT+EXT+STRUTS2+Eclipse完整范例.rar

    标题 "GWT+EXT+STRUTS2+Eclipse完整范例.rar" 提示我们这是一个包含一个集成开发环境Eclipse的项目,该项目集成了Google Web Toolkit (GWT), EXT JS 和Struts2框架。描述 "整合 GWT EXT STRUTS2 Eclipse 范例" 明确...

    Ext + Gwt + Struts2.0开发5

    例如,你可能需要在Struts2的配置文件中指定Action类如何调用GWT服务,以及如何处理返回的数据。 整合ExtJS到这种架构中,ExtJS可以用来构建富客户端界面,通过Ajax请求与GWT服务进行通信。ExtJS组件如Grid、Form等...

    Ext + Gwt + Struts2.0开发2

    ### 关于Ext + Gwt + Struts2.0的技术整合 在现代软件开发领域,将不同的框架和技术进行整合,能够创造出功能强大且高效的应用程序。本文将深入探讨如何结合Ext、Gwt(Google Web Toolkit)以及Struts2.0这三个框架...

    Ext + Gwt + Struts2.0开发3

    在Gwt端,使用GWT-RPC或RequestBuilder来发起HTTP请求到Struts2的Action。 6. **测试和优化**:完成基本集成后,测试所有功能,确保Gwt-Ext组件与后台Struts2.0 Action之间的通信无误。根据需求,可能需要进行性能...

    Ext + Gwt + Struts2.0开发4

    在本文中,我们将探讨如何使用Ext、Gwt和Struts2.0这三种技术结合进行Web应用开发。首先,理解每个技术的核心概念是至关重要的。 **Ext** 是一个JavaScript库,用于构建富客户端应用程序,提供了丰富的用户界面组件...

    浅论struts2与gwt

    Struts2和GWT是两种在Java开发领域中广泛使用的框架,它们分别专注于Web应用程序的MVC(模型-视图-控制器)架构和富客户端应用的构建。这篇博文的标题"浅论struts2与gwt"暗示了作者将探讨这两个技术的核心特性、应用...

    Ext + Gwt + Struts2.0开发1

    在整合这三种技术时,通常会利用GWT的强大客户端能力,通过Ext提供更丰富的用户界面,而Struts2作为服务器端的控制器,负责业务逻辑处理和与数据库的交互。GWT-Ext是将GWT与Ext.js结合的库,使得GWT开发者可以利用...

    struts2与gwt的整合

    Struts2和GWT(Google Web Toolkit)是两种在Java Web开发中广泛使用的框架。Struts2是一个基于MVC(Model-View-Controller)模式的开源框架,主要用于构建企业级的Web应用程序,而GWT则是一种用于开发富互联网应用...

    gwt+struts2 使用 struts2gwtplugin 例子

    Struts2和Google Web Toolkit(GWT)是两种在Java Web开发中广泛使用的框架。Struts2是一个MVC(Model-View-Controller)框架,它提供了强大的动作调度、拦截器和结果映射等功能,用于构建可维护性和扩展性良好的...

    gwtext无缝整合struts2

    研究gwt+ext+ssh框架整合编写的一个实例,实例中包括一个说明文档(google plugin创建gwt实例说明、gwt-ext实例的创建、gwt-ext与struts2整合实例说明);还包括文档对应的myeclipse项目。相关的包稍微有点大,没有...

    struts2最常用的几个类库

    9. **Struts2 Action and Result Types**:例如,`struts2-struts1-plugin.jar`允许Struts2与Struts1应用进行交互,而`struts2-gwt-plugin.jar`则提供了与Google Web Toolkit(GWT)的集成。 10. **Struts2 ...

    一个smart+struts2的例子

    标题中的“一个smart+struts2的例子”表明这是一个关于结合SmartGWT和Struts2框架进行Web应用开发的实际示例项目。SmartGWT是Google Web Toolkit (GWT)的一个扩展,提供了一系列高级UI组件和工具,使开发者能够构建...

    GWT_2BEXT_2BSTRUTS2完整实例

    1. **GWT-RPC与STRUTS2**:GWT应用通常使用GWT-RPC与服务器通信,但在STRUTS2中,可以使用Action和Interceptor来处理GWT-RPC请求,实现服务端的业务逻辑。 2. **JSON数据交换**:另一种常见的结合方式是通过JSON进行...

    struts与ext集成

    Struts2和EXT的集成是Java Web开发中的一个重要主题,特别是在构建用户界面时追求美观、交互性强的应用。这篇教程将深入探讨如何将这两者结合,为开发者提供一个更高效的开发环境。 **Struts2框架** Struts2是...

    Maven2 来完成Struts2.0项目的打包实例

    ### Maven2 完成 Struts2.0 项目打包实例详解 #### 一、Maven与Struts2.0简介 Maven是一个项目管理和理解工具,它提供了完整的生命周期管理,帮助开发者更容易地构建和管理Java项目。Struts2是Apache基金会下的一...

    gwt spring整合资源下载

    2. **gwt_spring.zip** - 类似于上面的文件,这可能是一个GWT与Spring集成的实例,但以ZIP格式提供,更适合Windows和Mac用户。它可能包括了项目的类文件、资源、配置文件等。 3. **springgwt_sudoku.zip** - 从名字...

    国外朋友的vaadin和struts2结合的例子

    Vaadin和Struts2是两种不同的Java Web开发框架,它们各自有着独特的特性和用途,但有时为了实现更复杂的业务逻辑或者充分利用两者的优势,开发者会选择将它们结合使用。在这个"国外朋友的vaadin和struts2结合的例子...

    gwt 服务器端(内含配置教程)

    2. **创建GWT项目**:安装插件后,你可以创建一个新的GWT项目。在Eclipse中选择“File” -&gt; “New” -&gt; “Google” -&gt; “Web Application Project”。在向导中填写项目名称,选择GWT版本,并设置其他必要的配置。 3...

    静态资源、struts2、Hibernate、Spring、js核心jar包

    这里提到的“静态资源、Struts2、Hibernate、Spring、js核心jar包”涵盖了Web开发中的几个关键组件。让我们逐一深入探讨它们的重要性及如何在项目中运用。 1. 静态资源: 静态资源主要包括HTML、CSS、JavaScript...

Global site tag (gtag.js) - Google Analytics