动态网站制作指南 [  QQ表情  ]
[ 投票调查 ]
[ 企业邮箱 ]
[ 网站空间 ]
网络编程 | 站长之家 | 网页制作 | 图形图象 | 操作系统 | 冲浪宝典 | 软件教学 | 网络办公 | 邮件系统 | 网络安全 | 认证考试 | 系统进程
ASP源码 | .Net源码 | PHP源码 | JSP源码 | JAVA源码 | CGI源码 | VB源码 | C++源码 | Delphi源码 | PB源码 | VF源码 | 汇编 | 服务器
Firefox | IE | Maxthon | 迅雷 | 电驴 | BitComet | FlashGet | QQ | QQ空间 | Vista | 输入法 | Ghost | Word | Excel | wps | Powerpoint
asp | .net | php | jsp | Sql | c# | Ajax | xml | Dreamweaver | FrontPages | Javascript | css | photoshop | fireworks | Flash | Cad | Discuz!
当前位置 > 网站建设学院 > 网络编程 > Java教程
Tag:注入,存储过程,分页,安全,优化,xmlhttp,fso,jmail,application,session,防盗链,stream,无组件,组件,md5,乱码,缓存,加密,验证码,算法,cookies,ubb,正则表达式,水印,索引,日志,压缩,base64,url重写,上传,控件,Web.config,JDBC,函数,内存,PDF,迁移,结构,破解,编译,配置,进程,分词,IIS,Apache,Tomcat,phpmyadmin,Gzip,触发器,socket
网络编程:ASP教程,ASP.NET教程,PHP教程,JSP教程,C#教程,数据库,XML教程,Ajax,Java,Perl,Shell,VB教程,Delphi,C/C++教程,软件工程,J2EE/J2ME,移动开发
文章搜索服务
邮件订阅
输入你的邮件地址,
你将不会错过任何关于:
[ Java教程 ]的信息

本月文章推荐
.Java IO学习基础之读写文本文件.
.开发框架:Java编程中Spring的一.
.Solaris下NFS使用手册.
.对 Robocode 的创始人 Mat Nelso.
.深入研究Servlet线程安全性问题.
.JAVA 的interface观念 与C++ 多重.
.针对.NET开发人员的存储过程评估.
.JAVA30个基本知识.
.对Spring中接口注入的理解实例分.
.italics 方法.
.Java嵌入式开发之j2me--一.
.调整JavaTM 的I/O性能(一)(zt).
.java连接sqlserver实例.
.用ChoiceFormat将数字与字符串联.
.length 属性 (String).
.开发线程安全的SpringWeb应用.
.WebLogic 7.0平台:灵活适应环境.
.Java嵌入式开发讲座 第三讲.
.iReport整合向量图形的使用心得.
.IBM发布AUS: 消除Java程序脆弱的.

Struts模块化编程教程(三)

发表日期:2008-1-5 |



  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)的话,那将会更加有用。 
上一篇:Struts模块化编程教程(二) 人气:467
下一篇:struts傻瓜式学习(一天篇) 人气:542
浏览全部Java的内容 Dreamweaver插件下载 常用网页广告代码全集
  最新网站源码 最新软件下载
2008-12-2 OpenPNE中文 v2.12.5 for win 中
2008-12-2 谷秋精品课程软件课程版 v2.3
2008-12-2 晴天电影系统(带一键迅雷/自定义
2008-12-2 QQip138闪字程序
2008-12-2 SmartWeb企业智能建站系统 v1.0.2
2008-12-2 梦想不死个人主页 v2009
2008-12-2 开良ASP小偷程序生成器 v1.1
2008-12-2 toolxp.cnalexa世界排名查询 php
2008-12-2 腾讯留言板 v1.3
2008-11-29 Tencent Traveler 4.4
2008-11-29 龙卷风网络收音机 v3.0.0.0
2008-11-29 Intel Chipset Software Install
2008-11-29 TweakVI 1.0 Build 1100
2008-11-29 Opera 9.62 Build 10469
2008-11-29 MPlayer WW编译版 SVN-r28044(20
2008-11-29 NetTools网络工具v1.0.0破解版
2008-11-29 3DGallery三维体验1.1破解版
2008-11-29 SecretBook保密本v1.0破解版
  发表评论
姓 名: 验证码:
内 容:
站长工具:网站收录查询 | Google PR查询 | ALEXA排名查询 | CSS在线编辑器 | OPEN参数生成器 | 弹出式窗口代码产生器 | 密码登录生成器 | 在线按钮生成器 | Meta标签生成器 | 多色彩特效字代码生成器 | 网页代码调试器 | 在线FTP登陆 | Flash取色器 | 配色代码对照表 | 配色辞典 | CSS生成器 | 广告代码 | 框架网页代码生成器 | js/vbs加密 | md5加密 | 进制转换 | UTF-8 转换工具 | 在线调色板 | Html转换js | Html转换asp | Html转换php | Html转换perl
实用工具:汉字翻译拼音 | 拼音字典 | 符号对照表 | 个税计算 | 实时汇率查询换算 | 经典小工具 | 汉字简繁转换 | 普通单位换算 | 公制单位换算 | 生辰老黄历 | 国内电话区号 | 国家代码与域名缩写 | 文字加密解密 | 元素周期表 | 健康查询 | 世界时间 | 万年历 | 二十四节气 | 汉字横竖排版 | 手机号码查询 | 计算器 | ip搜索
业务联系 | 广告刊登 | 频道合作 | 投稿荐稿 | 联系方式 | 加入收藏 | RSS订阅
Copyright © 2000-2009 www.knowsky.com All rights reserved | 沪ICP备05001343号
ホームページ制作 不動産検索システム 求人情報
防水工事·改修工事 フットサル大会 探偵
SEO対策 中国語教室 ホームページ作成