webwork2分为webwork和xwork,通过webwork和xwork的解耦,我们的测试已经可以脱离HTTP协议了
比如我们有一个简单的用户注册功能,
Domain Object: User类
Business Object: UserDao类
Action :RegisterAction
java 代码
- package mypackage;
-
- public class User {
- private String username;
- private String password;
- private String age;
- private String tel;
- User(){
-
- }
- public String getAge() {
- return age;
- }
- public void setAge(String age) {
- this.age = age;
- }
- public String getPassword() {
- return password;
- }
- public void setPassword(String password) {
- this.password = password;
- }
- public String getTel() {
- return tel;
- }
- public void setTel(String tel) {
- this.tel = tel;
- }
- public String getUsername() {
- return username;
- }
- public void setUsername(String username) {
- this.username = username;
- }
- }
UserAction:
java 代码
- package mypackage;
-
- import com.opensymphony.xwork.Action;
- public class UserAction implements Action {
-
- private User user = new User();
-
- public String execute() throws Exception {
-
- System.out.println("begin execute!......");
- UserDao.saveUser(user);
- return SUCCESS;
- }
-
- public User getUser() {
-
- return user;
- }
- }
java 代码
- package mypackage;
- public class UserDao {
-
- public static void saveUser(User user) {
- System.out.println("save User");
- }
-
- }
如果是平时测试,可能需要打开web server来测试,但是由于webwork2采用xwork核心,它不与特定的面向HTTP的Servlet发生关联,所以我们测试的时候完全不需要打开web服务器,我们只需要提供一个map就可以达到测试的要求
java 代码
- package test;
-
- import java.util.HashMap;
- import java.util.Map;
- import junit.framework.TestCase;
- import com.opensymphony.xwork.ActionProxy;
- import com.opensymphony.xwork.ActionProxyFactory;
-
- public class TestWebWork extends TestCase {
-
- Map items;
-
- protected void setUp() throws Exception {
- super.setUp();
- items = new HashMap();
- items.put("user.username", "sanyun");
- items.put("user.password", "123456");
- }
-
- public void testWebwork() throws Exception {
- ActionProxyFactory factory = ActionProxyFactory.getFactory();
-
-
-
-
- ActionProxy proxy = factory.createActionProxy("default", "register",
- items, false, true);
-
- String result = proxy.execute();
- String uaername = (String) proxy.getInvocation().getStack().findValue(
- "user.username");
- String passwork = (String) proxy.getInvocation().getStack().findValue(
- "user.password");
-
- assertEquals("success", result);
- assertEquals("sanyun", uaername);
- assertEquals("123456", passwork);
- }
-
- protected void tearDown() throws Exception {
- super.tearDown();
- }
-
- }
这里没有看不到一个HttpServletRequest, HttpServletResponse,因为xwork不会和特定的Servlet打交道(也许是FTP,JMS,都有可能),webwork才会和具体的HttpServlet打交道 webwork通过com.opensymphony.webwork.dispatcher.FilterDispatcher把application,session,parameter,等等都放在一个HashMap中,以后在xwork中就只会操作map里面的数据了
配置文件:必须放在classpath的根目录下
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">
-
- <action name="register" class="mypackage.RegisterAction">
- <interceptor-ref name="params"/>
- <result name="success" >success.jsp</result>
-
- </action>
-
- </package>
- </xwork>
简单说明:package name就是 namespace,interceptor-ref表示拦截器,,webwork使用拦截器来实现控制反转.,比如 对于每个action都有可能需要获取请求页面的参数,记录日志,等等,这些任务并不和本身的业务逻辑沾边, 我们把这些系统级的任务称为基础设施,我们可以把这些基础设施抽象出来,如果我们的客户程序需要用到获取请求页面的功能,只需要在配置文挡里面配置就可以了,客户程序不需要显示的调用getParameter("username"),这就是为什么我们Action里面没有这些方法的原因了. 拦截器是通过动态代理来实现的.
最后需要配置web.xml,主要是把com.opensymphony.webwork.dispatcher.FilterDispatcher设置成前端分发器,处理所有的页面请求而已.
xml 代码
- <?xml version="1.0" encoding="ISO-8859-1"?>
- <!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>Acme Corp</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>
-
- </web-app>