This tutorial will demonstrate how to call an Struts 2 action, using GWT to submit a form and use the returned data, without using the GWT plugin. The recommended way of integrating GWT with Struts 2 is using the GWT Plugin
To use this tutorial you will need the Struts 2 JSON Plugin installed.
Write your action.
Example action:
...
Code Block | ||||||
---|---|---|---|---|---|---|
| ||||||
<?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="strutsjson-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:
...
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, and add this to your GWT application file (i.e Main.gwt.xml):
...