Struts 2 > Lesson 1 - Setting up webwork in a web application |
For this lesson, you need to have a Servlet container set up and know how to create a web application. If you don't, we suggest you learn about Apache Tomcat, which is a free Servlet container from the Apache Jakarta Project, or Resin, from Caucho Technology
, which is free for noncommercial use.
![]() | Notation Throughout these lessons, we'll assume that your web application root is the directory [webapp], and that your Java source files are kept in [src]. |
To install WebWork in a web application:
[the tomcat root directory] \|_webapps \|_webwork-lessons \|_WEB-INF \|_classes \|_lib
![]() | WebWork jar name If you have a later version of WebWork than 2.1.7, the WebWork jar will not be named webwork-2.1.7.jar. Be sure to replace all occurrences of this jar's name below with the name of the jar you are using. |
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> <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>
This registers ServletDispatcher as a servlet, and maps it to the suffix *.action. We will go into this more in the section on Actions in the next lesson. WebWork's taglib descriptor allows WebWork tags to be used (see lesson 4.1
).
Read more: web.xml
Create the following file xwork.xml in [webapp]/WEB-INF/classes/.
<!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"> </package> </xwork>
For now, this xwork.xml does only two things:
Read more: struts.xml
Create a file validators.xml in [webapp]/WEB-INF/classes/ with the following content:
<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>
This file defines the validators used, for example, for validating html form fields.
Read more: Validation
Restart your servlet container (for example, restart Tomcat), and your webapp should be ready for use as a skeleton WebWork application.
To test whether everything is working, create [webapp]/test.jsp:
<html>
<body>
Hello html world
<hr/>
<%
out.println("Hello jsp world.");
%>
</body>
</html>
If you can load this file in your browser and see the two Hello messages, your web application is working.