Cookbook-Struts1.3.8案例分析-Simple Form using ActionForm
Cookbook-Struts1.3.8案例分析-Simple Form using ActionForm
l Simple Form using ActionForm
入口Action配置
<action path="/prepareSimple" type="examples.SuccessAction"> <forward name="success" path="/jsp/simple/Simple.jsp" /> </action> <action path="/processSimple" type="examples.simple.ProcessSimpleAction" name="simpleForm" scope="request" input="/jsp/simple/Simple.jsp" cancellable="true" validate="true"> <forward name="success" path="/jsp/simple/SimpleResults.jsp" /> </action> |
SuccessAction繼承自Action類
Execute方法非常簡單,轉向success標識的頁面simple.jsp
public ActionForward execute( ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception { return mapping.findForward("success"); } |
Form Jsp頁面里用到的標簽
<html:base/> 類似html的<base/>標簽 <html:html xhtml="true" lang="true"> 類似與<html>標簽 <html:link page="/jsp/simple/source.jsp"> XXX </html:link> 類似<a href=?>XXX</a> <html:errors/> 顯示錯誤 <html:form action="/processSimple"> form,并定義一個Action <html:text property="name" size="40" maxlength="50"/> 文本輸入框 <html:password property="secret" size="40" maxlength="50"/> 密碼輸入框 <html:select property="color"> <html:option value="red">Red</html:option> <html:option value="green">Green</html:option> <html:option value="blue">Blue</html:option> </html:select> 靜態的下拉框 <html:radio property="rating" value="1">Actually, I hate it.</html:radio> <html:radio property="rating" value="2">Not so much.</html:radio>靜態單選按鈕 <html:textarea property="message" cols="40" rows="6"/> 大文本框 <html:hidden property="hidden" value="Sssh! It's a secret. Nobody knows I'm here."/> 隱藏域 <html:submit> <bean:message key="button.submit" />來自資源文件的文字 </html:submit>提交按鈕 <html:cancel/>取消按鈕 |
ActionForm類的定義
publicclass SimpleActionForm extends ActionForm 繼承自ActionForm
public ActionErrors validate( ActionMapping mapping, HttpServletRequest request) { ActionErrors errors = new ActionErrors(); 創建ActionErrors // Name must be entered if ((name == null) || (name.length() < 1)) { errors.add("name", new ActionMessage("errors.name.required")); } 添加ActionError // Secret Phrase must be entered if ((secret == null) || (secret.length() < 1)) { errors.add("secret", new ActionMessage("errors.secret.required")); } return (errors); } publicvoid reset(ActionMapping mapping, HttpServletRequest request) { this.name = null; this.secret = null; this.color = null; this.confirm = false; this.rating = null; this.message = null; this.hidden = null; } 重置ActionForm的屬性 |
Result JSP里用到的標簽
<bean:write name="simpleForm" property="name" /> 輸出actionform的屬性值 <logic:equal name="simpleForm" property="confirm" value="true"> 邏輯判斷屬性值是否是true <logic:notEqual name="simpleForm" property="confirm" value="true"> 邏輯判斷屬性值不是true <bean:write name="simpleForm" property="message" filter="false"/> filter=false表示可以輸出<>等符號 |
posted on 2008-07-08 02:21 MingIsMe 閱讀(122) 評論(0) 編輯 收藏 所屬分類: 16 案例分析