关于Struts1之ActionMapping的示例详解
这篇文章主要介绍了Struts1教程之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中有一个
进入断点调试,首先在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[] = moduleConfigfindActionConfigs(); for (int i = 0; i < configslength; 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;
来看下ActionServlet中的一个方法processActionForm,当我们在截取字符串,再根据字符串取得ActionMapping(这是前两篇文章中介绍的)之后,我们就要用利用ActionMapping来创建ActionForm了,并且把ActionForm放到request或session中管理。
先来看具体struts中processActionForm方法的具体实现:
/** * <p>Retrieve and return the <code>ActionForm</code> associatedwith * this mapping, creating and retaining oneif necessary. If there is no * <code>ActionForm</code> associated with this mapping,return * <code>null</code>.</p> * * @param request The servlet request weare processing * @param response The servlet response weare creating * @param mapping The mapping we are using */ protectedActionForm processActionForm(HttpServletRequestrequest, HttpServletResponse response, ActionMapping mapping) { // Create (if necessary) a form bean to use ActionForm instance = RequestUtilscreateActionForm (request, mapping, moduleConfig, servlet); if (instance == null) { return (null); } // Store the new instance in the appropriate scope if (log.isDebugEnabled()) { log.debug(" Storing ActionForm bean instance in scope '" + mapping.getScope() + "' under attribute key '" + mapping.getAttribute() + "'"); } if ("request".equals(mapping.getScope())) { request.setAttribute(mapping.getAttribute(), instance); } else { HttpSession session =requestgetSession(); session.setAttribute(mapping.getAttribute(), instance); } return (instance); }
这个方法的大体流程是:根据ActionMapping中的name名称查找ActionForm,如果配置了ActionForm,那么就到request或session中查找,如果在request或session中存在已经创建的ActionForm,那么将返回。如果不存在那么会根据ActionForm的完成路径采用反射进行创建,再将创建好的ActionForm放到request或session中,之后返回ActionForm。
具体我们可以跟随断点调试来看看这个方法是如何运行的。
先设置断点,之后进入processActionForm方法。
第一个步骤就是创建ActionForm:
// Create (if necessary) a formbean to use ActionForm instance = RequestUtils.createActionForm (request, mapping, moduleConfig, servlet); if (instance == null) { return (null); }
通过调用RequestUtils.createActionForm的方法把ActionMapping中的ActionForm字符串生成对象,并且返回。进入这段代码中:
publicstaticActionForm createActionForm( HttpServletRequest request, ActionMapping mapping, ModuleConfig moduleConfig, ActionServlet servlet) { // Is there a form bean associated with this mapping? String attribute = mappinggetAttribute(); if (attribute == null) { return (null); } // Look up the form bean configuration information to use String name = mapping.getName(); FormBeanConfig config =moduleConfigfindFormBeanConfig(name); if (config == null) { log.warn("No FormBeanConfig found under '"+ name + "'"); return (null); } ActionForm instance = lookupActionForm(request,attribute, mappinggetScope()); // Can we recycle the existing form bean instance (if there is one)? try { if (instance != null && canReuseActionForm(instance,config)) { return (instance); } } catch(ClassNotFoundException e) { log.error(servlet.getInternal().getMessage("formBean",config.getType()), e); return (null); } return createActionForm(config,servlet); }
方法首先定义变量name,并且从mapping中获取值,String name = mapping.getName();也就是我们实例中的LoginForm字符串。之后通过调用FormBeanConfig config =moduleConfig.findFormBeanConfig(name);这句话把相应的LoginForm字符串生成相应的对象。
这里要说明的是我们在struts-config配置文件中,配置过这样一个标签信息:
<form-beans> <form-bean name="loginForm" type=".struts.LoginActionForm"/> </form-beans>
这个标签在服务器一启动的时候就会利用digester读取这里的配置信息,并且放在FormBeanConfig类中,这样我们可以通过上面那一句话就可以把LoginForm字符串生成相应的对象。
之后调用了ActionForm instance = lookupActionForm(request,attribute, mapping.getScope());这个方法,这个方法主要是查找scope属性中有没有存在ActionForm。具体实现:
if ("request".equals(scope)){ instance = (ActionForm)request.getAttribute(attribute); } else { session = request.getSession(); instance = (ActionForm)session.getAttribute(attribute); }
这里判断scope属性值是否为request,如果是则从request中读出ActionForm,如果不是则从session中读出。程序如果是第一次执行,那么ActionForm会是为空的。因为这里的ActionForm为空,所以就进入了if判断语句中,最后通过调用return createActionForm(config, servlet);创建ActionForm并且返回。
之后processActionForm就会把返回来的ActionForm放入request或者session中。具体实现就是:
if ("request".equals(mapping.getScope())){ request.setAttribute(mapping.getAttribute(), instance); } else { HttpSession session =request.getSession(); session.setAttribute(mapping.getAttribute(), instance); }
到此为止,ActionForm就创建完成,当ActionForm创建完成之后,就要用其他的方法来往ActionForm中赋值了
以上是关于Struts1之ActionMapping的示例详解的详细内容。更多信息请关注PHP中文网其他相关文章!

热AI工具

Undresser.AI Undress
人工智能驱动的应用程序,用于创建逼真的裸体照片

AI Clothes Remover
用于从照片中去除衣服的在线人工智能工具。

Undress AI Tool
免费脱衣服图片

Clothoff.io
AI脱衣机

AI Hentai Generator
免费生成ai无尽的。

热门文章

热工具

记事本++7.3.1
好用且免费的代码编辑器

SublimeText3汉化版
中文版,非常好用

禅工作室 13.0.1
功能强大的PHP集成开发环境

Dreamweaver CS6
视觉化网页开发工具

SublimeText3 Mac版
神级代码编辑软件(SublimeText3)

热门话题

C++中的众数函数详解在统计学中,众数指的是一组数据中出现次数最多的数值。在C++语言中,我们可以通过编写一个众数函数来找到任意一组数据中的众数。众数函数的实现可以采用多种不同的方法,下面将详细介绍其中两种常用的方法。第一种方法是使用哈希表来统计每个数字出现的次数。首先,我们需要定义一个哈希表,将每个数字作为键,出现次数作为值。然后,对于给定的数据集,我们遍

Windows操作系统是全球最流行的操作系统之一,其新版本Win11备受瞩目。在Win11系统中,管理员权限的获取是一个重要的操作,管理员权限可以让用户对系统进行更多的操作和设置。本文将详细介绍在Win11系统中如何获取管理员权限,以及如何有效地管理权限。在Win11系统中,管理员权限分为本地管理员和域管理员两种。本地管理员是指具有对本地计算机的完全管理权限

OracleSQL中的除法运算详解在OracleSQL中,除法运算是一种常见且重要的数学运算操作,用于计算两个数相除的结果。除法在数据库查询中经常用到,因此了解OracleSQL中的除法运算及其用法是数据库开发人员必备的技能之一。本文将详细讨论OracleSQL中除法运算的相关知识,并提供具体的代码示例供读者参考。一、OracleSQL中的除法运算

C++中的取余函数详解在C++中,取余运算符(%)用于计算两个数相除的余数。它是一种二元运算符,其操作数可以是任何整数类型(包括char、short、int、long等),也可以是浮点数类型(如float、double)。取余运算符返回的结果与被除数的符号相同。例如,对于整数的取余运算,我们可以使用以下代码来实现:inta=10;intb=3;

Vue.nextTick函数用法详解及在异步更新中的应用在Vue开发中,经常会遇到需要进行异步更新数据的情况,比如在修改DOM后需要立即更新数据或者在数据更新后需要立即进行相关操作。而Vue提供的.nextTick函数就是为了解决这类问题而出现的。本文就会详细介绍Vue.nextTick函数的用法,并结合代码示例来说明它在异步更新中的应用。一、Vue.nex

PHP-FPM是一种常用的PHP进程管理器,用于提供更好的PHP性能和稳定性。然而,在高负载环境下,PHP-FPM的默认配置可能无法满足需求,因此我们需要对其进行调优。本文将详细介绍PHP-FPM的调优方法,并给出一些代码示例。一、增加进程数默认情况下,PHP-FPM只启动少量的进程来处理请求。在高负载环境下,我们可以通过增加进程数来提高PHP-FPM的并发

PHP中的模运算符(%)是用来获取两个数值相除的余数的。在本文中,我们将详细讨论模运算符的作用及用法,并提供具体的代码示例来帮助读者更好地理解。1.模运算符的作用在数学中,当我们将一个整数除以另一个整数时,会得到一个商和一个余数。例如,当我们将10除以3时,商为3,余数为1。模运算符就是用来获取这个余数的。2.模运算符的用法在PHP中,使用%符号来表示模

Linux系统调用system()函数详解系统调用是Linux操作系统中非常重要的一部分,它提供了一种与系统内核进行交互的方式。其中,system()函数是一个常用的系统调用函数之一。本文将详细介绍system()函数的使用方法,并提供相应的代码示例。系统调用的基本概念系统调用是用户程序与操作系统内核交互的一种方式。用户程序通过调用系统调用函数来请求操作系统
