CRUD 為 Create(新增)、Retrieve(查詢)、Update(修改)、Delete(刪除)首字母縮寫,描述持久層四大基本操作。本文以「新增部門管理員」為例,串聯 Struts 2 + Spring + Hibernate 全流程。
JSP 頁面 depAdd.jsp 含表單 userinfoForm,提交按鈕觸發 JS 驗證:
function check() {
var userName = document.userinfoForm.userName.value;
var password = document.userinfoForm.password.value;
// ... 非空、長度、兩次密碼一致、電話格式驗證 ...
if (allValid) document.userinfoForm.submit();
}
驗證通過後,表單資料自動封裝至 UserinfoForm (Struts FormBean)。
struts-config.xml 關鍵片段:
<form-bean name="userinfoForm" type="com.dpt.ec.web.form.UserinfoForm" />
<action path="/depAdd" name="userinfoForm"
type="org.springframework.web.struts.DelegatingActionProxy"
scope="request" validate="false">
<forward name="success" path="/ok.jsp" />
<forward name="warning" path="error.pub" />
<forward name="failure" path="error.sys" />
</action>
DelegatingActionProxy:Spring 整合關鍵,將 Action 實例化委託給 Spring 容器。scope="request":FormBean 存活於單次請求。applicationContext.xml 核心定義:
<!-- 事務管理器 -->
<bean id="transactionManager"
class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory" />
</bean>
<!-- DAO 事務代理 -->
<bean id="userinfoDAOProxy"
class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean">
<property name="transactionManager" ref="transactionManager" />
<property name="target" ref="UserinfoDAO" />
<property name="transactionAttributes">
<props>
<prop key="save*">PROPAGATION_REQUIRED</prop>
<prop key="modify*">PROPAGATION_REQUIRED</prop>
<prop key="delete*">PROPAGATION_REQUIRED</prop>
<prop key="get*">PROPAGATION_REQUIRED,readOnly</prop>
<prop key="*">PROPAGATION_REQUIRED,readOnly</prop>
</props>
</property>
</bean>
<!-- Service 注入 DAO 代理 -->
<bean id="UserinfoService" class="com.dpt.ec.service.impl.UserinfoService">
<property name="userinfoDAO" ref="userinfoDAOProxy" />
</bean>
<!-- Action 注入 Service -->
<bean name="/depAdd" class="com.dpt.ec.web.action.admin.DepAddAction" singleton="false">
<property name="userinfoService" ref="UserinfoService" />
</bean>
// DepAddAction.java
public class DepAddAction extends ActionSupport {
private UserinfoService userinfoService; // setter 注入
public String execute() {
UserinfoForm form = (UserinfoForm) getForm(); // 從 Struts 獲取
Userinfo userinfo = new Userinfo();
userinfo.setUserName(form.getUserName());
userinfo.setPassword(form.getPassword());
userinfo.setTel(form.getTel());
userinfo.setDepartment(form.getDepartment());
userinfo.setUserType("dep");
userinfoService.save(userinfo); // 呼叫 Service
return SUCCESS;
}
}
// UserinfoServiceImpl.java
public class UserinfoServiceImpl implements UserinfoService {
private UserinfoDAO userinfoDAO; // setter 注入
public void save(Userinfo u) { userinfoDAO.save(u); }
}
// UserinfoDAO.java (extends HibernateDaoSupport)
public class UserinfoDAO extends HibernateDaoSupport implements IUserinfoDAO {
public void save(Userinfo instance) {
getHibernateTemplate().save(instance);
}
}
Userinfo.hbm.xml 映射 userinfo 資料表,POJO 類別 Userinfo 對應表欄位。Spring 管理 SessionFactory,讀取資料庫連線資訊與映射檔。
applicationContext.xml 統一管理 Bean 定義與關係,利於維護。