Heim > Java > javaLernprogramm > Hauptteil

Beispielerklärung für Struts1 ActionMapping

巴扎黑
Freigeben: 2017-09-06 09:34:42
Original
1596 Leute haben es durchsucht

Dieser Artikel stellt hauptsächlich das ActionMapping des Struts1-Tutorials vor. Der Herausgeber findet es ziemlich gut, ich werde es jetzt mit Ihnen teilen und als Referenz geben. Folgen wir dem Editor und werfen wir einen Blick darauf.

Zunächst liegt der Haltepunkt außerhalb der Prozesspfadmethode

Diese Methode dient zum Abfangen von Zeichenfolgen. Heute werden wir uns ansehen, wie man die ActionMapping-Methode erhält ---processMapping.

Lassen Sie uns vorher kurz über ActionMapping sprechen. Aus dem Quellcode geht hervor, dass die wichtigsten Attribute dem ActionMapping in unserer kleinen MVC-Instanz ähneln Es stammt aus der entsprechenden Struts-Config-Konfigurationsdatei. Dies dient dazu, die Informationen dieser Konfigurationsdatei im Speicher zu speichern.

Der ActionMapping-Code der spezifischen MVC-Kleininstanz lautet wie folgt:


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; 
  } 
}
Nach dem Login kopieren

Und die Actionconfig in Struts (da ActionMapping diese ActionConfig erbt, also wir Es ist einfacher, sich ActionConfig anzusehen) Der Code lautet wie folgt:

Aus diesen beiden Teilen geht hervor, dass der Code weiter bestätigt, dass das kleine MVC-Beispiel, das ich zu Beginn geschrieben habe, der Prototyp eines Struts-Frameworks ist.

Nachdem ich über einige Inhalte von ActionMapping gesprochen habe, glaube ich, dass ich ein gewisses Verständnis für ActionMapping habe. Wie generiert das System also ActionMapping und wie findet man ActionMapping? Das ist das Ganze, worüber ich heute sprechen möchte:

Werfen wir einen Blick auf die Konfigurationsinformationen von 2 Die Struts-Config-Konfigurationsdatei wird dynamisch gelesen und die Informationen in der Konfigurationsdatei werden in ActionMapping eingegeben. Wenn wir also den Server ausführen, haben wir bereits das ActionMapping, das den Informationen der Struts-Config-Konfigurationsdatei entspricht, im Speicher. Heute werden wir diese ActionMapping-Klasse über ProcessMapping lesen.

Um das Breakpoint-Debugging zu starten, legen Sie zunächst einen Breakpoint für die Methode „processMapping“ fest.

Geben Sie den Quellcode ein:


