这篇文章是给具备一些基本的j2ee开发的兄弟们看的
1 Webwork 2 作为MVC
要让服务器支持webwork,首先需要对web服务器的web.xml进行配置,这里用/*,表示所有请求都作为webwork的action,也可以使用*.action, 这样只有后缀名为action的才执行。
web.xml  
<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>
配置好上面以后,webwork就可以运行了,我们添加两个action试试
xwork.xml
<action name="employee" class="net.reumann.demo.webwork.employee.EmployeeAction">
  <result name="list">/WEB-INF/jsp/employee/employeeList.jspx</result>
  <result name="input">/WEB-INF/jsp/employee/employeeForm.jspx</result>
  <result name="success" type="redirect">employee.action</result>
</action>
这里添加了一个action叫做employee,也就是说,所有请求http://site/employee 访问,都被看成触发一个action.其具体内容如下:
这是一个最简单的action,它起到了控制器的作用,实现了CRUD的基本功能,代码非常简介。几乎和PHP,ROR差不多
EmployeeAction.java
public class EmployeeAction extends ActionSupport implements Preparable {
public String execute() throws Exception {  //如果URL没有指定Action,默认调用它
  return list();  //转到list
}
public String list() {
  return "list";
}
public String add() {
  employee = new Employee();
  return Action.INPUT;
}
public String edit() {
  employee = employeeDao.getEmployee(id);
  return Action.INPUT;
}
public String save() {
  if (nullOrZero(employee.getEmployeeId())) {
    employeeDao.insert(employee);
  } else {
    employeeDao.update(employee);
  }
    return Action.SUCCESS;
}
public String delete() {
  employeeDao.delete(id);
  return Action.SUCCESS;
}
public void prepare() {
  departmentList = departmentDao.getAllDepartments();  //用于selectbox中下拉菜单
  employeeList = employeeDao.getAllEmployees();
}
}
webwork是基于MVC2的,因此上面的控制器对应下面两个模板文件。当然实际应用中不一定所有的界面都要采用MVC.
employeeList.jspx
<a href="employee!add.action">Add New Employee</a>
employeeForm.jspx
<w:if test="employee==null || employee.employeeId == null">
 <ww:set name="title" value="%{'Add new employee'}"/>
</w:if>
<w:else>
 <ww:set name="title" value="%{'Update employee'}"/>
</w:else>
<w:form action="employee!save.action" method="post">
    <w:textfield name="employee.firstName" 
        value="%{employee.firstName}" label="%{getText('label.firstName')}" size="40"/>
    <w:textfield name="employee.lastName" 
        value="%{employee.lastName}" label="%{getText('label.lastName')}" size="40"/>
    <w:textfield name="employee.age" 
        value="%{employee.age}" label="%{getText('label.age')}" size="20"/>
    <w:select name="employee.department.departmentId" 
        value="%{employee.department.departmentId}" 
        list="departments" listKey="departmentId" listValue="name"/>
    <w:hidden name="employee.employeeId" 
        value="%{employee.employeeId}"/>
    <w:submit name="action" value="%{getText('button.label.submit')}" cssClass="butStnd"/>
    <w:submit name="action" value="%{getText('button.label.cancel')}" cssClass="butStnd"/>
</w:form>
我们看到,这个表单非常简单,由于其名字的规范定义。webwork的action响应时,会自己装载其中的变量,和struts的formbean差不多。
EmployeeAction-validation.xml
<validators>
  <field name="employee.firstName">
     <field-validator type="requiredstring">
        <message key="errors.required.firstname"/>
      </field-validator>
  </field>
  <field name="employee.lastName">
     <field-validator type="requiredstring">
        <message key="errors.required.lastname"/>
      </field-validator>
  </field>
  <field name="employee.age">
     <field-validator type="required" short-circuit="true">
        <message key="errors.required.age"/>
      </field-validator>
      <field-validator type="int">
       <param name="min">18</param>
       <param name="max">65</param>
        <message key="errors.required.age.limit"/>
      </field-validator>
  </field>
</validators>
可以通过独立的文件来定义字段验证
EmployeeAction.properties
errors.required.age.limit=Please provide an age between ${min} and ${max}.
2 简化访问数据库
3 部分使用Spring连接系统原有资源,例如EJB
要使webwork支持spring,只要简单的下面的配置即可
web.xml
<context-param>
  <param-name>contextConfigLocation</param-name>
  <param-value>/WEB-INF/classes/net/reumann/demo/spring.xml</param-value>
</context-param>
<listener>
  <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
修改webwork的配置文件objectFactory=spring
webwork.properties
webwork.devMode=false
webwork.objectFactory=spring
然后定义好需要IoC的组件
spring.xml
<beans>
  <!-- DAO Configuration -->
  <bean id="employeeDao" class="net.reumann.demo.persistence.EmployeeNoDBdao" />
  <bean id="departmentDao" class="net.reumann.demo.persistence.DepartmentNoDBdao"/>
  <bean id="employeeAction" class="net.reumann.demo.webwork.employee.EmployeeAction">
    <constructor-arg ref="employeeDao" />
    <constructor-arg ref="departmentDao" />
  </bean>
</beans>
你就可以在action中自由的IoC了
action
public EmployeeAction(EmployeeDao employeeDao, 
    DepartmentDao departmentDao) {
  this.employeeDao = employeeDao;
  this.departmentDao = departmentDao;
}
页面结构设计
1 jsp include (部分使用taglib)
  <webwork:component template=”calendar.vm” label=”Birthdate”
   name=”birthday”>
    <webwork:param name=”showWeekends” value=”true”/>
  </webwork:component>
2 sitemesh
3 使用OA皮肤框架
4 ajax + dojo
业务层的设计
1 action中直接调用JDBC SQL/Proc, 即action + jdbc
2 action + service/session bean + dao + jdbc/ibatis/hibernate/entity bean (两个接口,两个实现)
public class DepartmentDaoService implements DepartmentService {
  DepartmentDao dao = new DepartmentDaoIbatisImpl();
}
3 没有service层,只有action + dao + jdbc/ibatis/hibernate/entity bean ,(如上面的EmployeeAction)
其它一些Struts的比较
Struts不是一个框架,而是一组项目
两个框架:
  Action: Action model
  Shale: Component model
  Action 和 Shale 会尽可能多的共享代码
各种子项目,例如Tiles
Struts 2将是Webwork 2.2和一些Struts的特性的结合
Class (re)Loading
1 Java 也可以像脚本语言那样支持 edit-refresh 风格的开发
2 Janino, Eclipse compiler 以及一些其他的编译器都使用 Commons-JCI is used
3 所有的库和框架都必须摆脱editcompile-package-deploy-wait-refresh的思想
Webwork 允许动态重新装载xml配置文件 (例如, 自动重新装载 actions.xml).
webwork.properties: webwork.configuration.xml.reload=true
EclipseWork 是Eclipse的WebWork插件 
http://eclipsework.sourceforge.net/
Trackback: http://tb.blog.csdn.net/TrackBack.aspx?PostId=1144287
 
                     
                    
                 
                    
                 
         
                
            
         浙公网安备 33010602011771号
浙公网安备 33010602011771号