September 21, 2014
Hot Topics:
RSS RSS feed Download our iPhone app

Working with the WebWork Framework

  • August 29, 2005
  • By Vlad Kofman
  • Send Email »
  • More Articles »

In the project, I added a package called "webwork_test" and there I created an action class HelloAction that extends ActionSupport. ActionSupport is a parent class that all action classes must extend and implement the "execute" method. This action class will be invoked when the client initiates a request with the action name mapped in the configuration file (more on this later). The class simply has two data String variables—greeting and name—and getter and setter methods for them. In the execute method, greeting is set and name is checked because its value will come from the client's request.

package webwork_test;
import com.opensymphony.xwork.ActionSupport;
public class HelloAction
   extends ActionSupport {
   String greeting;
   String name;
   public String execute() throws Exception {
      greeting = "Aloha! ";
      if ( (name == null) || (name.length() == 0)) {
         return ERROR;
      }
      return SUCCESS;
   }
   public String getName() {
      return name;
   }
   public void setName(String name) {
      this.name = name;
   }
   public String getGreeting() {
      return greeting;
   }
}

In addition to the package, I created a Web archive (WAR) module "WebWorkWebModule" in JBuilder, using the Web module wizard under the File -> New menu.



Click here for a larger image.

In addition to the standard J2EE Web application configuration XML files, WebWork uses several config XML property files to coordinate requests, validate data, and manage data flow. The main ones are xwork.xml and valuators.xml. They need to be put in the servlet container root path for the framework to see them at runtime.

I placed these files in the project root directory on the hard drive and added them in JBuilder using properties of the Web module.

Right-click on the new Web module. Select the properties' open content section and use the Add Files button to add these files to the build process.



Click here for a larger image.

The xwork.xml defined my action and which class to use to perform it, and what to do in the event of success or failure.

<!DOCTYPE xwork PUBLIC "-//OpenSymphony Group//XWork 1.0//EN"
"http://www.opensymphony.com/xwork/xwork-1.0.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 stack. -->
         <default-interceptor-ref name="defaultStack" />
         <action name="helloAction"
                 class="webwork_test.HelloAction">
            <result name="error"
                    type="dispatcher">index.jsp</result>
            <result name="success"
                    type="dispatcher">success.jsp</result>
         </action>
      </package>
</xwork>

I took the valuators.xml from the WebWork distribution. Please see the attached project for its source.

To configure the main controller/dispatcher of the framework, I had to edit the standard web.xml file created with the Web module by JBuilder and located under the WEB-INF directory.

The final result looked like this:

Note: The main WebWork dispatcher servlet mapping and the reference to tag libraries and mapping of all actions URL pattern to the servlet URL.
<web-app>
   <display-name>My WebWork Application</display-name>
   <servlet>
      <servlet-name>webwork</servlet-name>
      <servlet-class>com.opensymphony.webwork.dispatcher.
                         ServletDispatcher</servlet-class>
      <load-on-startup>1</load-on-startup>
   </servlet>
   <servlet-mapping>
      <servlet-name>webwork</servlet-name>
      <url-pattern>*.action</url-pattern>
   </servlet-mapping>
   <taglib>
      <taglib-uri>webwork</taglib-uri>
      <taglib-location>
         /WEB-INF/lib/webwork-2.1.7.jar
      </taglib-location>
   </taglib>
</web-app>

After the module was created, I placed the WebWork tag libraries descriptor TLD file (that comes with the distribution) in the WEB-INF directory,

Now I was done with the configuration!

To test the framework in action, I created two JSP files: index.jsp and success.jsp. They both use the WebWork tag libraries to work with data presentation and flow.

Below is the source for the index jsp. I indicated that my action is helloAction in the form action parameter and created an input field with name "name". If you recall, the action class looks for this variable in its execute() method.





Page 2 of 3



Comment and Contribute

 


(Maximum characters: 1200). You have characters left.

 

 


Sitemap | Contact Us

Rocket Fuel