隨筆 - 5  文章 - 17  trackbacks - 0
          <2008年3月>
          2425262728291
          2345678
          9101112131415
          16171819202122
          23242526272829
          303112345

          常用鏈接

          留言簿(3)

          隨筆分類

          隨筆檔案

          搜索

          •  

          最新評論

          閱讀排行榜

          評論排行榜

          舉一個例子:工程中有一個公司列表頁面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 雨奏 閱讀(3854) 評論(2)  編輯  收藏 所屬分類: Java

          FeedBack:
          # re: 使用Struts 2實現從哪個頁面轉進來,處理完畢后就轉到原來的頁面 2008-03-15 02:43 隔葉黃鶯
          很好很實用,頂一下  回復  更多評論
            
          # re: 使用Struts 2實現從哪個頁面轉進來,處理完畢后就轉到原來的頁面[未登錄] 2008-09-02 13:58 zy
          沒有baseaction的內容阿  回復  更多評論
            
          主站蜘蛛池模板: 阳信县| 永康市| 明溪县| 应用必备| 阿瓦提县| 响水县| 天镇县| 华容县| 乌海市| 绵竹市| 太原市| 德安县| 万盛区| 尼木县| 林周县| 江达县| 宁强县| 道孚县| 易门县| 莎车县| 湄潭县| 龙里县| 茶陵县| 上饶县| 浠水县| 克什克腾旗| 东阿县| 高邮市| 林州市| 清流县| 南皮县| 临高县| 鸡东县| 金昌市| 望江县| 罗甸县| 新闻| 青州市| 张掖市| 依兰县| 清丰县|