webapps/*.war 複製至 Tomcat/webappshttps://localhost:8080/struts-example/index.jsp 出現 "powered by Struts" 即成功複製 struts-example/WEB-INF 至新專案目錄,清空 src、classes、struts-config.xml 內容,即得乾淨 Struts 專案骨架。
<html:form action="/regUserAction" focus="logname">
<html:text property="logname" size="20" maxlength="20"/>
<html:password property="password" size="20" maxlength="20"/>
<html:password property="email" size="30" maxlength="50"/> <!-- 應改用 html:text -->
<html:submit/> <html:reset/>
</html:form>
<form-bean name="regUserForm" type="org.cjea.struts.example.RegUserForm"/>
<action path="/regUserAction"
type="org.cjea.struts.example.RegUserAction"
attribute="regUserForm"
scope="request"
validate="false">
<forward name="failure" path="/messageFailure.jsp"/>
<forward name="success" path="/messageSuccess.jsp"/>
</action>
public class RegUserForm extends ActionForm {
private String logname, password, email;
// getter/setter/reset 省略
}
public class RegUserAction extends Action {
public ActionForward perform(ActionMapping mapping,
ActionForm form,
HttpServletRequest req,
HttpServletResponse res) {
// 取得參數、處理資料庫邏輯、回傳 forward
}
}
| 傳統 JSP/Servlet/JavaBean | Struts 架構 |
|---|---|
| Servlet 兼任 Controller、資料取得、邏輯處理 | ActionServlet 專司導航,FormBean 封裝資料,Action 處理邏輯 |
struts-config.xmlStruts 是優秀的 J2EE MVC 實作,掌握其核心配置與標籤庫,配合團隊規範,能顯著提升專案可維護性。建議在中型以上專案或長期維護系統中採用。