⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 building_controller.html

📁 struts api,学习使用struts必备的文档
💻 HTML
📖 第 1 页 / 共 4 页
字号:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">
<head>
<title>The Struts User Guide - Building Controller Components</title>
<meta content="text/html; charset=utf-8" http-equiv="Content-Type" />
<meta content="Craig R. McClanahan" name="author" />
<meta content="Mike Schachter" name="author" />
<meta content="Ted Husted" name="author" />
<meta content="Martin Cooper" name="author" />
<meta content="Ed Burns" name="author" />
<meta content="Donald Ball" name="author" />
<meta content="Eddie Bush" name="author" />
<meta content="Yann Cebron" name="author" />
<meta content="David Graham" name="author" />
<meta content="Tim O'Brien" name="author" />
<meta content="Phil Steitz" name="author" />
<link href="../struts.css" type="text/css" rel="stylesheet" />
</head>
<body>
<div id="heading">
<a href="http://apache.org/">
<img id="asf_logo_wide" alt="The Apache Project" src="../images/asf_logo_wide.gif" />
</a>
<a href="http://struts.apache.org/">
<img id="struts-logo" alt="Struts Framework" src="../images/struts.gif" />
</a>
</div>
<!--end heading-->
<div id="content">
<div id="menu">

    
    
    <p>User Guide</p>
<ul>
      <li>
<a href="index.html">Table of Contents</a>
</li>
      <li>
<a href="preface.html">Preface</a>
</li>
      <li>
<a href="introduction.html">Introduction</a>
</li>
      <li>
<a href="building_model.html">Model Components</a>
</li>
      <li>
<a href="building_view.html">View Components</a>
</li>
      <li>
<a href="building_controller.html">Controller Components</a>
</li>
      <li>
<a href="configuration.html">Configuration</a>
</li>
      <li>
<a href="release-notes.html">Release Notes</a>
</li>
      <li>
<a href="installation.html">Installation</a>
</li>
    </ul>

    <p>Developer Guides</p>
<ul>
        <li>
<a href="dev_bean.html">Bean Tags</a>
</li>
        <li>
<a href="dev_html.html">HTML Tags</a>
</li>
        <li>
<a href="dev_logic.html">Logic Tags</a>
</li>
        <li>
<a href="dev_nested.html">Nested Tags</a>
</li>
        <li>
<a href="dev_tiles.html">Tiles Tags</a>
</li>
        <li>
<a href="dev_util.html">Utilities</a>
</li>
        <li>
<a href="dev_validator.html">Validator</a>
</li>
    </ul>

    <p>Quick Links</p>
<ul>
        <li>
<a href="../index.html">Welcome</a>
</li>
        <li>
<a href="index.html">User and Developer Guides *</a>
</li>
        <li>
<a href="../faqs/index.html">FAQs and HowTos</a>
</li>
    </ul>

<div class="authors">
<p>
<strong>Contributors</strong>
</p>
<ul>
<li>Craig R. McClanahan</li>
<li>Mike Schachter</li>
<li>Ted Husted</li>
<li>Martin Cooper</li>
<li>Ed Burns</li>
<li>Donald Ball</li>
<li>Eddie Bush</li>
<li>Yann Cebron</li>
<li>David Graham</li>
<li>Tim O'Brien</li>
<li>Phil Steitz</li>
</ul>
</div>
</div>
<!--end menu-->
<div id="main">
<h1 id="building_controller">4. Building Controller Components</h1>
<h2 id="overview">4.1 Overview</h2>
<div class="indent">

    <p>
    Now that we understand how to construct the Model and View components
    of your application, it is time to focus on the <code>Controller</code>
    components.  
    Struts includes a servlet that implements the primary function of mapping 
    a request URI to an <code>Action</code> class.  
    Therefore, your primary responsibilities related to the Controller are:
    </p>

    <ul>
    
        <li>
        Write an <code>ActionForm</code> class to mediate between the Model
        and the View. (See also <a href="../faqs/actionForm.html">Building
        an ActionForm</a>).
        </li>

        <li>
        Write an <code>Action</code> class for each logical request that may
        be received (extend <code>org.apache.struts.action.Action</code>).
        </li>

        <li>
        Configure a ActionMapping (in XML) for each logical request that may
        be submitted. 
        The XML configuration file is usually named 
        <code>struts-config.xml</code>.
        </li>

    </ul>
    
    <p>
    To deploy your application, you will also need to:
    </p>
    
    <ul>

        <li>
        Update the web application deployment descriptor file (in XML)
        for your application to include the necessary Struts components.
        </li>

        <li>
        Add the appropriate Struts components to your application.
        </li>
        
    </ul>
    
    <p>
    The latter two items are covered in the 
    "<a href="configuration.html">Configuring Applications</a>" chapter.
    </p>

