当前位置:编程学习 > JSP >>

Struts模块化编程教程(三)

答案: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的属性中就会有这样的属性键/值关系:

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);
    }
&nb

上一个:Struts中html:options的使用
下一个:Struts模块化编程教程(二)

CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,