第二章: WebWork方式的HelloWorld
一. 准备skeleton
1. 所需文件:
a. WebWork发布包下: webwork-2.2.4.jar
b. WebWork发布包下: \lib\default\*.jar
2. web.xml配置文件:
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee
http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
<servlet>
<servlet-name>webwork</servlet-name>
<servlet-class>
com.opensymphony.webwork.dispatcher.ServletDispatcher
</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>webwork</servlet-name>
<url-pattern>*.action</url-pattern>
</servlet-mapping>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
<jsp-config>
<taglib>
<taglib-uri>webwork</taglib-uri>
<taglib-location>
/WEB-INF/lib/webwork-2.2.4.jar
</taglib-location>
</taglib>
</jsp-config>
</web-app>
3. xwork.xml配置文件:
<!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">
<default-action-ref name="home" />
</package>
</xwork>
4. webwork.properties配置文件:
webwork.tag.altSyntax=true
二. 第一个Action
1. action类:
package ch2.example1;
import com.opensymphony.xwork.Action;
public class HelloWorld implements Action {
private String message;
public String execute() throws Exception {
message = " Hello, World!\n";
message += "The time is:\n";
message += System.currentTimeMillis();
return SUCCESS;
}
public String getMessage() {
return message;
}
public void setMessage(String message) {
this.message = message;
}
}
2. JSP:
<%@taglib uri="webwork" prefix="ww"%>
<html>
<head>
<title>Hello Page</title>
</head>
<body>
<The message generated by my first action is:
<ww:property value="message"/>
</body>
</html>
3. xwork.xml
<!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">
<default-action-ref name="home" />
<action name="helloWorld" class="ch2.example1.HelloWorld">
<result name="success">index.jsp</result>
</action>
<!-- Add your actions here -->
</package>
</xwork>
4. 运行
在地址栏内输入: http://localhost:8080/helloWorld.action
三. 处理输入
1. 增加一个表单页面hello.jsp
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>Enter your name</title>
</head>
<body>
Please enter your name:
<form action="helloWorld.action">
<input type="textfield" name="name"/>
<input type="submit"/>
</form>
</body>
</html>
2. 在之前HelloWorld的Action中增加name名称属性以及对应setName()和getName()方法;
3. 运行
在地址栏内输入: http://localhost:8080/hello.jsp, 输入姓名后,在结果页面中可看到输入内容。
四. 高级控制流(增加表单验证以及验证出错提示)
1. action
public String execute() throws Exception {
if(name==null || "".equals(name) || "World".equals(name)) {
message = "Blank names or names of 'World' art not allowed!";
return INPUT;
}
message = " Hello, " + name + "!\n";
message += "The time is:\n";
message += System.currentTimeMillis();
return SUCCESS;
}
2. hello.jsp
<%@taglib uri="webwork" prefix="ww" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>Enter your name</title>
</head>
<body>
<ww:if test="message!=null">
<font color="red">
<ww:property value="message"/>
</font>
</ww:if>
Please enter your name:
<form action="helloWorld.action">
<input type="textfield" name="name"/>
<input type="submit"/>
</form>
</body>
</html>
五. 让WebWork完成任务
需求:1. 在出错的输入控制处理显示输入数据的错误信息;
2. 保证之前输入的值能够显示出来;
1. action
a. 继承ActionSupport
b. 改写execute()方法:
public String execute() throws Exception {
if(name==null || "".equals(name) || "World".equals(name)) {
addFieldError("name","Blank names or names of 'World' art not allowed!");
return INPUT;
}
message = " Hello, " + name + "!\n";
message += "The time is:\n";
message += System.currentTimeMillis();
return SUCCESS;
}
2. hello.jsp
<%@taglib uri="webwork" prefix="ww" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>Enter your name</title>
</head>
<body>
<ww:form action="helloWorld">
<ww:textfield label="Please enter your name" name="name"/>
<ww:submit/>
</ww:form>
</body>
</html>