舉一個例子:工程中有一個公司列表頁面companyList.jsp、一個公司信息編輯頁面companyForm.jsp,一個action CompanyAction。在公司列表頁面上點擊某個公司的Id字段就跳轉到公司信息編輯頁面,編輯完成后點擊save按鈕又返回到companyList.jsp。
companyList.jsp的代碼:
<%@ include file="/common/taglibs.jsp"%>
<head>
<title><fmt:message key="companyList.title"/></title>
<meta name="heading" content="<fmt:message key='companyList.heading'/>"/>
<meta name="menu" content="CompanyMenu"/>
</head>
<c:out value="${buttons}" escapeXml="false" />
<display:table name="companies" class="table" requestURI="" id="companyList" export="true" pagesize="25">
<display:column property="id" sortable="true" href="editCompany.html" media="html"
paramId="id" paramProperty="id" titleKey="company.id"/>
<display:column property="name" sortable="true" titleKey="company.name"/>
</display:table>
<script type="text/javascript">
highlightTableRows("companyList");
</script>
其中id列的連接為editCompany.html,實際上就是companyForm.jsp,Struts 2會進行映射的。
companyForm.jsp的代碼:
<%@ include file="/common/taglibs.jsp"%>
<head>
<title><fmt:message key="companyDetail.title"/></title>
<meta name="heading" content="<fmt:message key='companyDetail.heading'/>"/>
</head>
<s:form id="companyForm" action="saveCompany" method="post" validate="true">
<li style="display: none">
<s:hidden key="company.id"/>
</li>
<s:textfield key="company.name" required="false" maxlength="50" cssClass="text medium"/>
<li class="buttonBar bottom">
<s:submit cssClass="button" method="save" key="button.save" theme="simple"/>
<s:submit cssClass="button" method="delete" key="button.delete"
onclick="return confirmDelete('Company')" theme="simple"/>
<s:submit cssClass="button" method="cancel" key="button.cancel" theme="simple"/>
</li>
</s:form>
<script type="text/javascript">
Form.focusFirstElement($("companyForm"));
</script>
CompanyAction的代碼:
public class CompanyAction extends BaseAction implements Preparable {
private GenericManager<Company, Long> companyManager;
private Company company;
private Long id;
public void setCompanyManager(GenericManager<Company, Long> companyManager) {
this.companyManager = companyManager;
}
public void setId(Long id) {
this. id = id;
}
public Company getCompany() {
return company;
}
public void setCompany(Company company) {
this.company = company;
}
public String delete() {
companyManager.remove(company.getId());
return SUCCESS;
}
public String edit() {
if (id != null) {
company = companyManager.get(id);
} else {
company = new Company();
}
return SUCCESS;
}
public String save() throws Exception {
if (cancel != null) {
return "cancel";
}
if (delete != null) {
return delete();
}
boolean isNew = (company.getId() == null);
companyManager.save(company);
return SUCCESS;
}
}
struts.xml中的相關配置:
<action name="editCompany" class="com.opentide.openstore.webapp.action.CompanyAction" method="edit">
<result>/WEB-INF/pages/companyForm.jsp</result>
<result name="error">/WEB-INF/pages/companyList.jsp</result>
</action>
<action name="saveCompany" class="com.opentide.openstore.webapp.action.CompanyAction" method="save">
<result name="input">/WEB-INF/pages/companyForm.jsp</result>
<result name="cancel" type="redirect">companyList.jsp</result>
<result name="delete" type="redirect">companyList.jsp</result>
<result name="success" type="redirect">companyList.jsp</result>
</action>
如果現在有一個customerForm.jsp,要在該頁面上提供編輯公司信息的入口,編輯完畢并保存后返回到customerForm.jsp。這個要求可以擴展為“從哪個頁面轉進來,處理完畢后就轉到原來的頁面”,這該如何實現呢?
在目前的配置中,CompanyAction的各個result指向的頁面是寫死的,如果能夠在運行時才確定各個result指向的頁面就好了,恰好Struts 2提供了這樣的方法。
修改CompanyAction,為它增加一個屬性ReturnUrl,用來保存處理完畢時要跳轉回去的頁面:
private String returnUrl;
public String getReturnUrl() {
return returnUrl;
}
public void setReturnUrl(String returnUrl) {
this.returnUrl = returnUrl;
}
修改struts.xml中saveCompany的配置,各個result指向的頁面從returnUrl參數取得:
<action name="saveCompany" class="com.opentide.openstore.webapp.action.CompanyAction" method="save">
<result name="input">/WEB-INF/pages/companyForm.jsp</result>
<result name="cancel" type="redirect">${returnUrl}</result>
<result name="delete" type="redirect">${returnUrl}</result>
<result name="success" type="redirect">${returnUrl}</result>
</action>
修改companyForm.jsp,為表單增加一個名為returnUrl的隱藏域,用于記錄returnUrl參數的值,并且在提交表單的時候為該參數重新賦值:
<s:hidden key="returnUrl" />
最后修改companyList.jsp,為跳轉到companyForm.jsp的連接增加returnUrl參數,參數值為companyList.jsp本身(即通知companyForm.jsp,處理完畢后還回到現在companyList.jsp頁面):
<display:column property="id" sortable="true" href="editCompany.html?returnUrl=companyList.jsp" media="html"
paramId="id" paramProperty="id" titleKey="company.id"/>
發布修改后的工程并運行,果然達到了預期的目的。
要在其它頁面上提供公司信息編輯的入口,只要在該頁面上提供一個連接,連接地址為editCompany.html?returnUrl=<當前頁面地址>即可。
posted on 2008-03-13 21:14
雨奏 閱讀(3846)
評論(2) 編輯 收藏 所屬分類:
Java