Action子類
DispatchAction ForwardAction
頁面
url?methodName=add
<input type="hidden" name="methodName" value="add">
設定檔
<action parameter="methodName">
DispatchAction
add(mapping,form,req,resp){
}
MappingDispatchAction
功能:和父類一樣,將多個商務程序組織放在一個處理類中
優勢:
不需要頁面指定額外的方法名資訊
只需要在設定檔中直接指定方法名即可
<%@page isELIgnored="false"%>
xml檔案
系統運行必不可少的資訊
資源檔
系統啟動並執行輔助資訊
可能儲存:國際化顯示資訊
錯誤描述資訊
1 全域跳轉設定<global-forwards />:在設定檔中配置一次,所有的action共用
<global-forwards>
<forward name="errorPage" path="/WEB-INF/page/error.jsp"></forward>
</global-forwards>
2 基於web容器,設定錯誤碼跳轉資源
<error-page>
<error-code>404</error-code>
<location>/404.jsp</location>
</error-page>
<error-page>
<exception-type>java.lang.NullPointerException</exception-type>
<location>/nullPointer.jsp</location>
</error-page>
3 基於struts架構,設定異常的跳轉資源
1) 在設定檔中指定異常類型及跳轉目標
<global-exceptions>
<exception key="error.exception.empty" type="java.lang.NullPointerException" path="/nullPointer.jsp"></exception>
</global-exceptions>
===================================
type 是異常類路徑
path 是異常出現之後,系統跳轉的資源目標
key 是跳轉到頁面之後,頁面中顯示的資訊,在資源檔中所對應的key資訊
===================================
2) 把錯誤描述資訊書寫在資源檔中
error.exception.empty = null pointer exception occured
3) 在頁面中使用struts標籤,讀取錯誤描述資訊
<%@taglib prefix="bean" uri="http://struts.apache.org/tags-bean"%>
<bean:message key="error.exception.empty"/>
4 Action
ForwardAction IncludeAction DispatchAction
MappingDispatchAction LookupDispatchAction
5 IncludeAction:
解決頁面中引入其他頁面時,遵循mvc思想
1) 在頁面中必鬚髮送.do方式的請求
<jsp:include page="/headPage.do">
<jsp:param name="title" value="stu Management"/>
</jsp:include>
2) 在struts-config.xml中指定處理類
<action path="/headPage" type="org.apache.struts.actions.IncludeAction" parameter="/head.jsp"></action>
【3)】 如果引入方頁面傳遞參數給被引入得頁面,則在被引入得頁面中動態擷取參數資訊
${param.title } System
function assign(msg){
if(msg == 'modify')
form1.action.value = 'modify.do';
if(msg == 'delete')
form1.action.value = 'delete.do';
form1.action.submit();
}
<form name="form1" action="">
......
<input type="submit" value="modify" onclick="assign('modify')"/>
<input type="submit" value="delete" onclick="assign('delete')"/>
</form>
6 LookupDispatchAction
解決問題:一個form表單中具有多個提交按鈕
編寫流程:
1) 編寫頁面index.jsp
<form name="form1" action="operate.do">
<input type="text" name="uname"/><br>
<html:submit property="bu">
<bean:message key="button.modifyButton"/>
</html:submit>
<html:submit property="bu">
<bean:message key="button.deleteButton"/>
</html:submit>
</form>
===============================================
action 指定統一的目標operate.do
property指定提交按鈕的name屬性值
<bean:message key="button.modifyButton"/>
指定提交按鈕在用戶端的顯示資訊
===============================================
2) 配置struts-config.xml
<action path="/operate" type="com.sinojava.struts.web.AllAction" parameter="bu">
<forward name="resultPage" path="/result.jsp" />
</action>
===============================================
parameter="bu" 指定用戶端頁面中提交按鈕的名字
===============================================
3) 編寫處理類:
要求必須繼承LookupDispatchAction,覆蓋其中geteKeyMethodMap方法
AllAction extends LookupDispatchAction {
protected Map getKeyMethodMap() {
Map map = new HashMap();
map.put("button.modifyButton", "modifyInfo");
map.put("button.deleteButton", "deleteInfo");
return map;
}
public ActionForward modifyInfo(....
public ActionForward deleteInfo(....
}
4) 編寫資源檔
button.modifyButton = modify info button
button.deleteButton = delete info button
html 顯示
jstl 邏輯控制
el 資料操作
struts標籤:
struts-html 顯示
struts-bean 資料操作
struts-logic 邏輯控制
注意:
使用struts標籤的常見異常:
1 Cannot retrieve mapping for action /regist
解決:使用<html:form>要求必須配置path屬性
2 Cannot retrieve definition for form bean null
解決,<html:form>要求必須建立並配置formBean