使用 html:form、html:text、html:password、html:submit 等標簽,配合 html:javascript 自動生成客戶端驗證腳本。錯誤信息通過 html:errors 顯示。
封裝 user/password 屬性,reset() 方法清空密碼,便於登錄失敗後重新輸入。
將登錄、註冊等操作集中在 AjaxDispatchAction,通過 method=login 參數分發。核心邏輯:
Service 業務層實例return new ActionForward(type + ".do", true))validation.xml 定義 required、mask(正則)、minlength/maxlength 規則ApplicationResources.propertiesinterface Person { getId(), getUser(), getPassword(), getType()... }
abstract class User implements Person { ... }
class Admin extends User { getType() { return "admin"; } }
class Teacher extends User { getType() { return "teacher"; } }
class Student extends User { getType() { return "student"; } }
登錄時無需關心具體類型,利用多態統一處理,getType() 動態決定跳轉目標。
封裝通用功能:
getService():從 Application 獲取 ServiceFactory 創建 ServiceisLogin() / getPerson():Session 登錄態檢查execute() 模板方法:未登錄攔截 → 子類 executeAction()XMLHttpRequest(兼容 IE ActiveXObject)ajax.do?method=checkUserIsExist&user=xxx)onreadystatechange 回調處理響應Vector<LabelValueBean> 渲染getDepartments(instituteId) 異步獲取專業 → JS buildSelect 動態填充專業下拉getClasses(departmentId) 異步獲取班級 → 填充班級下拉後端返回編碼字符串(如 \"計算機科學,1|軟體工程,2\"),前端分割構建 Option。
理解 MVC:Action(Controller)處理業務邏輯,結果放入 Request,轉發至 JSP(View)純渲染。
public class Page<E> {
private int current, total, pages, each, start, end;
private boolean next, previous;
private Vector<E> data; // 泛型容器,支援任意業務對象
// 構造器自動計算總頁數、起止記錄、上下頁標誌
}
結合泛型,同一分頁組件可復用於學生、教師、課程等任意列表,大幅減少重複代碼。
Struts 雖屬經典,但其「配置驅動、標簽庫豐富、驗證框架內建」的思想仍具參考價值。實踐中應注重: