WebWork2教程(中文版)(2)

WebWork2教程(中文版)(2)

2、建立Web应用程序

1)这里假设你安装了Servlet容器,并知道如何创建一个Web应用程序;如果你不知道,建议学习Apache TomcatApache Jakarta项目中的一个免费Servlet容器);

2)复制需要的运行库(webwork-2.1.jarlib/core/*.jar)到Web应用程序的WEB-INF/lib文件夹中;

3)如下配置web.xmlxwork.xmlvalidators.xml文件;

4web.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>
       <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.jar</taglib-location>
       </taglib>
</web-app>

为了使用WebWork,必须注册ServletDispatcher,并映射到*.action;作为可选,如果要使用WebWorktags,要声明WebWorktaglib描述;

5)在WEB-INF/classes目录下创建WebWork的配置文件xwork.xml,下面是一个配置框架(会在教程的后面增加内容):

<!DOCTYPE xwork PUBLIC "-//OpenSymphony Group//XWork 1.0//EN" 
"http://www.opensymphony.com/xwork/xwork-1.0.dtd">
 
<xwork>
       <!-- Include webwork defaults (from WebWork-2.1 JAR). -->
       <include file="webwork-default.xml" />
       
       <!-- Configuration for the default package. -->
       <package name="default" extends="webwork-default">
       </package>
</xwork>

这做两件事:

l         导入webwork-default.xml(位于webwork-2.1.jar中)的配置信息,包含WebWork应用程序的缺省配置;

l         定义名为defaultpackage,用于actions resultsinterceptors的注册;这个package继承webwork-default,以继承其中定义的配置;

6WEB-INF/classes目录下创建validators.xml。包括以下内容:

<!DOCTYPE validators PUBLIC "-//OpenSymphony Group//XWork Validator
1.0//EN" "http://www.opensymphony.com/xwork/xwork-validator-1.0.dtd"> 
 
<validators> 
       <validator name="required"
              class="com.opensymphony.xwork.validator.validators.RequiredFieldValidator"/> 
       <validator name="requiredstring"
              class="com.opensymphony.xwork.validator.validators.RequiredStringValidator"/> 
       <validator name="int"
              class="com.opensymphony.xwork.validator.validators.IntRangeFieldValidator"/> 
       <validator name="date"
              class="com.opensymphony.xwork.validator.validators.DateRangeFieldValidator"/> 
       <validator name="expression"
              class="com.opensymphony.xwork.validator.validators.ExpressionValidator"/> 
       <validator name="fieldexpression"
              class="com.opensymphony.xwork.validator.validators.FieldExpressionValidator"/> 
       <validator name="email"
              class="com.opensymphony.xwork.validator.validators.EmailValidator"/> 
       <validator name="url"
              class="com.opensymphony.xwork.validator.validators.URLValidator"/> 
       <validator name="visitor"
              class="com.opensymphony.xwork.validator.validators.VisitorFieldValidator"/> 
       <validator name="conversion"
              class="com.opensymphony.xwork.validator.validators.ConversionErrorFieldValidator"/> 
</validators>

这个文件定义可以使用的有效Validators

你可能感兴趣的:(WebWork2教程(中文版)(2))