您的位置:首页 > 编程语言 > Java开发

深入浅出学习Struts框架(十)-分析Struts框架实例5

2012-04-18 15:47 537 查看
前几篇博客主要介绍的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。敬请期待!
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: