国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學院 > 開發設計 > 正文

Struts模塊化編程教程(三)

2019-11-18 15:11:48
字體:
來源:轉載
供稿:網友

  4、模塊定義 

通過上面對STRUTS的模塊化機制的講解,我們現在可以開始實現我們的模塊化例子程序了。 

4.1 Actionservlet參數 

我們在struts的web.xml中定義模塊。下面的代碼定義了三個模塊:缺省模塊,apPRoval和registration模塊,前綴分別是””,/approval和/registration。 

<web-app>
    <servlet>
        <servlet-name>action</servlet-name>
        <servlet-class>org.apache.struts.action.ActionServlet</servlet-class>
        <init-param>
            <param-name>config</param-name>
            <param-value>/WEB-INF/struts-config.xml</param-value>
                    </init-param>
        <init-param>
            <param-name>config/approval</param-name>
            <param-value>/WEB-INF/struts-config-approval.xml</param-value>
                    </init-param>
        <init-param>
            <param-name>config/registration</param-name>
            <param-value>/WEB-INF/struts-config-registration.xml</param-value>
        </init-param>
     </init-param>
         <load-on-startup>1</load-on-startup>
            </servlet>    <servlet-mapping>
        <servlet-name>action</servlet-name>
        <url-pattern>*.do</url-pattern>
            </servlet-mapping>
    </web-app>
 

這樣在初始化actionservlet的過程中,servletcontext的屬性中就會有這樣的屬性鍵/值關系:
Struts模塊化編程教程(三)

4.2 approval模塊配置文件 

下面是approval模塊的配置文件,定義了form和action,以及相應的forward。 

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts-config PUBLIC "-//Apache Software Foundation//
                              DTD Struts Configuration 1.1//EN" 
"http://jakarta.apache.org/struts/dtds/struts-config_1_1.dtd">
<struts-config>
        <form-beans>
        <form-bean name="approvalForm" type="com.i505.struts.approval.form.ApprovalForm">
         </form-bean>
            </form-beans>
       <action-mappings>
        <action
            attribute="approvalForm"
            name="approvalForm"
            input="/index.jsp"
            path="/approval"
            scope="request"
            type="com.i505.struts.approval.action.ApprovalAction">
            <forward name="sUCcess" contextRelative="false" path="/resultok.jsp" />
        </action>
    </action-mappings>
</struts-config>
 

4.3 registration模塊配置文件 

下面是registration模塊的配置文件,定義了form和action,以及相應的message-resources和forward。 

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts-config PUBLIC "-//Apache Software Foundation//
                              DTD Struts Configuration 1.1//EN" 
"http://jakarta.apache.org/struts/dtds/struts-config_1_1.dtd">
<struts-config>
    <form-beans>
        <form-bean name="registrationForm" type="com.i505.struts.registration.form.RegistrationForm" />
            </form-beans>
      <action-mappings>
        <action
            attribute="registrationForm"
            input="/index.jsp"
            name="registrationForm"
            path="/registration"
            type="com.i505.struts.registration.action.RegistrationAction">
             <forward name="success" path="/resultok.jsp" />
        </action>
    </action-mappings>
    <message-resources    parameter="com.i505.struts.applicationResources"/>
    </struts-config>
 

5、模塊選擇 

本節主要講述struts中如何選擇模塊,實現模塊的真正運作的。 

5.1 action的模塊選擇 

當我們在瀏覽器中使用http://hostaddress/contextpath/module/action.do式樣的的url時,actionservlet會根據module選擇模塊對象,下面是actionservlet處理http請求的代碼: 

protected void process(HttpServletRequest request,
                           HttpServletResponse response)
        throws IOException, ServletException {
        RequestUtils.selectModule(request, getServletContext());
       getRequestProcessor(getModuleConfig(request)).process
            (request, response);
    }
 

RequestUtils.selectModule函數將使用下面的代碼把url中的模塊前綴(下面代碼的prefix將代表上面url式樣中的/module)指定的模塊對象保存在request屬性中,這個模塊對象就成了處理這個請求的當前模塊對象: 

// EXPose the resources for this module
        ModuleConfig config = (ModuleConfig)
 context.getAttribute(Globals.MODULE_KEY + prefix);
        if (config != null) {
            request.setAttribute(Globals.MODULE_KEY, config);
        }
 else {
            request.removeAttribute(Globals.MODULE_KEY);
        }
 

5.2 資源的模塊化 

資源(比如jsp)的模塊化是指資源可以按照模塊一樣來組織,比如approval模塊的資源可以放在approval目錄下,而registration模塊的資源則放在registration目錄下,缺省模塊的資源放在webroot下。 

url訪問這些資源很簡單,url式樣是 http://hostaddress/contextpath/module/xxx.jsp。對于input和forward訪問這些資源,我們只需直接寫相對于模塊路徑下的路徑,注重它們必須以”/”開頭。假如forward是相對servletcontext的,則要加上模塊路徑。 

<action-mappings>
        <action
            attribute="registrationForm"
            input="/index.jsp"
            name="registrationForm"
            path="/registration"
            type="com.i505.struts.registration.action.RegistrationAction">
             <forward name="success" path="/resultok.jsp" />
        </action>
    </action-mappings>
 

5.3 Formtag中表單action url的生成 

