当前位置: 移动技术网 > IT编程>开发语言>Java > Struts1教程之ActionMapping_动力节点Java学院整理

Struts1教程之ActionMapping_动力节点Java学院整理

2019年07月19日  | 移动技术网IT编程  | 我要评论

首先断点走出了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[] = 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中赋值了

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网