Writing WebWork HelloWorld App

From Foochal

Jump to: navigation, search

Contents

Getting the software

  1. Download the webwork release from the OpenSymphony website.
  2. Unzip the zip file in a folder, for example ‘webwork’.

Create a project

  1. Create a new Eclipse dynamic web project. We’ll chose the name ‘wwdemo’ for the rest of this article.
  2. Copy the webwork/src/java/template directory to your wwdemo/WebContent directory

Setting up the project

web.xml

Copy the following to your web.xml

<?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>/*</url-pattern>
 </filter-mapping>
 <listener>         
  <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
 </listener>
 <taglib>
  <taglib-uri>/webwork</taglib-uri>
   <taglib-location>/WEB-INF/lib/webwork.jar</taglib-location>
 </taglib>
</web-app>

xwork.xml

Create the following xwork.xml in your WebContent/WEB-INF/classes directory:

 <!DOCTYPE xwork PUBLIC "-//OpenSymphony Group//XWork 1.0//EN"    
  "http://www.opensymphony.com/xwork/xwork-1.0.dtd">  
  <xwork>
   <include file="webwork-default.xml"/>
   <package name="default" extends="webwork-default">
    <default-interceptor-ref name="completeStack"/>
    <action name="helloWorld" class="example.helloworld.HelloWorld">
     <result name="success">hello.jsp</result>
    </action>
   </package>  
  </xwork>

Required Libraries

Copy the following jars:

  1. xwork.jar
  2. webwork.jar

HelloWorld.java

Create an action file HelloWorld.java

package demo;
import com.opensymphony.xwork.Action;
import java.text.DateFormat;
import java.util.*;
public class HelloWorld implements Action {
 private String message; 
 public String execute() { 
  message = "Hello, WebWorld!\n";
  message += "The time is:\n";
  message += DateFormat.getDateInstance().format(new Date());
  return SUCCESS; 
 } 
 public String getMessage() {
  return message; 
 }
}

hello.jsp

Create a view hello.jsp

<%@ taglib prefix="ww" uri="/webwork" %>
<html>
 <head>
  <title>Hello Page</title>
 </head>
 <body>
  The message generated by my first action is: <ww:property value="message"/>
 </body>
</html>

Spring config

Create the file applicationContext.xml and place it in WEB-INF. This is for initializing spring beans:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN"  
 "http://www.springframework.org/dtd/spring-beans.dtd">
<beans default-autowire="autodetect"></beans>

WebWork config

Create a file called webwork.properties and place it in classes directory:

webwork.objectFactory = spring
webwork.devMode = true

Run the program

Goto: http://localhost:8080/wwdemo/helloWorld.action

External links

The relevant documentation is at http://www.opensymphony.com/webwork/wikidocs/Tutorial.html


Personal tools