GWT整合Struts2

Example

Setup Action

Start by creating an action that contains the method that is going to be called. This method needs to match the signature of the service interface, but you don't need to implement the interface on the action. This method will be different from regular Struts 2 action methods in that it returns any object (that GWT can serialize), instead of a result name. If you want to call regular actions from GWT (not using GWT's RPC) go here

In this example the action "Hello" will return the string passed to it. Remember, the returned object is not the regular Struts result, it is the object that you will receive on the client side.

Example:

public class Hello {
    public String hello(String text) {
        return text;
    }
}

Write the mapping for the action

For any action mapping that is going to be called from GWT:

  1. Add the map inside a package that extends "gwt-default"
  2. Add the "gwt" interceptor to the mapping

Example:

<?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>

  <constant name="struts.enable.DynamicMethodInvocation" value="false" />
  <constant name="struts.devMode" value="true" />

  <package name="example"  extends="gwt-default">
     <action name="Hello" class="example.Hello" method="hello">
        <interceptor-ref name="gwt"/>
     </action>
  </package>

</struts>
GWT classes

Write the required service interface as described here. This is regular GWT stuff.

As an example of the interface:

public interface MyService extends RemoteService {
    public String hello(String s);
}

public interface MyServiceAsync {
    public void hello(String s, AsyncCallback callback);
}

And to call it:

MyServiceAsync service = (MyServiceAsync) GWT.create(MyService.class);
AsyncCallback callback = new AsyncCallback() {
    public void onSuccess(Object result) {
        Window.alert(result.toString());
    }

    public void onFailure(Throwable caught) {
        Window.alert(caught.toString());
    }
};
ServiceDefTarget endpoint = (ServiceDefTarget) service;
endpoint.setServiceEntryPoint("Hello.action");
service.hello("Hi There!", callback);

Installation

This plugin can be installed by copying the plugin jar and gwt-servlet.jar (from GWT) into your application's /WEB-INF/lib directory. No other files need to be copied or created.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值