恶补J2EE Web 开发(讲稿大纲)

这篇文章是给具备一些基本的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/

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值