對于模塊編程,struts在formtag的action屬性似乎有些問題,這些問題出現在struts沒有考慮直接訪問jsp時的情況。應為forward和直接訪問這兩種環境是不同的,主要是直接訪問這些JSP,request屬性中沒有模塊對象,而forward訪問這些jsp時request屬性中有模塊對象。我們需要修改代碼,使得在產生action屬性時不受jsp所在環境的影響,也就是我們將在formtag的action屬性中指定模塊,而不是request中得到模塊。下面是registration模塊的index.jsp的代碼,它的formtag的action屬性包括了模塊的前綴/registration: 

<%@ taglib uri="/WEB-INF/struts-bean.tld" prefix="bean"%>
 <%@ taglib uri="/WEB-INF/struts-Html.tld" prefix="html"%>
 <head>
<title>申請注冊</title>
<%@ page contentType="text/html;charset=GB2312" %>
 </head>
<body>
<html:form action="/registration/registration.do" >
姓名:<html:text property="name" /><html:errors property="name"/><br /><br />
年齡:<html:text property="age" /><html:errors property="age"/><br /><br />
<html:submit />
</html:form>
</body>
</html>
 

下面我們來修改struts的相關代碼達到這個效果。 

5.3.1 Formtag 

Formtag的setAction將識別form tag的acton屬性的module前綴,并分離出真正的模塊相對的action路徑,lookup將直接從ServletContext中獲取模塊配置對象。 

private String getActionPath(String action) {
String temp = action.trim();
String x;
         int pos=0;
if(!temp.startsWith("/")) temp = "/"+ temp;
pos = temp.indexOf("/", 1);
if(pos<=0) return action;

                  return temp.substring(pos); }
private String getModulePrefix(String action) {
String result;
int pos;
String temp=action.trim();
if(!temp.startsWith("/")) {
temp= "/"+temp;
}
pos = temp.indexOf("/", 1);
if(pos<=1) return "";
else
  return temp.substring(0, pos);
}
public void setAction(String action)
 {this.modulePrefix = this.getModulePrefix(action);
this.action = this.getActionPath(action);
    }
protected void lookup() throws JspException {
//我們直接從ServletContext中獲取模塊配置對象
moduleConfig = (ModuleConfig)
 pageContext.getServletContext().getAttribute(Globals.MODULE_KEY + modulePrefix);
…}
     rotected String renderFormStartElement() {
        HttpServletResponse response =
            (HttpServletResponse) this.pageContext.getResponse();
                    StringBuffer results = new StringBuffer("<form");
        results.append(" name=/"");
        results.append(beanName);
        results.append("/""); 
       results.append(" method=/"");
        results.append(method == null ? "post" : method);
        results.append("/" action=/"");
//我們的action已經去掉了modulePrefix,所以我們得重新加上
       results.append(
            response.encodeURL(
                RequestUtils.getActionMappingURL(this.modulePrefix+ this.action, this.pageContext)));
         …
}
 

5.3.2 Requestutils 

Requestutils的getActionMappingURL主要用作附加servletcontext 路徑,因為我們現在在action參數附加了modulePrefix路徑,所以沒必要再追加模塊前綴。 

public static String getActionMappingURL(String action, PageContext pageContext)
 {
        HttpServletRequest request = (HttpServletRequest) pageContext.getRequest();
        StringBuffer value = new StringBuffer(request.getContextPath());
        ModuleConfig config =
            (ModuleConfig) pageContext.getRequest().getAttribute(Globals.MODULE_KEY);
//我們jsp中的formtag的action屬性已經表示了模塊,所以我們不能再追加模塊名//
 if (config != null) { 
      //
     value.append(config.getPrefix());
       // }
        // Use our servlet mapping, if one is specified
        String servletMapping =
            (String) pageContext.getAttribute(Globals.SERVLET_KEY,
 PageContext.APPLICATION_SCOPE);
        if (servletMapping != null) {
            String queryString = null;
            int question = action.indexOf("?");
            if (question >= 0) {
                queryString = action.substring(question);
            }
            String actionMapping = getActionMappingName(action);
            if (servletMapping.startsWith("*.")) {
                value.append(actionMapping);
                value.append(servletMapping.substring(1));
            } else if (servletMapping.endsWith("/*")) {
                value.append(servletMapping.substring(0, servletMapping.length() - 2));
                value.append(actionMapping);
            } else if (servletMapping.equals("/")) {
                value.append(actionMapping);
            }
            if (queryString != null) {
                value.append(queryString);
            }
        }
        else {
            if (!action.startsWith("/")) {
                value.append("/");
            }
            value.append(action);
        }
        // Return the completed value
        return (value.toString());
     }
 

6、總結 

模塊化編程有利于提高編程效率,但是struts中的模塊化支持有些小問題,本文具體分析了struts支持模塊化編程的機制,并作了些修改,希望對大家有幫助。另外假如我們可以把其改進為模塊化的相關的東西可以打成一個包進行動態部署(比如approval.mar)的話,那將會更加有用。 

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 蓬莱市| 滦南县| 岳池县| 卢湾区| 留坝县| 山阴县| 佳木斯市| 依兰县| 陆良县| 明光市| 彩票| 商都县| 黎平县| 斗六市| 达拉特旗| 兰溪市| 翼城县| 饶河县| 华亭县| 博罗县| 莱阳市| 察隅县| 襄城县| 永善县| 兰考县| 西乌| 镶黄旗| 宿迁市| 宜宾市| 同心县| 张家口市| 宁阳县| 荆门市| 黔西县| 沙坪坝区| 靖远县| 桂阳县| 阳春市| 博客| 无棣县| 平顶山市|