隨筆 - 5  文章 - 17  trackbacks - 0
          <2008年9月>
          31123456
          78910111213
          14151617181920
          21222324252627
          2829301234
          567891011

          常用鏈接

          留言簿(3)

          隨筆分類

          隨筆檔案

          搜索

          •  

          最新評(píng)論

          閱讀排行榜

          評(píng)論排行榜

          舉一個(gè)例子:工程中有一個(gè)公司列表頁(yè)面companyList.jsp、一個(gè)公司信息編輯頁(yè)面companyForm.jsp,一個(gè)action CompanyAction。在公司列表頁(yè)面上點(diǎn)擊某個(gè)公司的Id字段就跳轉(zhuǎn)到公司信息編輯頁(yè)面,編輯完成后點(diǎn)擊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,實(shí)際上就是companyForm.jsp,Struts 2會(huì)進(jìn)行映射的。

          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中的相關(guān)配置:
          <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> 

          如果現(xiàn)在有一個(gè)customerForm.jsp,要在該頁(yè)面上提供編輯公司信息的入口,編輯完畢并保存后返回到customerForm.jsp。這個(gè)要求可以擴(kuò)展為“從哪個(gè)頁(yè)面轉(zhuǎn)進(jìn)來(lái),處理完畢后就轉(zhuǎn)到原來(lái)的頁(yè)面”,這該如何實(shí)現(xiàn)呢?

          在目前的配置中,CompanyAction的各個(gè)result指向的頁(yè)面是寫死的,如果能夠在運(yùn)行時(shí)才確定各個(gè)result指向的頁(yè)面就好了,恰好Struts 2提供了這樣的方法。

          修改CompanyAction,為它增加一個(gè)屬性ReturnUrl,用來(lái)保存處理完畢時(shí)要跳轉(zhuǎn)回去的頁(yè)面:
          private String returnUrl;  
            
          public String getReturnUrl() {  
              
          return returnUrl;  
          }  
            
          public void setReturnUrl(String returnUrl) {  
              
          this.returnUrl = returnUrl;  
          }

          修改struts.xml中saveCompany的配置,各個(gè)result指向的頁(yè)面從returnUrl參數(shù)取得:
          <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,為表單增加一個(gè)名為returnUrl的隱藏域,用于記錄returnUrl參數(shù)的值,并且在提交表單的時(shí)候?yàn)樵搮?shù)重新賦值:
          <s:hidden key="returnUrl" />

          最后修改companyList.jsp,為跳轉(zhuǎn)到companyForm.jsp的連接增加returnUrl參數(shù),參數(shù)值為companyList.jsp本身(即通知companyForm.jsp,處理完畢后還回到現(xiàn)在companyList.jsp頁(yè)面):
          <display:column property="id" sortable="true" href="editCompany.html?returnUrl=companyList.jsp" media="html"  
              paramId
          ="id" paramProperty="id" titleKey="company.id"/>

          發(fā)布修改后的工程并運(yùn)行,果然達(dá)到了預(yù)期的目的。

          要在其它頁(yè)面上提供公司信息編輯的入口,只要在該頁(yè)面上提供一個(gè)連接,連接地址為editCompany.html?returnUrl=<當(dāng)前頁(yè)面地址>即可。

          posted on 2008-03-13 21:14 雨奏 閱讀(3854) 評(píng)論(2)  編輯  收藏 所屬分類: Java

          FeedBack:
          # re: 使用Struts 2實(shí)現(xiàn)從哪個(gè)頁(yè)面轉(zhuǎn)進(jìn)來(lái),處理完畢后就轉(zhuǎn)到原來(lái)的頁(yè)面 2008-03-15 02:43 隔葉黃鶯
          很好很實(shí)用,頂一下  回復(fù)  更多評(píng)論
            
          # re: 使用Struts 2實(shí)現(xiàn)從哪個(gè)頁(yè)面轉(zhuǎn)進(jìn)來(lái),處理完畢后就轉(zhuǎn)到原來(lái)的頁(yè)面[未登錄] 2008-09-02 13:58 zy
          沒有baseaction的內(nèi)容阿  回復(fù)  更多評(píng)論
            
          主站蜘蛛池模板: 东乡族自治县| 蓬莱市| 长宁区| 揭阳市| 苏尼特左旗| 涿鹿县| 石门县| 西贡区| 邢台县| 绥江县| 桑植县| 十堰市| 绥中县| 佛教| 靖远县| 广宁县| 宕昌县| 揭东县| 天长市| 白城市| 长沙市| 眉山市| 伊春市| 鄂尔多斯市| 无锡市| 武强县| 拉萨市| 贵南县| 琼中| 海口市| 武陟县| 调兵山市| 正蓝旗| 利川市| 马鞍山市| 资中县| 乌恰县| 赤水市| 濉溪县| 连平县| 周至县|