The objective of this simple 'Hello World' tutorial is to be a gentle introduction of the components in xwork2.
It shows the required libraries and how to execute a command (an action) that simply prints out 'Hello World' the XWork 2 way.
Step 1: Prepare Project Libraries
You need the following two libraries:
- ognl-2.6.11.jar (or ognl.jar)
- xwork-2.1.2.jar (or xwork.jar)
Step 2: Write up xwork.xml
The xwork.xml configuration file, in this case named xwork-hello-world.xml, defines a simple package that contains one action and a simple result that prints the data to the console.
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE xwork PUBLIC
"-//OpenSymphony Group//XWork 1.1.1//EN"
"http://www.opensymphony.com/xwork/xwork-1.1.1.dtd">
<xwork>
<include file="xwork-default.xml" />
<package name="default-hello-world" extends="xwork-default" namespace="/helloWorld">
<result-types>
<result-type name="printToConsole" class="com.opensymphony.xwork2.showcase.PrintToConsoleResult" />
</result-types>
<action name="helloWorld" class="com.opensymphony.xwork2.showcase.helloworld.HelloWorldAction">
<result type="printToConsole">
<param name="param">${message}</param>
</result>
</action>
</package>
</xwork>
Step 3: Code it up
Next, we need to write the Java code that will initialize XWork and execute our action. The entry point into our application will be a class titled com.opensymphony.xwork2.showcase.helloworld.HelloWorldTutorial.
01. /*
* Copyright 2002-2006,2009 The Apache Software Foundation.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
05. package com.opensymphony.xwork2.showcase.helloworld;
06.
07. import java.util.LinkedHashMap;
08.
09. import com.opensymphony.xwork2.ActionProxy;
10. import com.opensymphony.xwork2.ActionProxyFactory;
11. import com.opensymphony.xwork2.config.Configuration;
12. import com.opensymphony.xwork2.config.ConfigurationManager;
13. import com.opensymphony.xwork2.config.providers.XmlConfigurationProvider;
14.
15. /**
16. *
17. * @author tm_jee
18. * @version $Date$ $Id$
19. */
20. public class HelloWorldTutorial {
21.
22. public static void main(String[] args) throws Exception {
23.
24. ConfigurationManager confManager = new ConfigurationManager();
25. confManager.addConfigurationProvider(
26. new XmlConfigurationProvider(
27. "com/opensymphony/xwork2/showcase/helloworld/xwork-hello-world.xml",
28. true));
29.
30. Configuration conf = confManager.getConfiguration();
31. ActionProxyFactory actionProxyFactory = conf.getContainer().getInstance(ActionProxyFactory.class);
32. ActionProxy actionProxy = actionProxyFactory.createActionProxy(
33. "/helloWorld", "helloWorld", new LinkedHashMap());
34.
35.
36. actionProxy.execute();
37. }
38. }
Lines 24-28 show the XWork framework being initialized with our configuration file. Lines 30-33 show how we obtain an ActionProxy object that handles the Action, Interceptors, and Result execution. Finally, in line 36, we execute XWork.
NOTE: Between line 24 and 25 should come the following : confManager.addConfigurationProvider(new XWorkConfigurationProvider());
Step 4: Create the Result and Action
The action:
01. /*
02. * Copyright (c) 2002-2006 by OpenSymphony
03. * All rights reserved.
04. */
05. package com.opensymphony.xwork2.showcase.helloworld;
06.
07. import org.apache.commons.logging.Log;
08. import org.apache.commons.logging.LogFactory;
09.
10. import com.opensymphony.xwork2.ActionSupport;
11.
12. /**
13. *
14. * @author tm_jee
15. * @version $Date$ $Id$
16. */
17. public class HelloWorldAction extends ActionSupport {
18.
19. private static final long serialVersionUID = 6874543345469426109L;
20.
21. private static final Log _log = LogFactory.getLog(HelloWorldAction.class);
22.
23. private String message;
24.
25. public String getMessage() { return message; }
26. public void setMessage(String message) { this.message = message; }
27.
28. @Override
29. public String execute() throws Exception {
30.
31. _log.debug("execute ...");
32.
33. message = "Hello World";
34.
35. return SUCCESS;
36. }
37. }
The result:
01. /*
02. * Copyright (c) 2002-2006 by OpenSymphony
03. * All rights reserved.
04. */
05. package com.opensymphony.xwork2.showcase;
06.
07. import org.apache.commons.logging.Log;
08. import org.apache.commons.logging.LogFactory;
09.
10. import com.opensymphony.xwork2.ActionInvocation;
11. import com.opensymphony.xwork2.Result;
12. import com.opensymphony.xwork2.util.TextParseUtil;
13.
14.
15. /**
16. *
17. * @author tm_jee
18. * @version $Date$ $Id$
19. */
20. public class PrintToConsoleResult implements Result {
21.
22. private static final Log _log = LogFactory.getLog(PrintToConsoleResult.class);
23.
24. private static final long serialVersionUID = -6173326554804520601L;
25.
26. private String param = "whatsoever";
27.
28. public void setParam(String param) { this.param = param; }
29. public String getParam() { return this.param; }
30.
31. public void execute(ActionInvocation invocation) throws Exception {
32.
33. _log.debug("execute ...");
34.
35. String result = TextParseUtil.translateVariables(param, invocation.getStack());
36.
37. System.out.println(result);
38. }
39. }
|