THIS IS A TEST INSTANCE. ALL YOUR CHANGES WILL BE LOST!!!!
...
Example action:
Code Block | ||||||
---|---|---|---|---|---|---|
| ||||||
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; } } |
...
In this example the mapping is in struts.xml:
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="json-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> |
...
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.
Code Block | ||||||
---|---|---|---|---|---|---|
| ||||||
<%@ 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> |
...
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).
Code Block | ||||||
---|---|---|---|---|---|---|
| ||||||
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); } } |
...
Write your GWT entry point
Code Block | ||||||
---|---|---|---|---|---|---|
| ||||||
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()); } }); } } |
...
Nothing new on this file, just as reference:
Code Block | ||||||
---|---|---|---|---|---|---|
| ||||||
<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> |
...