/** 
   * <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; 
  }
Nach dem Login kopieren

Zuerst geben wir ein, was wir im abgefangen haben Vorheriger Schritt Pfad, finden Sie ActionConfig über die findAction-Methode von moduleConfig und geben Sie ActionMapping zurück. Der spezifische Code lautet:


ActionMapping mapping =(ActionMapping) 
   moduleConfig.findActionConfig(path);
Nach dem Login kopieren

Wenn gefunden, wird das ActionMapping im Kontext der Anfrage gespeichert. Code:


if (mapping != null) { 
      request.setAttribute(Globals.MAPPING_KEY, mapping); 
      return (mapping); 
    }
Nach dem Login kopieren

Wenn die Zuordnung nicht über den Pfad gefunden wird, durchsuchen Sie die Actionconfig, um die Zuordnung für den unbekannten Pfad zu finden, und speichern Sie sie, wenn sie gefunden wird Wenn es nicht gefunden wird, wird eine Fehlermeldung wie folgt zurückgegeben:


// 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;
Nach dem Login kopieren

Schauen wir uns das an ProcessActionForm, eine Methode in ActionServlet. Nachdem wir das ActionMapping basierend auf dem String erhalten haben (dies wird in den ersten beiden Artikeln vorgestellt), verwenden wir ActionMapping, um das ActionForm zu erstellen und das ActionForm in die Anfrage einzufügen Sitzung für das Management.

Sehen wir uns zunächst die spezifische Implementierung der ProcessActionForm-Methode in Struts an:


/** 
   * <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 &#39;" + 
       mapping.getScope() + "&#39; under attribute key &#39;" + 
       mapping.getAttribute() + "&#39;"); 
    } 
    if ("request".equals(mapping.getScope())) { 
      request.setAttribute(mapping.getAttribute(), instance); 
 
    } else { 
 
      HttpSession session =requestgetSession(); 
 
      session.setAttribute(mapping.getAttribute(), instance); 
 
    } 
 
    return (instance); 
 
  
 
}
Nach dem Login kopieren

Der allgemeine Prozess dieser Methode lautet: Gemäß dem Namen in ActionMapping wird nach ActionForm gesucht, es wird in der Anfrage oder Sitzung gesucht. Wenn in der Anfrage oder Sitzung ein ActionForm erstellt wurde, wird es zurückgegeben. Wenn es nicht vorhanden ist, wird es mithilfe der Reflektion gemäß dem Vervollständigungspfad der ActionForm erstellt. Anschließend wird die erstellte ActionForm in die Anforderung oder Sitzung eingefügt und anschließend wird die ActionForm zurückgegeben.

Konkret können wir dem Breakpoint-Debugging folgen, um zu sehen, wie diese Methode ausgeführt wird.

Setzen Sie zuerst einen Haltepunkt und geben Sie dann die Methode „processActionForm“ ein.

Der erste Schritt besteht darin, eine ActionForm zu erstellen:


// Create (if necessary) a formbean to use 
 
    ActionForm instance = RequestUtils.createActionForm 
 
      (request, mapping, moduleConfig, servlet); 
 
    if (instance == null) { 
 
      return (null); 
 
    }
Nach dem Login kopieren

Generieren Sie die ActionForm-Zeichenfolge in ActionMapping, indem Sie das Methodenobjekt RequestUtils.createActionForm aufrufen und kehrte zurück. Geben Sie diesen Code ein:


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 &#39;"+ name + "&#39;"); 
 
      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); 
 
}
Nach dem Login kopieren

Die Methode definiert zunächst den Variablennamen und ruft den Wert aus der Zuordnung ab, String name = programming.getName(); ist in unserem Beispiel der LoginForm-String. Anschließend wird der entsprechende LoginForm-String im entsprechenden Objekt generiert, indem FormBeanConfig config =moduleConfig.findFormBeanConfig(name); aufgerufen wird.

Was ich hier erklären möchte, ist, dass wir eine solche Beschriftungsinformation in der Struts-Config-Konfigurationsdatei konfiguriert haben:


<form-beans> 
 
    <form-bean name="loginForm" type=".struts.LoginActionForm"/> 
 
  </form-beans>
Nach dem Login kopieren

这个标签在服务器一启动的时候就会利用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); 
 
    }
Nach dem Login kopieren

这里判断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); 
 
    }
Nach dem Login kopieren

到此为止,ActionForm就创建完成,当ActionForm创建完成之后,就要用其他的方法来往ActionForm中赋值了

Das obige ist der detaillierte Inhalt vonBeispielerklärung für Struts1 ActionMapping. Für weitere Informationen folgen Sie bitte anderen verwandten Artikeln auf der PHP chinesischen Website!

Verwandte Etiketten:
Quelle:php.cn
Erklärung dieser Website
Der Inhalt dieses Artikels wird freiwillig von Internetnutzern beigesteuert und das Urheberrecht liegt beim ursprünglichen Autor. Diese Website übernimmt keine entsprechende rechtliche Verantwortung. Wenn Sie Inhalte finden, bei denen der Verdacht eines Plagiats oder einer Rechtsverletzung besteht, wenden Sie sich bitte an admin@php.cn
Beliebte Tutorials
Mehr>
Neueste Downloads
Mehr>
Web-Effekte
Quellcode der Website
Website-Materialien
Frontend-Vorlage
Über uns Haftungsausschluss Sitemap
Chinesische PHP-Website:Online-PHP-Schulung für das Gemeinwohl,Helfen Sie PHP-Lernenden, sich schnell weiterzuentwickeln!