JavaRocks

Webwork Tutorial #1 - Hello World Program


This tutorial will show how to write a simple helloWorld program using webwork.

There are two configuration files required.
web.xml
xwork.xml

web.xml (WEB-INF/web.xml)
This file is a deplotment descriptor where you specify the webwork filter.

Xwork.xml (WEB-INF/classes/xwork.xml)
This file is required to configure actions. (To know more about actions visit http://www.opensymphony.com

Add this filter to your web.xml file

<?xml version="1.0"?>
<!DOCTYPE web-app PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
"http://java.sun.com/dtd/web-app_2_3.dtd">

<web-app>
  <display-name>My WebWork Application</display-name>
  
  <filter>
    <filter-name>webwork</filter-name>
    <filter-class>com.opensymphony.webwork.dispatcher.FilterDispatcher</filter-class>
  </filter>
 
  <filter-mapping>
    <filter-name>webwork</filter-name>
    <url-pattern>*.action</url-pattern>
  </filter-mapping>     
</web-app>


Now we will add our helloWorld action to xwork.xml

<!DOCTYPE xwork PUBLIC "-//OpenSymphony Group//XWork 1.1.1//EN" "http://www.opensymphony.com/xwork/xwork-1.1.1.dtd">
<xwork>
    <!-- Include webwork defaults (from WebWork JAR). -->
    <include file="webwork-default.xml" />

    <!-- Configuration for the default package. -->
    <package name="default" extends="webwork-default">
     <default-interceptor-ref name="basicStack"/>
   
      <action name="helloWorld" class="action.HelloWorldAction">
        <result name=”success”>helloWorld.html</result>
      </action>
</package>
</xwork>


In the above xwork.xml file, the tags other tan <action> are default configuration which is required. To be beginner don’t bother about that. When you are working with advanced application you can read about them.

So now we need to define the class HelloWorldAction which we have used above and helloWorld.html file. Below are the two.

HelloWorldAction.java

package action;
import com.opensymphony.xwork.ActionSupport;
public class HelloWorldAction extends ActionSupport{
    public String execute() {
        return SUCCESS;
}
}



helloWorld.html

<html>
     <body>
       <p>Hello World</p>
     </body>
<html>



This is all you have to do to write a simple HelloWorld WebApp. Run the application by starting your application server (Eg:Tomcat).

http://localhost:8080/MyWebapp/helloWorld.action