1.开发前台展示页面*.jsp程序。
(1)增加预订房间页面文件add.jsp;
(2)房间预订列表页面文件list.jsp;
(3)更新预定房间的页面文件update.jsp;
(4)查看预定信息页面文件result.jsp。
add.jsp文件:
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%@ taglib uri="/struts-tags" prefix="s"%>
<%
String path = request.getContextPath();
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>增加房间</title>
</head>
<body>
<%@include file="/info/adminInfo.jsp"%>
<h1>
<font color="red">增加房间</font>
</h1>
<s:form action="saveRoom">
<s:textfield name="room.name" label="房间名称"></s:textfield>
<s:textfield name="room.price" label="房间价格"></s:textfield>
<s:textfield name="room.category" label="房间类型"></s:textfield>
<tr>
<td class="tdLabel">
房间状态:
</td>
<td>
<select name="room.status">
<option value="0" selected="selected">
空闲
</option>
<option value="1">
已入住
</option>
</select>
</td>
</tr>
<s:submit value="增 加"></s:submit>
</s:form>
</body>
</html>
list.jsp文件:
<%@ page language="java" import="java.util.*" pageEncoding="GB18030"%>
<%@ taglib uri="/struts-tags" prefix="s"%>
<%
String path = request.getContextPath();
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>房间列表</title>
<script type="text/javascript">
function del() {
if (confirm("确定删除房间信息吗?")) {
return true;
}
return false;
}
</script>
</head>
<body>
<%@include file="/info/adminInfo.jsp"%>
<h1>
<font color="red"><center>
房间列表
</center> </font>
</h1>
<s:a href="/hotel/room/add.jsp">增加房间</s:a>
<table border="1" width="80%" align="center">
<tr>
<td>
房间序号
</td>
<td>
房间名称
</td>
<td>
房间价格
</td>
<td>
房间类型
</td>
<td>
房间状态
</td>
<td>
删除
</td>
<td>
更新
</td>
</tr>
<s:iterator value="#request.list" id="room">
<tr>
<td>
<s:property value="#room.roomid" />
</td>
<td>
<s:property value="#room.name" />
</td>
<td>
<s:property value="#room.price" />
</td>
<td>
<s:property value="#room.category" />
</td>
<td>
<s:if test="#room.status == 0">空闲</s:if>
<s:else>已入住</s:else>
</td>
<td>
<s:a href="deleteRoom.action?room.roomid=%{#room.roomid}"
onclick="return del();">delete</s:a>
</td>
<td>
<s:a href="updatePRoom.action?room.roomid=%{#room.roomid}">update</s:a>
</td>
</tr>
</s:iterator>
</table>
</body>
</html>
update.jsp文件:
<%@ page language="java" import="java.util.*" pageEncoding="GB18030"%>
<%@ taglib uri="/struts-tags" prefix="s"%>
<%
String path = request.getContextPath();
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>修改房间信息</title>
</head>
<body>
<%@include file="/info/adminInfo.jsp"%>
<h1>
<font color="red"><center>
修改房间
</center> </font>
</h1>
<s:form action="updateRoom">
<table>
<tr>
<td>
<s:hidden name="room.roomid" value="%{room.roomid}"></s:hidden>
</td>
</tr>
<tr>
<td>
<s:textfield name="room.name" value="%{room.name}" label="房间名称"
readonly="true"></s:textfield>
</td>
</tr>
<tr>
<td>
<s:textfield name="room.price" value="%{room.price}"
label="房间价格"></s:textfield>
</td>
</tr>
<tr>
<td>
<s:textfield name="room.category" value="%{room.category}"
label="房间类型"></s:textfield>
</td>
</tr>
<tr>
<td>
<tr>
<td class="tdLabel">
房间状态:
</td>
<s:if test="room.status == 0">
<td>
<select name="room.status">
<option value="0" selected="selected">
空房
</option>
<option value="1">
有客人
</option>
</select>
</td>
</s:if>
<s:else>
<td>
<select name="room.status">
<option value="0">
空房
</option>
<option value="1" selected="selected">
有客人
</option>
</select>
</td>
</s:else>
</tr>
</td>
</tr>
<tr>
<td>
<s:submit value="修改"></s:submit>
</td>
</tr>
</table>
</s:form>
</body>
</html>
管理员管理功能的表现层JSP页面:
(1)添加管理员的页面文件add.jsp;
(2)管理员列表的页面文件list.jsp;
(3)更新管理员的页面文件update.jsp。
add.jsp文件:
<%@ page language="java" import="java.util.*" pageEncoding="GB18030"%>
<%@ taglib uri="/struts-tags" prefix="s"%>
<%
String path = request.getContextPath();
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>添加管理员</title>
</head>
<body>
<%@include file="/info/adminInfo.jsp"%>
<h1>
<font color="red">添加管理员</font>
</h1>
<s:form action="saveAdmin">
<s:textfield name="admin.username" label="用户名"></s:textfield>
<s:password name="admin.password" label="密码"></s:password>
<s:submit value="添 加"></s:submit>
</s:form>
</body>
</html>
list.jsp文件:
<%@ page language="java" import="java.util.*" pageEncoding="GB18030"%>
<%@ taglib uri="/struts-tags" prefix="s"%>
<%
String path = request.getContextPath();
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>用户列表</title>
<script type="text/javascript">
function del() {
if (confirm("你确定要删除该用户吗?")) {
return true;
}
return false;
}
</script>
</head>
<body>
<%@include file="/info/adminInfo.jsp"%>
<h1>
<font color="red"><center>
用户列表
</center> </font>
</h1>
<table border="1" width="80%" align="center">
<tr>
<td>
序号
</td>
<td>
姓名
</td>
<td>
电话
</td>
<td>
邮箱
</td>
<td>
删除
</td>
<td>
更新
</td>
</tr>
<s:iterator value="#request.list" id="us">
<tr>
<td>
<s:property value="#us.userid" />
</td>
<td>
<s:property value="#us.username" />
</td>
<td>
<s:property value="#us.mobile" />
</td>
<td>
<s:property value="#us.email" />
</td>
<td>
<s:a href="deleteUser.action?user.userid=%{#us.userid}"
onclick="return del();">delete</s:a>
</td>
<td>
<s:a href="updatePUser.action?user.userid=%{#us.userid}">update</s:a>
</td>
</tr>
</s:iterator>
</table>
</body>
</html>
update.jsp文件:
<%@ page language="java" import="java.util.*" pageEncoding="GB18030"%>
<%@ taglib uri="/struts-tags" prefix="s"%>
<%
String path = request.getContextPath();
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>修改用户资料</title>
</head>
<body>
<%@include file="/info/adminInfo.jsp"%>
<h1>
<font color="red"><center>
修改用户
</center> </font>
</h1>
<s:form action="updateUser">
<table>
<tr>
<td>
<s:hidden name="user.userid" value="%{user.userid}"></s:hidden>
</td>
</tr>
<tr>
<td>
<s:textfield name="user.username" value="%{user.username}"
label="用户名"></s:textfield>
</td>
</tr>
<tr>
<td>
<!--<s:password name="user.password" value="%{user.password}" label="密码"></s:password>-->
<s:textfield name="user.password" value="%{user.password}"
label="密码"></s:textfield>
</td>
</tr>
<tr>
<td>
<s:textfield name="user.mobile" value="%{user.mobile}"
label="手机号码"></s:textfield>
</td>
</tr>
<tr>
<td>
<s:textfield name="user.email" value="%{user.email}" label="邮箱地址"></s:textfield>
</td>
</tr>
<tr>
<td>
<s:submit value="修改"></s:submit>
</td>
</tr>
</table>
</s:form>
</body>
</html>
2.后台业务控制器Action的开发
实现房间预订功能需要3个Action:
(1)预定房间列表文件ListOrderAction.java;
(2)预定房间服务列表OrderServiceAction.java;
(3)保存预定信息的文件SaveOrderAction.java.
ListOrderAction.java文件:
package com.integration.action.order;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import com.integration.entity.Room;
import com.integration.entity.Order;
import com.integration.service.RoomService;
import com.integration.service.OrderService;
@SuppressWarnings("serial")
public class ListOrderAction extends ActionSupport {
private Order order;
private Room room;
private OrderService orderService;
private RoomService roomService;
private String message;
public String getMessage() {
return message;
}
public void setMessage(String message) {
this.message = message;
}
public Room getRoom() {
return room;
}
public void setRoom(Room room) {
this.room = room;
}
public RoomService getRoomService() {
return roomService;
}
public void setRoomService(RoomService roomService) {
this.roomService = roomService;
}
public Order getOrder() {
return order;
}
public void setOrder(Order order) {
this.order = order;
}
public OrderService getOrderService() {
return orderService;
}
public void setOrderService(OrderService orderService) {
this.orderService = orderService;
}
@SuppressWarnings("unchecked")
@Override
public String execute() throws Exception {
//�õ�orders�������û��Ķ���
List<Order> orders = this.orderService.findAllOrder();
//�����û���order.getRoomid�õ��û������ķ������hotels
List<Room> rooms = new ArrayList<Room>();
if (orders.size() > 0) {
for (Order order : orders) {
rooms.add((Room) this.roomService.findRoomById(order
.getRoomid()));
}
}
//�����û������ķ���hotels��listAllHotel���棬��jsp����ȡ������ʾ
Map requestList = (Map) ActionContext.getContext().get("request");
requestList.put("listAllRoom", rooms);
return SUCCESS;
}
}
OrderServiceAction.java文件:
package com.integration.action.order;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import org.apache.struts2.ServletActionContext;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import com.integration.entity.Room;
import com.integration.entity.Order;
import com.integration.entity.User;
import com.integration.service.RoomService;
import com.integration.service.OrderService;
@SuppressWarnings("serial")
public class OrderServiceAction extends ActionSupport {
private Room room;
private OrderService orderService;
private RoomService roomService;
private String message;
public String getMessage() {
return message;
}
public void setMessage(String message) {
this.message = message;
}
public Room getRoom() {
return room;
}
public void setRoom(Room room) {
this.room = room;
}
public OrderService getOrderService() {
return orderService;
}
public void setOrderService(OrderService orderService) {
this.orderService = orderService;
}
public RoomService getRoomService() {
return roomService;
}
public void setRoomService(RoomService roomService) {
this.roomService = roomService;
}
// �õ��û���Ԥ���ķ���
@SuppressWarnings("unchecked")
public String execute() throws Exception {
//�õ�orders�ĵ�ǰ�û��Ķ���
HttpServletRequest request = ServletActionContext.getRequest();
HttpSession session = request.getSession();
User user = (User) session.getAttribute("user");
List<Order> orders = this.orderService.getUserOrder(user);
//�����û���order.getRoomid�õ��û������ķ������hotels
List<Room> rooms = new ArrayList<Room>();
if (orders.size() > 0) {
for (Order order : orders) {
rooms.add((Room)this.roomService.findRoomById(order
.getRoomid()));
}
}
//�����û������ķ���hotels��listUserHotel���棬��jsp����ȡ������ʾ
Map requestList = (Map) ActionContext.getContext().get("request");
requestList.put("listUserRoom", rooms);
return SUCCESS;
}
// �˶�
public String delete() {
HttpServletRequest request = ServletActionContext.getRequest();
HttpSession session = request.getSession();
User user = (User) session.getAttribute("user");
// ɾ������
Order order = this.orderService.findOrderByUseridAndRoomid(user
.getUserid(), room.getRoomid());
this.orderService.removeOrder(order);
// ���÷���Ϊ�գ�����
Room roomUser = this.roomService.findRoomById(room.getRoomid());
roomUser.setStatus(0);
this.roomService.updateRoom(roomUser);
message = "�˶�����ɹ���";
return "delSuc";
}
}
SaveOrderAction.java文件:
package com.integration.action.order;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import org.apache.struts2.ServletActionContext;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import com.integration.entity.Room;
import com.integration.entity.Order;
import com.integration.entity.User;
import com.integration.service.RoomService;
import com.integration.service.OrderService;
@SuppressWarnings("serial")
public class SaveOrderAction extends ActionSupport {
private Order order;
private User user;
private Room room;
private OrderService orderService;
private RoomService roomService;
private String message;
public String getMessage() {
return message;
}
public void setMessage(String message) {
this.message = message;
}
public User getUser() {
return user;
}
public void setUser(User user) {
this.user = user;
}
public Room getRoom() {
return room;
}
public void setRoom(Room room) {
this.room = room;
}
public RoomService getRoomService() {
return roomService;
}
public void setRoomService(RoomService roomService) {
this.roomService = roomService;
}
public Order getOrder() {
return order;
}
public void setOrder(Order order) {
this.order = order;
}
public OrderService getOrderService() {
return orderService;
}
public void setOrderService(OrderService orderService) {
this.orderService = orderService;
}
@SuppressWarnings("unchecked")
@Override
public String execute() throws Exception {
Map request = (Map) ActionContext.getContext().get("request");
request.put("listRoom", this.roomService.findAllRoom());
return SUCCESS;
}
public String add() {
// �жϷ����Ƿ���
Room roomUser = this.roomService.findRoomById(room.getRoomid());
if (roomUser.getStatus() == 0) {
HttpServletRequest request = ServletActionContext.getRequest();
HttpSession session = request.getSession();
User user = (User) session.getAttribute("user");
order.setUserid(user.getUserid());
// Ԥ��
this.orderService.saveOrder(order);
// ���÷���Ϊ�����п��ˣ�
roomUser.setStatus(1);
this.roomService.updateRoom(roomUser);
return "addSuc";
} else {
message = "�÷����Ѿ��п�����ס�ˡ�";
return "addFail";
}
}
}
3.在struts.xml中配置全部订单管理控制器
在struts.xml文件中配置实现订单管理功能的全部控制器,并定义处理结果与视图资源之间的关系,配置代码如下:
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
"http://struts.apache.org/dtds/struts-2.0.dtd">
<struts>
<package name="hotel" extends="struts-default">
<action name="userService" class="userAction">
<result name="success" type="redirect">/info/userInfo.jsp</result>
<result name="input" type="redirect">/login.jsp</result>
<result name="userUpdate" type="dispatcher">/info/changePwd.jsp</result>
<result name="updateSuc" type="dispatcher">/info/result.jsp</result>
</action>
<action name="adminService" class="adminServiceAction">
<result name="loginSuc" type="redirect">/info/adminInfo.jsp</result>
<result name="input" type="redirect">/adminLogin.jsp</result>
</action>
<action name="saveUser" class="saveUserAction">
<result name="success" type="redirect">
<param name="actionName">listUser.action</param>
</result>
<result name="regSuc" type="dispatcher">/info/userInfo.jsp</result>
</action>
</package>
<!-- 用户登录后才能访问的action -->
<package name="authority" extends="struts-default">
<interceptors>
<interceptor name="authority"
class="com.integration.interceptor.AuthorityInterceptor" />
<interceptor-stack name="mydefault">
<interceptor-ref name="defaultStack" />
<interceptor-ref name="authority" />
</interceptor-stack>
</interceptors>
<default-interceptor-ref name="mydefault"></default-interceptor-ref>
<global-results>
<result name="login">/login.jsp</result>
</global-results>
<!-- order -->
<action name="saveOrder" class="saveOrderAction">
<result name="success" type="dispatcher">/order/add.jsp</result>
<result name="addSuc" type="redirect">orderService</result>
<result name="addFail" type="dispatcher">/info/result.jsp</result>
</action>
<action name="orderService" class="orderServiceAction">
<result name="success" type="dispatcher">/info/orderInfo.jsp</result>
<result name="delSuc" type="dispatcher">/info/result.jsp</result>
</action>
</package>
<!-- 管理员后才能访问的action -->
<package name="authorityAdmin" extends="struts-default">
<interceptors>
<interceptor name="authorityAdmin" class="com.integration.interceptor.AdminInterceptor" />
<interceptor-stack name="mydefaultAdmin">
<interceptor-ref name="defaultStack" />
<interceptor-ref name="authorityAdmin" />
</interceptor-stack>
</interceptors>
<default-interceptor-ref name="mydefaultAdmin"></default-interceptor-ref>
<global-results>
<result name="login">/adminLogin.jsp</result>
</global-results>
<!-- user -->
<action name="listUser" class="listUserAction">
<result name="success">/user/list.jsp</result>
</action>
<action name="deleteUser" class="removeUserAction">
<result name="success" type="redirect">listUser.action</result>
</action>
<action name="updatePUser" class="updatePUserAction">
<result name="success">/user/update.jsp</result>
</action>
<action name="updateUser" class="updateUserAction">
<result name="success" type="redirect">listUser.action</result>
</action>
<!-- admin -->
<action name="saveAdmin" class="saveAdminAction">
<result name="success" type="redirect">/index.jsp</result>
</action>
<!-- hotel -->
<action name="saveRoom" class="saveRoomAction">
<result name="success" type="redirect">listRoom.action</result>
</action>
<action name="listRoom" class="listRoomAction">
<result name="success">/room/list.jsp</result>
</action>
<action name="deleteRoom" class="removeRoomAction">
<result name="success" type="redirect">listRoom.action</result>
</action>
<action name="updatePRoom" class="updatePRoomAction">
<result name="success">/room/update.jsp</result>
</action>
<action name="updateRoom" class="updateRoomAction">
<result name="success" type="redirect">listRoom.action</result>
</action>
<!-- order -->
<action name="listOrder" class="listOrderAction">
<result name="success" type="dispatcher">/order/list.jsp</result>
</action>
</package>
</struts>
使用Spring技术集成Struts与Hibernate
1.Spring集成Hibernate
Spring集成Hibernate是通过applicationContext.xml文件完成的。
代码如下:
<bean id="sessionFactory"
class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="dataSource">
<ref local="dataSource" />
</property>
<property name="mappingResources">
<list>
<value>com/integration/entity/Order.hbm.xml</value>
<value>com/integration/entity/Admin.hbm.xml</value>
<value>com/integration/entity/Room.hbm.xml</value>
<value>com/integration/entity/User.hbm.xml</value>
</list>
</property>
</bean>
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"
destroy-method="close">
<property name="driverClassName">
<value>com.mysql.jdbc.Driver</value>
</property>
<property name="url">
<value>jdbc:mysql://localhost:3306/hotel</value>
</property>
<property name="username">
<value>root</value>
</property>
<property name="password">
<value>root</value>
</property>
<prop key="hibernate.show_sql">true</prop>
</props>
</bean>
完整的applicationContext.xml文件如下:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"
destroy-method="close">
<property name="driverClassName">
<value>com.mysql.jdbc.Driver</value>
</property>
<property name="url">
<value>jdbc:mysql://localhost:3306/hotel</value>
</property>
<property name="username">
<value>root</value>
</property>
<property name="password">
<value>root</value>
</property>
</bean>
<bean id="sessionFactory"
class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="dataSource">
<ref local="dataSource" />
</property>
<property name="mappingResources">
<list>
<value>com/integration/entity/Order.hbm.xml</value>
<value>com/integration/entity/Admin.hbm.xml</value>
<value>com/integration/entity/Room.hbm.xml</value>
<value>com/integration/entity/User.hbm.xml</value>
</list>
</property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">
org.hibernate.dialect.MySQLDialect
</prop>
<prop key="hibernate.show_sql">true</prop>
</props>
</property>
</bean>
<bean id="transactionManager"
class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory"></property>
</bean>
<!-- userDao -->
<bean id="userDao" class="com.integration.dao.impl.UserDAOImpl" scope="singleton">
<property name="sessionFactory">
<ref bean="sessionFactory" />
</property>
</bean>
<bean id="userServiceTarget" class="com.integration.service.impl.UserServiceImpl">
<property name="userDao" ref="userDao"></property>
</bean>
<bean id="userService"
class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean">
<property name="target" ref="userServiceTarget"></property>
<property name="transactionManager" ref="transactionManager"></property>
<property name="transactionAttributes">
<props>
<prop key="find*">PROPAGATION_REQUIRED,readOnly</prop>
<prop key="*">PROPAGATION_REQUIRED</prop>
</props>
</property>
</bean>
<bean id="saveUserAction" class="com.integration.action.user.SaveUserAction"
scope="prototype">
<property name="service" ref="userService"></property>
</bean>
<bean id="listUserAction" class="com.integration.action.user.ListUserAction"
scope="prototype">
<property name="service" ref="userService"></property>
</bean>
<bean id="removeUserAction" class="com.integration.action.user.RemoveUserAction"
scope="prototype">
<property name="service" ref="userService"></property>
</bean>
<bean id="updatePUserAction" class="com.integration.action.user.UpdatePUser">
<property name="service" ref="userService"></property>
</bean>
<bean id="updateUserAction" class="com.integration.action.user.UpdateUserAction"
scope="prototype">
<property name="service" ref="userService"></property>
</bean>
<bean id="userAction" class="com.integration.action.user.UserAction"
scope="prototype">
<property name="userService" ref="userService"></property>
</bean>
<!-- adminDao -->
<bean id="adminDao" class="com.integration.dao.impl.AdminDAOImpl" scope="singleton">
<property name="sessionFactory">
<ref bean="sessionFactory" />
</property>
</bean>
<bean id="adminServiceTarget" class="com.integration.service.impl.AdminServiceImpl">
<property name="adminDao" ref="adminDao"></property>
</bean>
<bean id="adminService"
class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean">
<property name="target" ref="adminServiceTarget"></property>
<property name="transactionManager" ref="transactionManager"></property>
<property name="transactionAttributes">
<props>
<prop key="find*">PROPAGATION_REQUIRED,readOnly</prop>
<prop key="*">PROPAGATION_REQUIRED</prop>
</props>
</property>
</bean>
<bean id="saveAdminAction" class="com.integration.action.admin.SaveAdminAction"
scope="prototype">
<property name="adminService" ref="adminService"></property>
</bean>
<bean id="adminServiceAction" class="com.integration.action.admin.AdminServiceAction"
scope="prototype">
<property name="adminService" ref="adminService"></property>
</bean>
<!-- roomDao -->
<bean id="roomDao" class="com.integration.dao.impl.RoomDAOImpl" scope="singleton">
<property name="sessionFactory">
<ref bean="sessionFactory" />
</property>
</bean>
<bean id="roomServiceTarget" class="com.integration.service.impl.RoomServiceImpl">
<property name="roomDao" ref="roomDao"></property>
</bean>
<bean id="roomService"
class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean">
<property name="target" ref="roomServiceTarget"></property>
<property name="transactionManager" ref="transactionManager"></property>
<property name="transactionAttributes">
<props>
<prop key="find*">PROPAGATION_REQUIRED,readOnly</prop>
<prop key="*">PROPAGATION_REQUIRED</prop>
</props>
</property>
</bean>
<bean id="saveRoomAction" class="com.integration.action.room.SaveRoomAction"
scope="prototype">
<property name="roomService" ref="roomService"></property>
</bean>
<bean id="listRoomAction" class="com.integration.action.room.ListRoomAction"
scope="prototype">
<property name="roomService" ref="roomService"></property>
</bean>
<bean id="removeRoomAction" class="com.integration.action.room.RemoveRoomAction"
scope="prototype">
<property name="roomService" ref="roomService"></property>
</bean>
<bean id="updatePRoomAction" class="com.integration.action.room.UpdatePRoom">
<property name="roomService" ref="roomService"></property>
</bean>
<bean id="updateRoomAction" class="com.integration.action.room.UpdateRoomAction"
scope="prototype">
<property name="roomService" ref="roomService"></property>
</bean>
<!-- orderDao -->
<bean id="orderDao" class="com.integration.dao.impl.OrderDAOImpl" scope="singleton">
<property name="sessionFactory">
<ref bean="sessionFactory" />
</property>
</bean>
<bean id="orderServiceTarget" class="com.integration.service.impl.OrderServiceImpl">
<property name="orderDao" ref="orderDao"></property>
</bean>
<bean id="orderService"
class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean">
<property name="target" ref="orderServiceTarget"></property>
<property name="transactionManager" ref="transactionManager"></property>
<property name="transactionAttributes">
<props>
<prop key="find*">PROPAGATION_REQUIRED,readOnly</prop>
<prop key="*">PROPAGATION_REQUIRED</prop>
</props>
</property>
</bean>
<bean id="saveOrderAction" class="com.integration.action.order.SaveOrderAction"
scope="prototype">
<property name="orderService" ref="orderService"></property>
<property name="roomService" ref="roomService"></property>
</bean>
<bean id="orderServiceAction" class="com.integration.action.order.OrderServiceAction"
scope="prototype">
<property name="roomService" ref="roomService"></property>
<property name="orderService" ref="orderService"></property>
</bean>
<bean id="listOrderAction" class="com.integration.action.order.ListOrderAction"
scope="prototype">
<property name="roomService" ref="roomService"></property>
<property name="orderService" ref="orderService"></property>
</bean>
</beans>
2.整合Struts 2 和Spring
先为项目添加Spring所需要的jar文件,然后修改web.xml文件。通过Listener的配置,是的Web应用启动时能够自动查找位于WEB-INF下的applicationContext.xml文件,并根据该文件创建Spring容器。接着在配置文件ApplicationContext.xml文件中完成对各个Bean的配置,该配置文件的作用是使得Spring能够自动完成依赖注入。
web.xml文件代码如下:
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
<filter>
<filter-name>struts2</filter-name>
<filter-class>
org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter
</filter-class>
</filter>
<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
</web-app>
哇~~到这里整个项目就该完成了……