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

          常用鏈接

          留言簿(3)

          隨筆分類

          隨筆檔案

          搜索

          •  

          最新評論

          閱讀排行榜

          評論排行榜

          舉一個例子:工程中有一個公司列表頁面companyList.jsp、一個公司信息編輯頁面companyForm.jsp,一個action CompanyAction。在公司列表頁面上點擊某個公司的Id字段就跳轉(zhuǎ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,實際上就是companyForm.jsp,Struts 2會進(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)在有一個customerForm.jsp,要在該頁面上提供編輯公司信息的入口,編輯完畢并保存后返回到customerForm.jsp。這個要求可以擴展為“從哪個頁面轉(zhuǎn)進(jìn)來,處理完畢后就轉(zhuǎn)到原來的頁面”,這該如何實現(xiàn)呢?

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

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

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

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

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

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

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

          FeedBack:
          # re: 使用Struts 2實現(xiàn)從哪個頁面轉(zhuǎn)進(jìn)來,處理完畢后就轉(zhuǎn)到原來的頁面 2008-03-15 02:43 隔葉黃鶯
          很好很實用,頂一下  回復(fù)  更多評論
            
          # re: 使用Struts 2實現(xiàn)從哪個頁面轉(zhuǎn)進(jìn)來,處理完畢后就轉(zhuǎn)到原來的頁面[未登錄] 2008-09-02 13:58 zy
          沒有baseaction的內(nèi)容阿  回復(fù)  更多評論
            
          主站蜘蛛池模板: 石渠县| 卓尼县| 越西县| 当雄县| 体育| 阳谷县| 托里县| 文登市| 兴和县| 潍坊市| 潮州市| 永平县| 农安县| 宝清县| 大余县| 武城县| 鹤壁市| 新干县| 绍兴县| 抚远县| 冀州市| 洮南市| 准格尔旗| 清苑县| 迭部县| 南召县| 安达市| 陇西县| 洞口县| 长治市| 新野县| 平和县| 临洮县| 大丰市| 白朗县| 孝义市| 肥西县| 县级市| 抚远县| 宜兴市| 卫辉市|