</div>
<h2 id="action_servlet">4.2 The ActionServlet</h2>
<div class="indent">

    <p>
    For those of you familiar with MVC architecture, the ActionServlet
    represents the C - the controller.  
    The job of the controller is to:
    </p>
    
    <ul>
    
    <li>
    process user requests, 
    </li>
    
    <li>
    determine what the user is trying to achieve according to the request, 
    </li>
    
    <li>
    pull data from the model (if necessary) to be given to the appropriate 
    view, and 
    </li>
    
    <li>
    select the proper view to respond to the user.  
    </li>
    
    </ul>
    
    <p>
    The Struts controller delegates most of this grunt work to the
    <a href="#request_processor">Request Processor</a> and Action classes.
    </p>
    
    <p>
    In addition to being the front controller for your application, the
    ActionServlet instance also is responsible for initialization and
    clean-up of resources.  
    When the controller initializes, it first loads the application config 
    corresponding to the "config" init-param.
    It then goes through an enumeration of all <code>init-param</code>
    elements, looking for those elements who's name starts with
    <code>config/</code>.  
    For each of these elements, Struts loads the configuration file specified 
    by the value of that <code>init-param</code>, and assigns a "prefix" 
    value to that module's ModuleConfig instance consisting of the piece
    of the <code>init-param</code> name following "config/".  
    For example, the module prefix specified by the 
    <code>init-param config/foo</code> would be "foo". 
    This is important to know, since this is how the controller determines 
    which module will be given control of processing the request.  
    To access the module foo, you would use a URL like:
    </p>
    
    <pre>http://localhost:8080/myApp/foo/someAction.do</pre>
    
    <p>
    For each request made of the controller, the method
    <code>process(HttpServletRequest, HttpServletResponse)</code> will be
    called.  
    This method simply determines which module should service the request and 
    then invokes that module's RequestProcessor's process method, passing the 
    same request and response.
    </p>

</div>
<h2 id="request_processor">4.2.1 Request Processor</h2>
<div class="indent">

    <p>
    The RequestProcessor is where the majority of the core processing 
    occurs for each request.  
    Let's take a look at the helper functions the process method invokes 
    in-turn:
    </p>

    <table>
    <tr>
        <td>
        <code>processPath</code>
        </td>
        <td>
        Determine the path that invoked us.  
        This will be used later to retrieve an ActionMapping.
        </td>
    </tr>

    <tr>
        <td>
        <code>processLocale</code>
        </td>
        <td>
        Select a locale for this request, if one hasn't already been 
        selected, and place it in the request.
        </td>
    </tr>

    <tr>
        <td>
        <code>processContent</code> 
        </td>
        <td>
        Set the default content type (with optional character encoding) for 
        all responses if requested.
        </td>
    </tr>

    <tr>
        <td>
        <code>processNoCache</code>
        </td>
        <td>
        If appropriate, set the following response headers: "Pragma", 
        "Cache-Control", and "Expires".
        </td>
    </tr>

    <tr>
        <td>
        <code>processPreprocess</code>
        </td>
        <td>
        This is one of the "hooks" the RequestProcessor makes available for 
        subclasses to override.  
        The default implementation simply returns <code>true</code>.  
        If you subclass RequestProcessor and override processPreprocess you 
        should either return <code>true</code> (indicating process should 
        continue processing the request) or <code>false</code> (indicating 
        you have handled the request and the process should return)
        </td>
    </tr>

    <tr>
        <td>
        <code>processMapping</code> 
        </td>
        <td>
        Determine the ActionMapping associated with this path.
        </td>
    </tr>

    <tr>
        <td>
        <code>processRoles</code> 
        </td>
        <td>
        If the mapping has a role associated with it, ensure the requesting 
        user is has the specified role.  
        If they do not, raise an error and stop processing of the request.
        </td>
    </tr>

    <tr>
        <td>
        <code>processActionForm</code> 
        </td>
        <td>
        Instantiate (if necessary) the ActionForm associated with this 
        mapping (if any) and place it into the appropriate scope.
        </td>
    </tr>

    <tr>
        <td>
        <code>processPopulate</code>
        </td>
        <td>
        Populate the ActionForm associated with this request, if any.
        </td>
    </tr>

    <tr>
        <td>
        <code>processValidate</code> 
        </td>
        <td>
        Perform validation (if requested) on the ActionForm associated with 
        this request (if any).
        </td>
    </tr>
    
    <tr>
        <td>
        <code>processForward</code>
        </td>
        <td>
        If this mapping represents a forward, forward to the path specified 
        by the mapping.
        </td>
    </tr>
    
    <tr>
        <td>
        <code>processInclude</code> 
        </td>
        <td>
        If this mapping represents an include, include the result of 
        invoking the path in this request.
        </td>
    </tr>
    
    <tr>
        <td>
        <code>processActionCreate</code>
        </td>
        <td>
        Instantiate an instance of the class specified by the current 
        ActionMapping (if necessary).
        </td>
    </tr>
    
    <tr>
        <td>
        <code>processActionPerform</code> 
        </td>
        <td>
        This is the point at which your action's <code>perform</code> or 
        <code>execute</code> method will be called.
        </td>
    </tr>

    <tr>
        <td>
         <code>processForwardConfig</code>
        </td>
        <td>
        Finally, the process method of the RequestProcessor takes the 
        ActionForward returned by your Action class, and uses to select the 
        next resource (if any).
        Most often the ActionForward leads to the presentation page that
        renders the response.
        </td>
    </tr>

    </table>

</div>
<h2 id="action_form_classes">4.3 ActionForm Classes</h2>
<div class="indent">

    <p>
    An ActionForm represents an HTML form that the user interacts with over 
    one or more pages.  
    You will provide properties to hold the state of the form with getters 
    and setters to access them.  
    ActionForms can be stored in either the session (default) or request 
    scopes.  
    If they're in the session it's important to implement the form's 
    <code>reset</code> method to initialize the form before each use.  
    Struts sets the ActionForm's properties from the request parameters and 
    sends the validated form to the appropriate Action's <code>execute</code> 
    method.
    </p>

    <p>
    When you code your <code>ActionForm</code> beans, keep the following

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -