序言
WebWork 是一个流行的, 容易使用的 MVC 框架. 如果需要更多的关于WebWork项目的信息, 请访问 WebWork项目主页
.
此文档将有助于你开始使用WebWork, 帮助你运行例子和演示程序,即使你不是一个有经验的java web应用开发人员. 不过, 使用WebWork开发,你也需要懂得一些相关技术.在深入理解WebWork之前, 推荐您重新审视下列概念:
- Java
- Servlets, JSP,Tag Libraries
- JavaBeans
- HTML and HTTP
- Web 容器(例如Tomcat)
- XML
下载发行包和相关资源
下载Webwork发行包
你能从这里
下载WebWork的所有发行包. 它包含 webwork.jar文件和全部文档, 源代码, 所有 必须的和可选的依赖库文件,示例. 如果需要从WebWork源代码或CVS捡出重新编译WebWork包, 请查看构建WebWork.
联机信息和项目资源
在网络上有很多的关于Webwork项目的资源和信息,下列链接能帮助你找到这些:
Distribution Quickstart
概述
发行包包含下列目录结构:
docs目录包含当前版本的Javadoc, 您正在阅读的文档,标签库文档 以及Junit的构建报告.
在dist目录包含WebWork的不同jar封装文件:
- webwork-nostatic-<version>.jar: 仅仅包含WebWork的非静态内容.
- webwork-static-<version>.zip: 包含WebWork必须的静态内容.
lib目录包含WebWork必需的以及可选的依赖包, organized in subdirectories to represent different optional configurations:
注意:WebWork运行时可选包不是必须要的. 如果你希望使用某些特性如JasperReport或Java 5 (Tiger) 的注解支持,就必须包含所选特性的支持包.
Webwork也将全部的源文件和JSP标签模版打包在其中.
使用QuickStart运行示例
WebWork提供了一个快速启动应用示例的方法叫做QuickStart. QuickStart 从本质上讲是很少几种技术的组合, including the possibility to run web applications such as the provided examples out of the box with a stripped Jetty
container.
With that, running the demos is as easy as can be. You just need to call:
java -jar webwork.jar quickstart:<application-name>
from the distribution's top directory, whereby <application-name> is to be replaced by one of the subdirectory names under webapps/, representing the various supplied demo applications. So if you want to start the shopping cart example, just type on your command line:
java -jar webwork.jar quickstart:shopping-cart
After starting an example in that way, you will simply need to point your browser to http://localhost:8080/shopping-cart
to view the results.
The packaged examples are:
| blank |
Not really an example, but a blank web application template for creating your own application |
| portlet |
Demonstration of WebWork porlet integration (to be deployed in a Portal Server) - see Portlet Tutorial to read more about that |
| shopping-cart |
Nice example application demonstrating various aspects of WebWork |
| showcase |
Demonstration of all tag and AJAX features, along with other examples and some best practices |
| starter |
Basic web application, meant as an easy starting point for experimenting with WebWork's features - one could call it "playground" |
Read Quickstart documentation to learn more about how it works and how you can utilize it for your own applications.
See below for a more detailed description of how the blank application helps you to easily create your own WebWork-based applications.
Running Demos with Your Favorite Webcontainer
 | To Quickstart or not to Quickstart?
The previous section described the Quickstart mechanism for trying the supplied example webapps, but Quickstart is also a handy tool for development of your own WebWork based applications. We recommend you using the said mechanism unless you are familiar with standalone servlet containers and have good reasons to switch, such as preparing a production system etc. |
In order to deploy WebWork applications and demos to your favorite servlet container (also called web container) such as Apache Tomcat
or Caucho Resin
, you will need to build war files from within the webapps directory. You will find an ant build file there which will provide you with an easy way to accomplish that.
To build a webapp war archive, simply run:
where XXX is the name of the webapp you want to build. After the build is finished, the fully-built war file can be found in the dist directory. You may deploy this file to any servlet container.
For example:
ant build -Dwebapp=showcase
To deploy the built showcase.war to Tomcat, just place it into <TOMCAT_HOME>/webapps/ or use the Tomcat Manager Application to upload and deploy the war. After the war is deployed (and your server is started), point your browser to
http://SERVER:PORT/APPLICATIONNAME
, according to your setup and the application you deployed.
你的第一个WebWork应用
Using the Blank Template
As said before, the webapps directory contains a blank web application template. It is meant to be left untouched, and instead, you will want to make a copy of it as a starting point for your own new WebWork-based application. Although you can perform such a copy operation yourself, there is a much easier way around: You will find an build.xml in the webapps directory, which if called with ant new will provide you with a fresh empty webapp with a name you will be prompted for.
Setting up from Scratch
Structure of your Web Application
The following illustrates how your web application should be set up. Copy the webwork-(VERSION).jar, all the *.jar files in /lib/default and any necessary optional *.jar files in /lib/(optional configuration) to your webapp/lib directory. If you need to customize your own templates (how HTML is rendered from webwork UI tags), copy the /src/java/template directory into your webapp/ directory. Your webapp should look similar to this:
/mywebapp/
/mywebapp/template/
/mywebapp/META-INF/
/mywebapp/WEB-INF/
/mywebapp/WEB-INF/classes/
/mywebapp/WEB-INF/lib/
/mywebapp/WEB-INF/lib/CORE&OPTIONAL *.jar
/mywebapp/WEB-INF/web.xml
Minimum Set of Libraries and Config Files
The following files are a minium requirement for your application.
| Filename |
Description |
| webwork.jar |
WebWork library itself, found in distribution root directory |
| xwork.jar |
XWork library on which WebWork is built |
| oscore.jar |
OSCore, a general-utility library from OpenSymphony |
| ognl.jar |
Object Graph Navigation Language (OGNL), the expression language used
throughout WebWork |
| commons-logging.jar |
Commons logging, which WebWork uses to support transparently logging to
either Log4J or JDK 1.4+ |
| freemarker.jar |
All UI tag templates are written in Freemarker, which is also a good option for your views |
| rife-continuations by rife |
for the continuations feature |
| web.xml |
J2EE web application configuration file that defines the servlets, JSP tag
libraries, and so on for your web application |
| xwork.xml |
WebWork configuration file that defines the actions, results, and interceptors
for your application |
The library files (*.jar) needs to be copied to your /mywebapp/WEB-INF/lib/ directory. If you need optional functionalities requiring dependencies on optional jars, they need to be copied to this directory, too.
 | Spring as default IoC Container
Before WebWork 2.2, the builtin Inversion of Control (IoC) container was default. Since Spring has been found to do this job better, the container integrated in WebWork / XWork was deprecated. If you would like to follow our recommendation to use Spring as the default IoC container, please make sure to include all jars found in lib/spring in the WEB-INF/lib directory of your application. |
web.xml:
Create the following web.xml file in [webapp]/WEB-INF. If you already have a web.xml file, just add the content of the <web-app> tag below to your existing <web-app> tag.
<?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>
<!-- As of 2.2, Spring is the preferred IoC container rather than XWork,
so you'll have to include the spring jars if you want to use
Spring's IoC capabilities in WebWork. (Thanks Hani for commenting)
If you want to use deprecated integrated IoC container instead, you may
want to omit the following listener configuration.
-->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<!-- The following taglib directive would be needed if your servlet container would comply
to Servlet Spec <= 2.2
<taglib>
<taglib-uri>/webwork</taglib-uri>
<taglib-location>/WEB-INF/lib/webwork.jar</taglib-location>
</taglib>
-->
</web-app>
This registers FilterDispatcher to enable webwork functionality for your requests. The ContextLoaderListener will take care of setting up Spring as your IoC container for WebWork. The taglib entry will help you use Tags in your JSP pages.
Read more: web.xml
xwork.xml:
Create the following file xwork.xml in [webapp]/WEB-INF/classes/.
<!DOCTYPE xwork PUBLIC "-//OpenSymphony Group//XWork 1.1.1//EN"
"http://www.opensymphony.com/xwork/xwork-1.1.1.dtd">
<xwork>
<include file="webwork-default.xml" />
<package name="default" extends="webwork-default">
</package>
</xwork>
For now, this xwork.xml does only two things:
- It informs WebWork that it should import the configuration information from webwork-default.xml. (This file is located at the root of the webwork.jar, so it is sure to be found.)
- It defines a default package (with the <package> section) where WebWork elements like actions, results and interceptors are registered.
Read more: xwork.xml
Onward to the First lesson or return to the Webwork Start page