新建 xwork.xml 文件,此文件为 webwork 的配置文件,必须放到 src 目录下
----------------------------------------------------------------------------------------------------
<!DOCTYPE xwork PUBLIC "-//OpenSymphony Group//XWork 1.0//EN" "http://www.opensymphony.com/xwork/xwork-1.0.dtd">
<xwork>
<include file="webwork-default.xml" />
<package name="default" extends="webwork-default">
<action name="login" class="com.test.LoginAction">
<!-- 这里的意思是登录成功后跳转到 /ok.jsp 页面 -->
<result name="success" type="dispatcher">
<param name="location">/ok.jsp</param>
</result>
<!-- 登录失败后则返回登录页 -->
<result name="error" type="dispatcher">
<param name="location">/login.jsp</param>
</result>
<!-- 此段代码为作用是将request请求的参数传递到 action 中 -->
<interceptor-ref name="params" />
</action>
<action name="register"
class="com.test.RegisterAction">
<result name="success" type="dispatcher">
<param name="location">/ok.jsp</param>
</result>
<interceptor-ref name="params" />
</action>
</package>
</xwork>
----------------------------------------------------------------------------------------------------
<action name="login" class="com.test.web.action.LoginAction"> 这一句为定义 action
执行 action 的路径为 /login.action
新建 jsp 文件
index.jsp 文件内容如下:
----------------------------------------------------------------------------------------------------
<html>
<head><title>INDEX</title></head>
<body>
<a href="login.jsp">Login</a><br>
<a href="register.jsp">Register</a>
</body>
</html>
----------------------------------------------------------------------------------------------------
login.jsp 文件内容如下:
----------------------------------------------------------------------------------------------------
<html>
<head><title>Login</title></head>
<body>
<table border=0 width=97%>
<tr><td align="left">
<form name="login" action="login.action" method="post">
Username:<input type="text" name="user.userName"><br>
Password:<input type="password" name="user.userPwd"><br>
<input type="submit" name="Submit"><br>
</form>
</td></tr>
</table>
</body>
</html>
----------------------------------------------------------------------------------------------------
register.jsp 文件内容如下:
----------------------------------------------------------------------------------------------------
<html>
<head><title>Register</title></head>
<body>
<table border=0 width=97%>
<tr><td align="left">
<form name="register" action="register.action" method="post">
Username:<input type="text" name="user.userName"><br>
Password:<input type="password" name="user.userPwd"><br>
<input type="submit" name="Submit"><br>
</form>
</td></tr>
</table>
</body>
</html>
----------------------------------------------------------------------------------------------------
ok.jsp 文件内容如下:
----------------------------------------------------------------------------------------------------
<%@ taglib prefix="ww" uri="/webwork"%>
<html>
<head>
<title>OK</title>
</head>
<body>
userName=<ww:property value="user.userName" />
<br>
userPwd=<ww:property value="user.userPwd" />
<br>
<a href="index.jsp">HOME</a>
</body>
</html>
----------------------------------------------------------------------------------------------------
<%@ taglib prefix="ww" uri="/webwork"%> 使用 webwork 的标签来获取值,注意红色字体,需要有斜杆
配置 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>
</web-app>
----------------------------------------------------------------------------------------------------
好了,进行第一段的测试
2、Model-Driven(模型驱动)
Model-Driven(模型驱动)的 Action 要求我们的 Action 实现 com.opensymphony.xwork.ModelDriven 接口,
它有一个方法:Object getModel(); 我们用这个方法返回我们的模型对象就可以了,也就是 User
修改 Action 加入 com.opensymphony.xwork.ModelDriven 接口,并创建 Object getModel() 方法
将表单中的 <input type="text" name="user.userName"> 改为 <input type="text" name="userName">
修改 xwork.xml 文件,增加 <interceptor-ref name="model-driven"/>
具体代码如下:
LoginAction.java 文件
----------------------------------------------------------------------------------------------------
package com.test;
import com.opensymphony.xwork.Action;
import com.opensymphony.xwork.ModelDriven;
public class LoginAction implements Action,ModelDriven {
// 必需进行初始化
private User user = new User();
public User getUser() {
return user;
}
public String execute() throws Exception {
// 可以在这里调用Service层来进行验证
if (user.getUserName().equals("oksonic") && user.getUserPwd().equals("oksonic"))
return this.SUCCESS;
else
return this.ERROR;
}
public Object getModel() {
return user;
}
}
----------------------------------------------------------------------------------------------------
RegisterAction.java 文件
----------------------------------------------------------------------------------------------------
package com.test;
import com.opensymphony.xwork.Action;
import com.opensymphony.xwork.ModelDriven;
public class RegisterAction implements Action,ModelDriven {
// 必需进行初始化
private User user = new User();
public User getUser() {
return user;
}
public String execute() throws Exception {
System.out.println(user.getUserName());
System.out.println(user.getUserPwd());
return this.SUCCESS;
}
public Object getModel() {
return user;
}
}
----------------------------------------------------------------------------------------------------
修改 xwork.xml 文件,此文件为 webwork 的配置文件,必须放到 src 目录下
----------------------------------------------------------------------------------------------------
<!DOCTYPE xwork PUBLIC "-//OpenSymphony Group//XWork 1.0//EN" "http://www.opensymphony.com/xwork/xwork-1.0.dtd">
<xwork>
<include file="webwork-default.xml" />
<package name="default" extends="webwork-default">
<action name="login" class="com.test.LoginAction">
<!-- 这里的意思是登录成功后跳转到 /ok.jsp 页面 -->
<result name="success" type="dispatcher">
<param name="location">/ok.jsp</param>
</result>
<!-- 登录失败后则返回提交页 -->
<result name="error" type="dispatcher">
<param name="location">/login.jsp</param>
</result>
<!-- 此段代码为作用是加入模型驱动 -->
<interceptor-ref name="model-driven"/>
<!-- 此段代码为作用是将request请求的参数传递到 action 中 -->
<interceptor-ref name="params" />
</action>
<action name="register"
class="com.test.RegisterAction">
<result name="success" type="dispatcher">
<param name="location">/ok.jsp</param>
</result>
<interceptor-ref name="model-driven"/>
<interceptor-ref name="params" />
</action>
</package>
</xwork>
----------------------------------------------------------------------------------------------------
注意:<interceptor-ref name="model-driven"/> 和 <interceptor-ref name="params" /> 的顺序一定要分先后
这时可以去掉 jsp 文件中 name 值中的对像名了,如:user.userName 可以改为 userName
login.jsp 文件内容如下:
----------------------------------------------------------------------------------------------------
<html>
<head><title>Login</title></head>
<body>
<table border=0 width=97%>
<tr><td align="left">
<form name="register" action="login.action" method="post">
Username:<input type="text" name="userName"><br>
Password:<input type="password" name="userPwd"><br>
<input type="submit" name="Submit"><br>
</form>
</td></tr>
</table>
</body>
</html>
----------------------------------------------------------------------------------------------------
register.jsp 文件内容如下:
----------------------------------------------------------------------------------------------------
<html>
<head><title>Register</title></head>
<body>
<table border=0 width=97%>
<tr><td align="left">
<form name="register" action="register.action" method="post">
Username:<input type="text" name="userName"><br>
Password:<input type="password" name="userPwd"><br>
<input type="submit" name="Submit"><br>
</form>
</td></tr>
</table>
</body>
</html>
----------------------------------------------------------------------------------------------------
ok.jsp 文件内容如下:
----------------------------------------------------------------------------------------------------
<%@ taglib prefix="ww" uri="/webwork"%>
<html>
<head>
<title>OK</title>
</head>
<body>
userName=<ww:property value="userName" />
<br>
userPwd=<ww:property value="userPwd" />
<br>
<a href="index.jsp">HOME</a>
</body>
</html>
3、使用 JUnit 进行单元测试
新建一个 JUnit Test Case (LoginActionTest),放到 test.com.test 包中,内容如下:
----------------------------------------------------------------------------------------------------
package test.com.test;
import java.util.HashMap;
import java.util.Map;
import com.opensymphony.xwork.ActionContext;
import com.opensymphony.xwork.ActionProxy;
import com.opensymphony.xwork.ActionProxyFactory;
import com.test.LoginAction;
import junit.framework.TestCase;
public class LoginActionTest extends TestCase {
public void testExecute() throws Exception {
Map params = new HashMap();
params.put("user.userName", "oksonic");
params.put("user.userPwd", "oksonic");
Map extraContext = new HashMap();
extraContext.put(ActionContext.PARAMETERS, params);
ActionProxy proxy = ActionProxyFactory.getFactory().createActionProxy(
"com.test", "login", extraContext);
proxy.setExecuteResult(false);
assertEquals(proxy.execute(), "success");
LoginAction action = (LoginAction) proxy.getAction();
assertEquals(action.getUser().getUserName(), "oksonic");
}
}
----------------------------------------------------------------------------------------------------
关于测试代码的详细解释请查看《WebWork教程》中的内容
新建一个 JUnit Test Case (RegisterActionTest),放到 test.com.test 包中,内容如下:
----------------------------------------------------------------------------------------------------
package test.com.test;
import java.util.HashMap;
import java.util.Map;
import com.opensymphony.xwork.ActionContext;
import com.opensymphony.xwork.ActionProxy;
import com.opensymphony.xwork.ActionProxyFactory;
import com.test.LoginAction;
import junit.framework.TestCase;
public class LoginActionTest extends TestCase {
public void testExecute() throws Exception {
Map params = new HashMap();
params.put("user.userName", "aaa");
params.put("user.userPwd", "bbb");
Map extraContext = new HashMap();
extraContext.put(ActionContext.PARAMETERS, params);
ActionProxy proxy = ActionProxyFactory.getFactory().createActionProxy(
"com.test", "register", extraContext);
proxy.setExecuteResult(false);
assertEquals(proxy.execute(), "success");
RegisterAction action = (RegisterAction) proxy.getAction();
assertEquals(action.getUser().getUserName(), "aaa");
}
}
----------------------------------------------------------------------------------------------------
新建一个 JUnit Test Suite ,这样就可以一次运行两个 test case 了,内容如下:
----------------------------------------------------------------------------------------------------
package test.com.test;
import junit.framework.Test;
import junit.framework.TestSuite;
public class AllTests {
public static Test suite() {
TestSuite suite = new TestSuite("Test for test.com.test");
//$JUnit-BEGIN$
suite.addTestSuite(LoginActionTest.class);
suite.addTestSuite(RegisterActionTest.class);
//$JUnit-END$
return suite;
}
}
----------------------------------------------------------------------------------------------------
直接进行测试(不需要启动服务),出现绿色条表示正常
4、配置表单验证
注册的验证类型:在 src 目录下新建 validators.xml 文件,文件内容如下:
----------------------------------------------------------------------------------------------------
<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"/>
<validator name="stringlength" class="com.opensymphony.xwork.validator.validators.StringLengthFieldValidator"/>
</validators>
----------------------------------------------------------------------------------------------------
要启动表单验证,需要增加一个拦截器,对 xwork.xml 文件进行修改,增加 <interceptor-ref name="validationWorkflowStack" />
代码如下:
xwork.xml
----------------------------------------------------------------------------------------------------
<!DOCTYPE xwork PUBLIC "-//OpenSymphony Group//XWork 1.0//EN" "http://www.opensymphony.com/xwork/xwork-1.0.dtd">
<xwork>
<include file="webwork-default.xml" />
<package name="default" extends="webwork-default">
<action name="login" class="com.test.LoginAction">
<!-- 这里的意思是登录成功后跳转到 /ok.jsp 页面 -->
<result name="success" type="dispatcher">
<param name="location">/ok.jsp</param>
</result>
<!-- 登录失败后则返回提交页 -->
<result name="error" type="dispatcher">
<param name="location">/login.jsp</param>
</result>
<!-- 如果验证失败,则返回登录页 -->
<result name="input" type="dispatcher">
<param name="location">/login.jsp</param>
</result>
<!-- 此段代码为作用是将request请求的参数传递到 action 中 -->
<interceptor-ref name="model-driven"/>
<interceptor-ref name="params" />
<interceptor-ref name="validationWorkflowStack"/>
</action>
<action name="register"
class="com.test.RegisterAction">
<result name="success" type="dispatcher">
<param name="location">/ok.jsp</param>
</result>
<!-- 如果验证失败,则返回登录页 -->
<result name="input" type="dispatcher">
<param name="location">/register.jsp</param>
</result>
<interceptor-ref name="model-driven"/>
<interceptor-ref name="params" />
<interceptor-ref name="validationWorkflowStack" />
</action>
</package>
</xwork>
----------------------------------------------------------------------------------------------------
修改 LoginAction、RegisterAction,使继承 ActionSupport 类
ActionSupport 类实现了数据校验错误信息、Action 运行错误信息的保存传递功
能。通过扩展 ActionSupport,LoginAction 即可携带执行过程中的状态信息,这
为之后的错误处理,以及面向用户的信息反馈提供了基础数据。
LoginAction.java 代码如下:
----------------------------------------------------------------------------------------------------
package com.test;
import com.opensymphony.xwork.Action;
import com.opensymphony.xwork.ActionSupport;
import com.opensymphony.xwork.ModelDriven;
public class LoginAction extends ActionSupport implements Action,ModelDriven {
// 必需进行初始化
private User user = new User();
public User getUser() {
return user;
}
public String execute() throws Exception {
// 可以在这里调用Service层来进行验证
if (user.getUserName().equals("oksonic") && user.getUserPwd().equals("oksonic"))
return this.SUCCESS;
else
return this.ERROR;
}
public Object getModel() {
return user;
}
}
----------------------------------------------------------------------------------------------------
RegisterAction.java 代码如下:
----------------------------------------------------------------------------------------------------
package com.test;
import com.opensymphony.xwork.Action;
import com.opensymphony.xwork.ActionSupport;
import com.opensymphony.xwork.ModelDriven;
public class RegisterAction extends ActionSupport implements Action,ModelDriven {
// 必需进行初始化
private User user = new User();
public User getUser() {
return user;
}
public String execute() throws Exception {
System.out.println(user.getUserName());
System.out.println(user.getUserPwd());
return this.SUCCESS;
}
public Object getModel() {
return user;
}
}
----------------------------------------------------------------------------------------------------
修改页面,为了能够显示错误消息,需要使用 webwork 的标签来实现页面,代码如下:
login.jsp
----------------------------------------------------------------------------------------------------
<%@ taglib prefix="ww" uri="/webwork"%>
<html>
<head><title>Register</title></head>
<body>
<table border=0 width=97%>
<tr><td align="left">
<ww:form name="test" action="login.action" method="POST">
<ww:textfield label="Username" name="userName" required="true"/>
<ww:password label="Password" name="userPwd" required="true"/>
<ww:submit value="Submit"/>
</ww:form>
</td></tr>
</table>
</body>
</html>
----------------------------------------------------------------------------------------------------
register.jsp
----------------------------------------------------------------------------------------------------
<%@ taglib prefix="ww" uri="/webwork"%>
<html>
<head><title>Login</title></head>
<body>
<table border=0 width=97%>
<tr><td align="left">
<ww:form name="test" action="register.action" method="POST">
<ww:textfield label="Username" name="userName" required="true"/>
<ww:password label="Password" name="userPwd" required="true"/>
<ww:submit value="Submit"/>
</ww:form>
</td></tr>
</table>
</body>
</html>
----------------------------------------------------------------------------------------------------
建立验证规则
对 LoginAction 进行验证规则的配置,需要新建一个配置文件,文件名以 “Action 的名称” + “-validation.xml”来命名,并保存到 LoginAction.java 文件所在的目录中
如:
LoginAction-validation.xml
代码如下:
LoginAction-validation.xml
----------------------------------------------------------------------------------------------------
<!DOCTYPE validators PUBLIC "-//OpenSymphony Group//XWork Validator 1.0//EN" "http://www.opensymphony.com/xwork/xwork-validator-1.0.dtd">
<validators>
<field name="userName">
<field-validator type="requiredstring">
<message>You must enter a value for username.</message>
</field-validator>
</field>
<field name="userPwd">
<field-validator type="requiredstring">
<message>You must enter a value for password.</message>
</field-validator>
</field>
</validators>
----------------------------------------------------------------------------------------------------
RegisterAction-validation.xml
----------------------------------------------------------------------------------------------------
<!DOCTYPE validators PUBLIC "-//OpenSymphony Group//XWork Validator 1.0//EN" "http://www.opensymphony.com/xwork/xwork-validator-1.0.dtd">
<validators>
<field name="userName">
<field-validator type="requiredstring">
<message>You must enter a value for username.</message>
</field-validator>
</field>
<field name="userPwd">
<field-validator type="requiredstring">
<message>You must enter a value for password.</message>
</field-validator>
</field>
</validators>
----------------------------------------------------------------------------------------------------
测试
5、国际化支持
Webwork 的国际化支持主要体现在两个部分:
1. UI-Tag
2. Validator
示例:读取 messages 资源中的 welcome 键值
<ww:i18n name="'messages'">
<ww:text name="'welcome'"/>
</ww:i18n>
在 src 目录下新建 messages_zh_CN.properties 文件,内容如下:
----------------------------------------------------------------------------------------------------
welcome=欢迎!{0}
----------------------------------------------------------------------------------------------------
这里的 “{0}”是为了加入参数而设定的,也可以再加入一个:welcome=欢迎!{0}{1}
修改 ok.jsp ,让它在登录后显示欢迎字样
ok.jsp
----------------------------------------------------------------------------------------------------
<%@ taglib prefix="ww" uri="/webwork"%>
<%@ page contentType="text/html;charset=UTF-8" language="java"%>
<html>
<head>
<title>OK</title>
</head>
<body>
<ww:i18n name="messages">
<ww:text name="welcome">
<ww:param>
<ww:property value="userName" />
</ww:param>
</ww:text>
</ww:i18n>
<br>
userPwd=<ww:property value="userPwd" />
<br>
</body>
</html>
----------------------------------------------------------------------------------------------------
验证部份使用的是对应 LoginAction.java 文件的一个指定的资源文件,和验证文件一样,
需要在 LoginAction.java 文件相同的目录下新建一个资源文件,文件名称为 LoginAction_zh_CN.properties
LoginAction_zh_CN.properties
----------------------------------------------------------------------------------------------------
error.username=用户名称必须输入!
error.password=密码必须输入!
----------------------------------------------------------------------------------------------------
RegisterAction_zh_CN.properties
----------------------------------------------------------------------------------------------------
error.username=用户名称必须输入!
error.password=密码必须输入!
----------------------------------------------------------------------------------------------------
对验证部份进行修改
LoginAction-validation.xml
----------------------------------------------------------------------------------------------------
<!DOCTYPE validators PUBLIC "-//OpenSymphony Group//XWork Validator 1.0//EN" "http://www.opensymphony.com/xwork/xwork-validator-1.0.dtd">
<validators>
<field name="userName">
<field-validator type="requiredstring">
<message key="error.username">You must enter a value for username.</message>
</field-validator>
</field>
<field name="userPwd">
<field-validator type="requiredstring">
<message key="error.password">You must enter a value for password.</message>
</field-validator>
</field>
</validators>
----------------------------------------------------------------------------------------------------
使用 key 来指定资源文件中的键值
RegisterAction-validation.xml
----------------------------------------------------------------------------------------------------
<!DOCTYPE validators PUBLIC "-//OpenSymphony Group//XWork Validator 1.0//EN" "http://www.opensymphony.com/xwork/xwork-validator-1.0.dtd">
<validators>
<field name="userName">
<field-validator type="requiredstring">
<message key="error.username">You must enter a value for username.</message>
</field-validator>
</field>
<field name="userPwd">
<field-validator type="requiredstring">
<message key="error.password">You must enter a value for password.</message>
</field-validator>
</field>
</validators>
----------------------------------------------------------------------------------------------------
修改 login.jsp 和 register.jsp 加入 <%@ page contentType="text/html;charset=UTF-8" language="java"%> 以支持显示中文
login.jsp
----------------------------------------------------------------------------------------------------
<%@ taglib prefix="ww" uri="/webwork"%>
<%@ page contentType="text/html;charset=UTF-8" language="java"%>
<html>
<head><title>Login</title></head>
<body>
<table border=0 width=97%>
<tr><td align="left">
<ww:form name="test" action="login.action" method="POST">
<ww:textfield label="Username" name="userName" required="true"/>
<ww:password label="Password" name="userPwd" required="true"/>
<ww:submit value="Submit"/>
</ww:form>
</td></tr>
</table>
</body>
</html>
----------------------------------------------------------------------------------------------------
register.jsp
----------------------------------------------------------------------------------------------------
<%@ taglib prefix="ww" uri="/webwork"%>
<%@ page contentType="text/html;charset=UTF-8" language="java"%>
<html>
<head><title>Register</title></head>
<body>
<table border=0 width=97%>
<tr><td align="left">
<ww:form name="test" action="register.action" method="POST">
<ww:textfield label="Username" name="userName" required="true"/>
<ww:password label="Password" name="userPwd" required="true"/>
<ww:submit value="Submit"/>
</ww:form>
</td></tr>
</table>
</body>
</html>
----------------------------------------------------------------------------------------------------
测试
6、集成 spring
因为现在使用的 webwork 版本为 2.2.2,所以在集成 spring 的方式上和 2.1.x 以前的版本有所不同。
1) 修改 web.xml,为 Web 应用增加相应的 Spring ContextLoaderListener
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>
<listener>
<listener-class>
org.springframework.web.context.ContextLoaderListener
</listener-class>
</listener>
</web-app>
----------------------------------------------------------------------------------------------------
2) 在 src 目录下新建 webwork.properties 文件,内容如下:
webwork.objectFactory = spring
这个文件非常重要,也是集成所必需的
3) 新增 Service 层的对像 IUserService、UserService
IUserService.java
----------------------------------------------------------------------------------------------------
package com.test.services;
import com.test.User;
public interface IUserService {
public boolean isValidUser(User user);
public boolean registerUser(User user);
}
----------------------------------------------------------------------------------------------------
UserService.java
----------------------------------------------------------------------------------------------------
package com.test.services;
import com.test.User;
public class UserService implements IUserService {
public boolean isValidUser(User user) {
if (user.getUserName().equals("oksonic") && user.getUserPwd().equals("oksonic"))
return true;
else
return false;
}
public boolean registerUser(User user) {
// 这里可以调用 DAO 层对像录入用户信息到数据库
return true;
}
}
----------------------------------------------------------------------------------------------------
4) 修改 xwork.xml
----------------------------------------------------------------------------------------------------
<!DOCTYPE xwork PUBLIC "-//OpenSymphony Group//XWork 1.0//EN" "http://www.opensymphony.com/xwork/xwork-1.0.dtd">
<xwork>
<include file="webwork-default.xml" />
<package name="default" extends="webwork-default">
<action name="login" class="login">
<!-- 这里的意思是登录成功后跳转到 /ok.jsp 页面 -->
<result name="success" type="dispatcher">
<param name="location">/ok.jsp</param>
</result>
<!-- 登录失败后则返回提交页 -->
<result name="error" type="dispatcher">
<param name="location">/login.jsp</param>
</result>
<!-- 如果验证失败,则返回登录页 -->
<result name="input" type="dispatcher">
<param name="location">/login.jsp</param>
</result>
<!-- 此段代码为作用是将request请求的参数传递到 action 中 -->
<interceptor-ref name="model-driven" />
<interceptor-ref name="params" />
<interceptor-ref name="validationWorkflowStack" />
</action>
<action name="register" class="register">
<result name="success" type="dispatcher">
<param name="location">/ok.jsp</param>
</result>
<!-- 如果验证失败,则返回登录页 -->
<result name="input" type="dispatcher">
<param name="location">/register.jsp</param>
</result>
<interceptor-ref name="model-driven" />
<interceptor-ref name="params" />
<interceptor-ref name="validationWorkflowStack" />
</action>
</package>
</xwork>
----------------------------------------------------------------------------------------------------
5) 添加 spring 框架到项目中
修改 applicationContext.xml 文件,加入 UserService 的配置信息
----------------------------------------------------------------------------------------------------
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd">
<beans>
<bean id="UserService" class="com.test.services.UserService" singleton="false"/>
<bean id="login" class="com.test.LoginAction" singleton="false" >
<property name="userService">
<ref bean="UserService"/>
</property>
</bean>
<bean id="register" class="com.test.RegisterAction" singleton="false" >
<property name="userService">
<ref bean="UserService"/>
</property>
</bean>
</beans>
----------------------------------------------------------------------------------------------------
注意:红色字体内容要和 xwork.xml 文件中的内容相对应
6) 修改 Action
LoginAction.java
----------------------------------------------------------------------------------------------------
package com.test;
import com.opensymphony.xwork.Action;
import com.opensymphony.xwork.ActionSupport;
import com.opensymphony.xwork.ModelDriven;
import com.test.services.IUserService;
public class LoginAction extends ActionSupport implements Action,ModelDriven {
// 必需进行初始化
private User user = new User();
private IUserService userService;
public User getUser() {
return user;
}
public String execute() throws Exception {
// 可以在这里调用Service层来进行验证
if (userService.isValidUser(user))
return this.SUCCESS;
else
return this.ERROR;
}
public Object getModel() {
// TODO Auto-generated method stub
return user;
}
public void setUserService(IUserService userService) {
this.userService = userService;
}
}
----------------------------------------------------------------------------------------------------
RegisterAction.java
----------------------------------------------------------------------------------------------------
package com.test;
import com.opensymphony.xwork.Action;
import com.opensymphony.xwork.ActionSupport;
import com.opensymphony.xwork.ModelDriven;
import com.test.services.IUserService;
public class RegisterAction extends ActionSupport implements Action,ModelDriven {
// 必需进行初始化
private User user = new User();
private IUserService userService;
public User getUser() {
return user;
}
public String execute() throws Exception {
userService.registerUser(user);
return this.SUCCESS;
}
public Object getModel() {
// TODO Auto-generated method stub
return user;
}
public void setUserService(IUserService userService) {
this.userService = userService;
}
}
----------------------------------------------------------------------------------------------------
好了,进行最后的测试!
为了制作更多更好的录像教程,希望大家多多提供相关资料!!!
联系方法:
http://www.oksonic.cn
Email: oksonic@tom.com
QQ: 71279650