?? 14. struts1.x note.txt
字號:
header="messages.header" footer="messages.footer">
<tr>
<td></td>
<td><bean:write name="msg" /></td>
</tr>
</html:messages>
4.<html:image>與<html:img>
作用:在頁面中產生圖像的輸出
最重要的屬性page:圖象文件的路徑,前面必須帶有一個斜線。
其它屬性:height、width(只有 img 可以設置長和寬)、alt(不知何用)。
Example:
<html:image page="/f4icmu.jpg" alt="軟件工程之通俗版"></html:image>
<html:img page="/f4icmu.jpg"/> 等價于: <input type="image" name="" src="/工程名/f4icmu.jpg" alt="軟件工程詳解"> <img src="/工程名/f4icmu.jpg">
5.<html:checkbox/>
生成一個checkbox。這里的value值可以是true,yes或on。
checkboxForm的屬性:
private boolean one = false;
private boolean two = false;
private boolean three = false;
<html:checkbox name="checkboxForm" property="one">One</html:checkbox>
<html:checkbox name="checkboxForm" property="two">Two</html:checkbox>
<html:checkbox name="checkboxForm" property="three">Three</html:checkbox>
如果選中后被提交則相應的屬性的值為true。
6.<html:radio/>
生成一個radio。主要的用法有兩種。
下面的代碼示例了html:radio標簽的一般用法,
如果被提交則選中的radio的value值將被提交到radioForm中的id中。
<html:radio name="radioForm" property="id" value="00001"> One </html:radio>
<html:radio name="radioForm" property="id" value="00002"> Two </html:radio>
7.<html:select>標簽和<html:option>標簽
作用:對html中的下拉選擇標簽與選項標簽做了封裝
單選下拉
Example1:
<html:select property="singleSelect" size="3">
<html:option value="Single 0">Single 0</html:option>
<html:option value="Single 1">Single 1</html:option>
<html:option value="Single 2">Single 2</html:option>
</html:select>
Example2:性別
1)提供label和value的數組
<% String[] label = { "男", "女", "未知" };
String[] value = { "male", "female", "unknown" };
pageContext.setAttribute("label", label);
pageContext.setAttribute("value", value);
%>
2)標簽使用:
<html:select property="sex" size="1">
<html:options labelName="label" name="value" />
</html:select>
Example3:興趣愛好多選
1)提供列表
<% List options = new ArrayList();
options.add(new LabelValueBean("電腦游戲", "PCGame"));
options.add(new LabelValueBean("看電視", "TV"));
options.add(new LabelValueBean("閱讀", "Reading"));
options.add(new LabelValueBean("唱歌", "Singing"));
pageContext.setAttribute("opt", options);
%>
其中,
public class LabelValueBean {
private String label;
private String value; public LabelValueBean(String label, String value){}
}
2)提供多選標簽
<html:select property="favors" multiple="true">
<html:options collection="opt" property="value"
labelProperty="label" />
</html:select>
Example4:聯系方式多選
1)提供多選列表
<% List myPhones=new ArrayList();
myPhones.add(new LabelValueBean("小靈通","33213322"));
myPhones.add(new LabelValueBean("固話","80512010"));
myPhones.add(new LabelValueBean("手機","13711221113"));
pageContext.setAttribute("myPhones",myPhones);
%>
2)標簽的使用
<html:select property="phones" multiple="true">
<html:optionsCollection name="myPhones"/>
</html:select>
8.<html:submit>標簽
<html:submit value="Submit" />
9.<hmtl:text>標簽
文本輸入框
<html:text property="username" />
10.<html:password>標簽
<html:password property="password"/>
11.<html:cancel/>取消標簽
四、Logic標記 :
該標簽庫包含的標簽可以用來進行邏輯判斷、集合迭代和流程控制。
1.<logic:iterate/>
作用:<logic:iterate/>標簽用來迭代集合
Example:
<%
List stuList=new ArrayList();
Student stu=new Student();
stu.setName("Alice");
...
stuList.add(stu);
request.setAttribute("stuList",stuList);
%>
<logic:iterate id="stu" name="stuList">
<tr>
<td>${stu.id }</td>
<td>${stu.name }</td>
<td>${stu.sex }</td>
<td>${stu.age }</td>
<td>${stu.desc }</td>
</tr>
</logic:iterate>
(2) logic:empty
用來判斷是否為空的。如果為空,該標簽體中嵌入的內容就會被處理。該標簽用于以下情況:
當Java對象為null時
當String對象為""時
當java.util.Collection對象中的isEmpty()返回true時
當java.util.Map對象中的isEmpty()返回true時
下面的代碼示例了logic:empty標簽判斷集合persons是否為空:
<logic:empty name="stu" property = "books">
<div>集合books為空!</div> </logic:empty>
logic:notEmpty標簽的應用正好和logic:empty標簽相反。
(3) logic:equal
這里要介紹的不只是logic:equal(=)標簽,而是要介紹一類標簽,這類標簽完成比較運算,
包括:
logic:equal(=)
logic:notEqual(!=)
logic:greaterEqual(>=)
logic:lessEqual(<=)
logic:graterThan(>)
logic:lessThan(<)
Example:
<logic:equal name="stu" property="name" value="Alice">
I am Alice.
</logic:equal>
<bean:define id="count" value="168"/>
<logic:equal name="count" value="168">
Good lucky number.
</logic:equal>Day3:1 定制Action2 禁止表單重復提交3 定制Controller4 驗證框架************************************************************************1 定制Action1) DispatchAction:可以將相關的一組操作放在一個Action類中(同一模塊功能) 特點(優點) : 1)一個 Action可以對應多個業務方法(CRUD),而無需通過增加隱藏域的方式來處理。 2)避免Action類數量隨業務復雜度而膨脹,可以共享公共的業務邏輯代碼。 3)無需重寫execute方法 4)DispatchAction類本身已經重寫了Action類的execute方法。 實現DispatchAction的步驟: 1) 繼承DispatchAction SuperGileAction extends DispatchAction 2) 定義自己的處理方法,不要覆蓋execute()方法 3) struts-config.xml中action元素增加parameter屬性 <action path="/super" name="superForm" parameter="method" type="com.tarena.action.SuperGileAction"> <forward name="success" path="/success.jsp"></forward> </action> 用戶請求URL應有如下格式: a)Get方法 http://localhost:8080/super/SuperGileAction.do?method=<method_name> b) Post方法 在表單中添加參數來完成2) LookupDispatchAction: 特點:它是DispatchAction的子類,所以具備DispatchAction的所有特性, 支持一個表單對應多個業務方法。 實現LookupDispatchAction的步驟: 1) 繼承LookupDispatchAction 2) 定義自己的處理方法,覆蓋getKeyMethodMap()方法,不要覆蓋execute()方法 getKeyMethodMap()完成資源文件中Key與Action中方法映射 @Override protected Map getKeyMethodMap() { Map map = new HashMap(); map.put("submit.modify", "modify"); map.put("submit.add", "register"); return map; } public ActionForward modify(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response){ ... } public ActionForward register(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response){ ... } 3)struts-config.xml中為Action增加parameter屬性 <action path="/lookupAction" name="superForm" type="com.tarena.action.SuperGirlLookupAction" parameter="method"> <forward name="success" path="/success.jsp"></forward> </action> 4) 提交頁面按鈕有如下格式 html:submit的property屬性值與Action中的parameter屬性值映射 <html:submit property="method"> <bean:message key="submit.add"/> </html:submit> <html:submit property="method"> <bean:message key="submit.modify"/> </html:submit>3) Action編程技巧(建議) : 1)盡量避免使用實例變量或靜態變量 (servlet是單實例多線程,所以要自己維護成員變量的同步問題) 2)使用自己的BaseAction完成Action的公共操作,其余Action可從BaseAction派生 3)Action不要代替Model工作 在設計多層架構時,使用Business Delegate可降低層與層之間的耦合度2 禁止表單重復提交 1)客戶端實現(JavaScript) 2)JSP實現(腳本) 圖解 實現代碼 3)Struts實現 (1)Struts內置實現,無需額外配置 (2)需要在前一個action的方法中調用saveToken()方法 saveToken(request); (3)在后一個執行業務邏輯的方法中使用isTokenValid(request)判斷表單是否被重復提交 if (isTokenValid(request)) { // invoke business method //...... // reset transaction token after transaction success! this.resetToken(request); return mapping.findForward("success"); } else { System.out.println("duplicate token"); return mapping.findForward("error"); }3 定制Controller(可選) 擴展控制器功能 ActionServlet與RequestProcessor的作用 1)繼承RequestProcessor 2)通過processPreprocess()增加定制功能 //自定義控制器需要派生自RequestProcessor package com.controller; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import org.apache.struts.action.RequestProcessor; public class SecurityRequestProcessor extends RequestProcessor { @Override protected boolean processPreprocess(HttpServletRequest request, HttpServletResponse response) { System.out.println("start controller ..."); request.getSession().getServletContext().log("URL="+request.getRequestURL());//寫日志 String ip = request.getRemoteAddr(); if (ip.equals("127.0.0.1")) { System.out.println("valid client: 127.0.0.1"); return true; //只允許此IP登錄 } int lastPoint = ip.lastIndexOf("."); String fourth = ip.substring(lastPoint + 1); int fourthInt = Integer.parseInt(fourth); if (ip.startsWith("192.168.1.") && fourthInt > 50 && fourthInt < 60) { System.out.println("invalid client: 192.168.1.[50~60]"); return false; //限制黑名單IP的登錄 } return false; }} 3)struts-config.xml中配置 <controller .../> (一般只有一個controller) 寫在<action-mappings /> 之后;<message-resources .../> 之前 <controller processorClass="com.controller.SecurityRequestProcessor"/> 補充說明: 通常的一個struts-config.xml只能配置一個controller 一般地,在開發時通過定制controller來增強系統功能,例如:日志、安全限制等。 但在實際的應用中比較少使用。 舉例: public class SecurityRequestProcessor extends RequestProcessor { public boolean processPreprocess(javax.servlet.http.HttpServletRequest request, javax.servlet.http.HttpServletResponse response){ String ip=request.getRemoteAddr(); if(ip.equals("127.0.0.1")) return true; int lastPoint = ip.lastIndexOf("."); String fourth=ip.substring(lastPoint+1); int fourthInt=Integer.parseInt(fourth); if(ip.startsWith("192.168.1.")&&fourthInt>50&&fourthInt<60){ return true; } return false; } }4 驗證(校驗) 框架 (1)有三種數據驗證方式: a.在ActionForm.validate() 當中進行 b.在Action.execute()方法中進行(這兩種參看day2的異常處理,前兩種) c.引入validator框架(apache.jakata) (2)聲明式的數據驗證(為什么能使用聲明式?)--配置 原因:對于數據的驗證有模式 (3)validator框架已經定義了一部分通用的驗證規則和邏輯 開發者只需對特定數據指定具體的驗證規則(業務)--使用 包含在validation.xml 使用驗證框架的步驟: 1)在struts-config.xml配置校驗插件(validate plugin) 放在<message-resources ../> 之后 <plug-in className="org.apache.struts.validator.ValidatorPlugIn"> <set-property property="pathnames" value="/WEB-INF/validator-rules.xml,/WEB-INF/validation.xml"/> </plug-in> 注釋: 1.驗證插件: 1) validator-rules.xml :定義驗證規則 規則名(如:required, maxlength.....) 驗證方法 驗證失敗時出錯提示(放到上下文中) 2)validation.xml 使用規則:對指定的表單屬性進行校驗 2.基本的結構: 1)驗證邏輯: validateXxx()方法 2)在validator-rule.xml中定義的都是校驗規則 3)在validation.xml中(聲明式驗證) a.引入了struts-config.xml的表單名字(form-bean的名字) b.聲明域的驗證依賴規則 depends = "required,Integer" 2)add validator-rule.xml and validation.xml to /WEB-INF/ copy validator-rule.xml from struts framework edit validation.xml 注意版本的一致<?xml version="1.0" encoding="UTF-8" ?><!DOCTYPE form-validation PUBLIC "-//Apache Software Foundation//DTD Commons Validator Rules Configuration 1.1.3//EN" "http://jakarta.apache.org/commons/dtds/validator_1_1_3.dtd">
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -