举一个例子:工程中有一个公司列表页面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
雨奏 阅读(3834)
评论(2) 编辑 收藏 所属分类:
Java