Apache Struts 2 Plugin Registry > Home > GWT Plugin > GWT Plugin Tutorial
Added by Ted Husted, last edited by Musachy Barroso on Feb 03, 2007  (view change)

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:

"Hello.java"

Write the mapping for your action.

In this example the mapping is in struts.xml:

struts.xml

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:

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

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 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):

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

See? It wasn't that bad.

Write your GWT entry point

Main.java

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

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