网站首页
JSP空间
动态资讯
开源项目
技术文档
资源下载
J2EE资源
客户论坛
在线支付
 
  技术文档>>JAVA>>新手入门>>基础入门>查看文档  
  struts模块化编程经典实战教程(三)     
  文章作者:未知  文章来源:水木森林  
  查看:73次  录入:管理员--2007-11-17  
 
  4、模块定义
  通过上面对struts的模块化机制的讲解,我们现在可以开始实现我们的模块化例子程序了。
  
  4.1 actionservlet参数
  
  我们在struts的web.xml中定义模块。下面的代码定义了三个模块:缺省模块,approval和registration模块,前缀分别是””,/approval和/registration。
  
  
  
    action
    org.apache.struts.action.actionservlet
    
      config
      /web-inf/struts-config.xml
          

    
      config/approval
      /web-inf/struts-config-approval.xml
          

    
      config/registration
      /web-inf/struts-config-registration.xml
    

  
     1
      
  
    action
    *.do
      

  

  
  这样在初始化actionservlet的过程中,servletcontext的属性中就会有这样的属性键/值关系:
  
struts模块化编程经典实战教程(三)

  4.2 approval模块配置文件
  
  下面是approval模块的配置文件,定义了form和action,以及相应的forward。
  
  
                 dtd struts configuration 1.1//en"
  "http://jakarta.apache.org/struts/dtds/struts-config_1_1.dtd">
  
    
    
    

      

   
          attribute="approvalform"
      name="approvalform"
      input="/index.jsp"
      path="/approval"
      scope="request"
      type="com.i505.struts.approval.action.approvalaction">
      
    
  

  

  
  4.3 registration模块配置文件
  
  下面是registration模块的配置文件,定义了form和action,以及相应的message-resources和forward。
  
  
                 dtd struts configuration 1.1//en"
  "http://jakarta.apache.org/struts/dtds/struts-config_1_1.dtd">
  
  
    
      

   
          attribute="registrationform"
      input="/index.jsp"
      name="registrationform"
      path="/registration"
      type="com.i505.struts.registration.action.registrationaction">
      
    
  

  
  

  
  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的,则要加上模块路径。
  
  
          attribute="registrationform"
      input="/index.jsp"
      name="registrationform"
      path="/registration"
      type="com.i505.struts.registration.action.registrationaction">
      
    
  

  
  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"%>
  
  申请注册
  <%@ page contenttype="text/html;charset=gb2312" %>
  
  
  
   姓名:


   年龄:


  
  

  
  
  
  下面我们来修改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
    re
 
 
上一篇: struts模块化编程经典实战教程(二)    下一篇: 利用 struts 实现动态控制表格大小(1)
  相关文档
log4j--新的日志操作方法 11-17
对比c++和java 11-17
2005 jdj readers choice awards - best java book nominees: 11-17
struts 用户指南(一) 11-17
什么是xsp? 11-17
网上收集的部分java资料 11-17
java io 包中的decorator模式 11-16
留言板 11-17
the alloy look and feel 1.4.4破解(图) 11-17
java网络编程之uri、url研究专题四 11-17
java规则引擎的工作原理及其实际应用 11-16
有感java is not platform-independent, it is the platform 11-17
基于java技术的搜索引擎的研究与实现 11-17
使用模仿对象进行单元测试 11-17
实现java程序跨平台运行的12点注意事项 11-16
简析java的xml编程(to:初学者们) 11-17
jdk 6.0 source snapshot releases 11-17
巧用java程序把word转换成html文件 11-17
swing读书笔记 11-17
拷贝文件的源代码 11-16
返回首页 | 关于我们 | J网章程 | JSP空间合租 | 客服中心 | 免责声明 | 常见问题 | 参观机房
本站主机空间代理至厦门市华众网络科技有限公司
《中华人民共和国增值电信业务经营许可证》
编号:闽B2-20050079
@2005-2008福建JSP技术网 版权所有 闽ICP备05000928号
技术电话:13616026886
邮箱:admin@fjjsp.com 站长QQ,点击这里给我发消息