dedecms织梦内容管理系统    
首页 | java | C/C++ | PHP | 操作系统 | ajax | 脚本编程 | 安全技术 | 本站下载页 | flex | CRM | 专题 | QQ群 | 测试中心 | 会员中心 | 积分规则
  当前位置:主页>java>开源框架>文章内容
Struts教程-Struts模块化编程教程
来源: 作者: 出处:www.100jq.com
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)的话,那将会更加有用

欢迎加入本站QQ群参与讨论


共3页: 上一页 [1] [2] 3 下一页
上一篇:JAVA中配置环境变量设置方法大全   下一篇:Struts入门经验
[收藏] [推荐] [评论(0条)] [返回顶部] [打印本页] [关闭窗口]  
用户名: 新注册) 密码: 匿名评论
评论内容:(不能超过250字,需审核后才会公布,请自觉遵守互联网相关政策法规。
 §最新评论
  热点文章
·关于JSF和Struts的讨论
·Struts入门经验
·用科学的思维方法指导软件的设计
·Hibernate配置文件中映射元素详
·Spring中事件处理的小技巧
·struts2.0pring2.0 hibernate3.2
·struts2.0 spring2.0 hibernate3
·浅谈hibernate lazy fetch
·Hibernate的Fetch
·优化hibernate性能的几点建议
·Hibernate中的取策略延迟加载
·Hibernate中outer-join、lazy 、
  相关文章
·Struts入门经验
·关于JSF和Struts的讨论
·用科学的思维方法指导软件的设计
·Hibernate配置文件中映射元素详
·Spring中事件处理的小技巧
·struts2.0pring2.0 hibernate3.2
·struts2.0 spring2.0 hibernate3
·浅谈hibernate lazy fetch
·Hibernate的Fetch
·优化hibernate性能的几点建议
·Hibernate中的取策略延迟加载
·Hibernate中outer-join、lazy 、
  相关信息
copy right @ 百家拳软件项目研究室 2007 辽ICP备07011763