前几篇博客主要介绍的ActionServlet的初始化和Struts是如何完成截取字符串工作的,今天继续分析Struts截取完字符串所要做的工作。
在struts专栏的开篇mvc小实例中我们编写了ActionMapping这样一个类、struts-config.xml配置文件,在那时我们对截取的字符串匹配,利用dom4j来读取了配置文件的信息,并且把他以ActionMapping的形式保存在内存中。
今天我们深入的来看看struts是怎样拿到ActionMapping的,依旧和上篇的博客思路一样,利用断点调试的方式来进入源代码中,具体做法见上篇博客。
紧接着上篇博客,我们的断点走出了processpath方法,
在上一篇博客中我们已经讲解了,这个方法是用来截取字符串的,今天我们来看怎样获得ActionMapping的方法---processMapping。
在此之前简单说一下ActionMapping,它的源代码中可以看出,其中最重要的属性和我们的mvc小实例中的ActionMapping类似,都是有path、type还有forwardMap,主要是对应的struts-config配置文件而来,这个就是保存这个配置文件的信息到内存中。
具体的mvc小实例的ActionMapping代码如下:
package com.cjq.servlet;
import java.util.Map;
public class ActionMapping {
private String path;
private Object type;
private Map forwardMap;
public String getPath() {
return path;
}
public void setPath(String path) {
this.path = path;
}
public Object getType() {
return type;
}
public void setType(Object type) {
this.type = type;
}
public Map getForwardMap() {
return forwardMap;
}
public void setForwardMap(Map forwardMap) {
this.forwardMap = forwardMap;
}
}
而Struts中的Actionconfig(因为ActionMapping是继承这个ActionConfig的,所以我们来看ActionConfig更加直接)的代码如下:
从这两部分代码来看,更加印证了我在开篇写的mvc小实例是一个struts框架的雏形。
讲完ActionMapping的一些内容后,相信对ActionMapping有所了解,那么系统是如何生成ActionMapping和如何找到ActionMapping的呢?这就是今天要说的整体:
不知道读者还记不记得,我们在分析实例的第一篇的时候web.xml中有一个<load-on-startup>2</load-on-startup> 配置信息,这个信息就是说明了但服务器已启动就动态读取struts-config配置文件把配置文件的信息put到ActionMapping中。所以当我们运行服务器的时候,我们在内存中已经存在对应struts-config配置文件信息对应的ActionMapping。今天就是要通过processMapping读取这个ActionMapping类。
进入断点调试,首先在processMapping方法上设置断点。
进入源代码中:
/**
* <p>Select the mapping used to process theselection path for this request.
* If no mapping can be identified, createan error response and return
* <code>null</code>.</p>
*
* @param request The servlet request weare processing
* @param response The servlet response weare creating
* @param path The portion of the requestURI for selecting a mapping
*
* @exception IOException if an input/outputerror occurs
*/
protectedActionMapping processMapping(HttpServletRequestrequest,
HttpServletResponse response,
String path)
throws IOException {
// Is there a mapping for this path?
ActionMapping mapping = (ActionMapping)
moduleConfig.findActionConfig(path);
// If a mapping is found, put it in the request and return it
if (mapping != null) {
request.setAttribute(Globals.MAPPING_KEY, mapping);
return (mapping);
}
// Locate the mapping for unknown paths (if any)
ActionConfig configs[] = moduleConfig.findActionConfigs();
for (int i = 0; i < configs.length; i++) {
if (configs[i].getUnknown()) {
mapping = (ActionMapping)configs[i];
request.setAttribute(Globals.MAPPING_KEY, mapping);
return (mapping);
}
}
// No mapping can be found to process this request
String msg = getInternal().getMessage("processInvalid");
log.error(msg + " " + path);
response.sendError(HttpServletResponse.SC_NOT_FOUND, msg);
return null;
}
首先我们传入我们在上一步截取的路径,通过moduleConfig的findAction方法来查找ActionConfig,并且返回ActionMapping。具体代码是:
ActionMapping mapping =(ActionMapping)
moduleConfig.findActionConfig(path);
如果找到,那么就讲ActionMapping存放到request的context中。代码:
if (mapping != null) {
request.setAttribute(Globals.MAPPING_KEY, mapping);
return (mapping);
}
如果没有通过path找到mapping,则在Actionconfig中遍历为未知路径寻找mapping,如果找到则存放到request中,如果没有找到,则返回错误信息,具体代码如下:
// Locate the mapping for unknownpaths (if any)
ActionConfig configs[] = moduleConfig.findActionConfigs();
for (int i = 0; i < configs.length; i++) {
if (configs[i].getUnknown()) {
mapping = (ActionMapping)configs[i];
request.setAttribute(Globals.MAPPING_KEY, mapping);
return (mapping);
}
}
// No mapping can be found to process this request
String msg = getInternal().getMessage("processInvalid");
log.error(msg + " " + path);
response.sendError(HttpServletResponse.SC_NOT_FOUND, msg);
return null;
通过这一段代码我们就能通过截取的字符串来找到匹配的ActionMapping,并且为下面获取ActionForm做铺
垫。这篇博客主要是让读者明白,Struts是如何生成ActionMapping和如何获得ActionMapping的,下一篇博客要介绍如何获得ActionForm。敬请期待!
分享到:
相关推荐
1 Struts框架 3 1.1 Struts压缩包内容 3 1.2 Struts体系结构 4 1.3 Struts框架中的组件 5 1.3.1 Struts配置文件 5 1.3.2 ActionServlet类 8 1.3.3 ActionMapping类 10 1.3.4 Action类 10 1.3.5 ActionForm类 11 ...
主要介绍了Struts1教程之ActionMapping,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
Struts简介 什么是Struts Struts基本运作流程 ActionMapping类 Action类 ActionForm类 ActionError与ActionMessage 协同开发 模块化程序 Struts异常处理 Struts国际化支持 PlugIn接口 等等
4 import org.apache.struts.action.ActionMapping; 5 6 /** 7 * MyEclipse Struts 8 * Creation date: 11-10-2005 9 * 10 * XDoclet definition: 11 * @struts.form name="LoginForm" 12 */ 13 public ...
import org.apache.struts.action.ActionForm;import org.apache.struts.action.ActionForward;import org.apache.struts.action.ActionMapping;import org.apache.struts.actions.DispatchAction等缺少
书名:Struts中文手册[文字版][中文](电子书) 格式:PDF 1. Struts 框架1 1.1. Struts 压缩包内容.1 1.2. Struts 体系结构1 1.2.1. 模型.2 1.2.2. 视窗.2 1.2.3. 控制器.2 1.3. Struts 框架中的组件.3 1.3.1. ...
Struts框架 1. struts压缩包内容………………………………………………………………………3 2. struts体系结构(模型-视窗-控制器)…………………………………………………3 3. struts框架的组件…………………………...
自己写的第一个structs 示例 体会到sturts的工作流程--IE.request-- ActionServlet.struts-config.xml Action --ActionMapping(ActionForm Bean); 继续向大家学习!
11.1 Validator框架和Struts客户化标签 14 11.1 在Validator框架中使用JavaScript 14 11.1 在struts中用validator作服务器端验证 14 第12章 Struts HTML标签库 14 12.1 Struts中基本的HTML标签 14 12.1 Struts中表单...
书名:Struts in Action[文字版][中文](电子书) 格式:PDF 本书的目的是帮助Web应用开发者能够最好的使用 Struts web 应用框架。 Struts是一个开源软件,有助于开发者更加快速和容易地建立Web应用程序。Struts依靠...
Struts原理、开发及项目实施 Holen 2002-9-12 <br/>1、 摘要 2、 关键词 3、 Framework 4、 Struts的起源 5、 Struts工作原理 6、 Struts安装 7、 一个实例 8、 Struts优缺点...
struts1.x的例子,struts2.x可以参考自己修改 1.action的写法 import java.io.*; import java.sql.*; import java.util.ArrayList; import javax.servlet.http.HttpServletRequest; import javax.servlet....
在Struts framework中, Controller主要是ActionServlet,但是对于业务逻辑的操作则主要由Action、ActionMapping、ActionForward这几个组件协调完成(也许这几个组件,应该划分到模型中的业务逻辑一块)。...
ActionForward and ActionMapping
struts1的几个核心组件是值得我们注意的: 1 、ActionServlet(核心控制器)。 2、RequestProcessor类(处理异常的核心组件)。 3、ActionForm(接收页面中传过的数据)。 4、Action(是控制器,主要是从ActionForm中...
【项目实战案例】java校园订餐系统项目(web端) /* * Generated by MyEclipse Struts * Template path: templates/java/JavaClass.vtl ...import org.apache.struts.action.ActionMapping; import com.bean.HzpBean;
The struts-config.xml configuration information is translated into a set of ActionMapping, which are put into container of ActionMappings. (If you have not noticed it, classes that end with s are ...
Struts+Spring+Hibernate实现上传下载 本文将围绕SSH文件上传下载的主题,向您详细讲述如何开发基于SSH的Web程序。SSH各框架的均为当前最新版本: •Struts 1.2 •Spring 1.2.5 •Hibernate 3.0 本文...
ActionMapping是ActionConfig的子类,实质上是对struts-config.xml的一个映射,从中可以取得所有的配置信息RequestProcessor根据提交过来的url,如*.do,从ActionMapping中得到相应的ActionForn和Action。...
第13章 Struts和Hibernate实例——两个与登录有关的实例 166 13.1 Struts和Hibernate的开发环境配置 166 13.1.1 数据库的安装和管理 166 13.1.2 Hibernate的安装 168 13.1.3 Struts的安装 169 13.2 实例一...