利用函數(shù)computeURL( )實現(xiàn)同一FORM的多動作提交
在實際處理的頁面中,往往在一個頁面中有多個觸發(fā)的動作,而Struts的ActionForm中只能指定一個Action,是一種粗粒度的實現(xiàn)(JSF中有更好的解決方案),computeURL( )可以提供一種變通的解決方法.computeURL( )是在org.apache.struts.util.RequestUtils(Struts Ver1.1)與org.apache.struts.taglib.TagUtils(Struts Ver1.2)類中的一個函數(shù),用來解析基于Forward,Action,鏈接,頁面參數(shù)的URL可以用來動態(tài)改變頁面中Form對應(yīng)的Action.Ver1.1中有以下兩種:
1.computeURL(javax.servlet.jsp.PageContext pageContext, java.lang.String forward, java.lang.String href, java.lang.String page, java.util.Map params, java.lang.String anchor, boolean redirect)
2.computeURL(javax.servlet.jsp.PageContext pageContext, java.lang.String forward, java.lang.String href, java.lang.String page, java.lang.String action, java.util.Map params, java.lang.String anchor, boolean redirect)
其中第一個是 Deprecated.第二個在新版本中得以保留,另外還提供了另外一種重載:
computeURL(javax.servlet.jsp.PageContext pageContext, java.lang.String forward, java.lang.String href, java.lang.String page, java.lang.String action, java.lang.String module, java.util.Map params, java.lang.String anchor, boolean redirect, boolean encodeSeparator)
參數(shù)說明如下:
pageContext
- PageContext for the tag making this call
forward
- Logical forward name for which to look up the context-relative URI (if specified)
href
- URL to be utilized unmodified (if specified)
page
- Module-relative page for which a URL should be created (if specified)
action
- Logical action name for which to look up the context-relative URI (if specified)
params
- Map of parameters to be dynamically included (if any)
anchor
- Anchor to be dynamically included (if any)
redirect
- Is this URL for a response.sendRedirect(
下面介紹一下詳細的使用方法:
1.在JSP頁面中導(dǎo)入對應(yīng)的包:
<%@ page import= "org.apache.struts.util.RequestUtils"%>
或
<%@ page import= "org.apache.struts.taglib.TagUtils"%>
2.創(chuàng)建一個JAVASCRIPT函數(shù):
<script language="JavaScript" type="text/javascript">
function search() {
<%String searchUrl = RequestUtils.computeURL(
pageContext,
null,
null,
"/Search.do",
null,
null,
null,
false);
%>
document.form1.action = "<%=searchUrl%>";
document.form1.submit();
}
</script>
3.在JSP頁面中給對應(yīng)的表單指定ID以便上面的函數(shù)進行確定提交的是哪個FORM(如果一個頁面在存在多個FORM的話):
<html:form styleId="form1" action="/aotherSearch">
.........
</html:form>
4.在需要觸發(fā)提交動作的地方,調(diào)用2中的JAVASCRIPT函數(shù):
<html:button property="searchInfo" value="檢索" onclick="search()" style="width:100px" />
對應(yīng)的ACTION與FORM在配置文件中定義.這樣,就可以動態(tài)更改FORM的ACTION實現(xiàn)一個FORM對應(yīng)多個ACTION了.
posted on 2005-11-24 11:29 Terence 閱讀(1620) 評論(2) 編輯 收藏 所屬分類: STRUTS