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

📄 building_controller.html

📁 struts api,学习使用struts必备的文档
💻 HTML
📖 第 1 页 / 共 4 页
字号:
    principles in mind:
    </p>

    <ul>
    
    <li>
    The <code>ActionForm</code> class itself requires no specific
    methods to be implemented.  
    It is used to identify the role these particular beans play in the overall 
    architecture.  
    Typically, an <code>ActionForm</code> bean will have only property getter 
    and property setter methods, with no business logic.
    </li>
    
    <li>
    The ActionForm object also offers a standard validation mechanism.
    If you override a "stub" method, and provide error messages in the
    standard application resource, Struts will automatically validate the
    input from the form (using your method). 
    See "<a href="./building_view.html#form_validation">Automatic Form
    Validation</a>" for details. Of course, you can also ignore the
    ActionForm validation and provide your own in the Action object.
    </li>

    <li>
    Define a property (with associated <code>getXxx</code> and
    <code>setXxx</code> methods) for each field that is present in the
    form.  
    The field name and property name must match according to the usual 
    JavaBeans conventions (see the Javadoc for the 
    <code>java.beans.Introspector</code> class for a start on information
    about this).  
    For example, an input field named <code>username</code> will cause the 
    <code>setUsername</code> method to be called.
    </li>

    <li>
    Buttons and other controls on your form can also be defined as properties.
    This can help determine which button or control was selected when the
    form was submitted. 
    Remember, the ActionForm is meant to represent your data-entry form, not 
    just the data beans.
    </li>
    
    <li>
    Think of your ActionForm beans as a firewall between HTTP and the Action.
    Use the <code>validate</code> method to ensure all required properties 
    are present, and that they contain reasonable values. 
    An ActionForm that fails validation will not even be presented to the 
    Action for handling.
    </li>
    
    <li>
    You may also place a bean instance on your form, and use nested property
    references. 
    For example, you might have a "customer" bean on your ActionForm, and 
    then refer to the property "customer.name" in your presentation page.
    This would correspond to the methods <code>customer.getName()</code> and
    <code>customer.setName(string Name)</code> on your customer bean. 
    See the Tag Library Developer Guides for more about using nested syntax 
    with the Struts JSP tags.
    </li>
    
    <li>
    <em>Caution:</em> If you nest an existing bean instance on your form, think
    about the properties it exposes. 
    Any public property on an ActionForm that accepts a single String value 
    can be set with a query string. 
    It may be useful to place beans that can affect the business state inside 
    a thin "wrapper" that exposes only the properties required. 
    This wrapper can also provide a filter to be sure runtime properties are 
    not set to inappropriate values.
    </li>
    
    </ul>

</div>
<h2 id="dyna_action_form_classes">4.3.1 DynaActionForm Classes</h2>
<div class="indent">

    <p>
    Maintaining a separate concrete ActionForm class for each form in your
    Struts application is time-consuming.
    It is particularly frustrating when all the ActionForm does is gather
    and validate simple properties that are passed along to a business
    JavaBean.
    </p>
    <p>
    This bottleneck can be alleviated through the use of DynaActionForm classes.
    Instead of creating a new ActionForm subclass and new get/set methods for 
    each of your bean's properties, you can list its properties, type, and 
    defaults in the Struts configuration file.
    </p>
    
    <p>
    For example, add the following to struts-config.xml for a UserForm bean 
    that stores a user's given and family names:
    </p>
    
<pre>
<code>
&lt;form-bean 
    name="UserForm" 
    type="org.apache.struts.action.DynaActionForm"&gt;
    &lt;form-property 
        name="givenName" 
        type="java.lang.String" 
        initial="John"/&gt;
    &lt;form-property 
        name="familyName" 
        type="java.lang.String" 
        initial="Smith"/&gt;
&lt;/form-bean&gt;
</code>
</pre>

    <p>
    The types supported by DynaActionForm include:
    </p>
    
    <ul>
    
        <li>
        java.math.BigDecimal
        </li>

        <li>
        java.math.BigInteger
        </li>

        <li>
        boolean and java.lang.Boolean
        </li>

        <li>
        byte and java.lang.Byte
        </li>

        <li>
        char and java.lang.Character
        </li>

        <li>
        java.lang.Class
        </li>

        <li>
        double and java.lang.Double
        </li>

        <li>
        float and java.lang.Float
        </li>

        <li>
        int and java.lang.Integer
        </li>

        <li>
        long and java.lang.Long
        </li>

        <li>
        short and java.lang.Short
        </li>

        <li>
        java.lang.String
        </li>

        <li>
        java.sql.Date
        </li>

        <li>
        java.sql.Time
        </li>

        <li>
        java.sql.Timestamp
        </li>
    
    </ul>

    <p>
        You may also specify Arrays of these types (e.g. <code>String[]</code>).
        You may also specify a concrete implementation of the Map Interface,
        such as <code>java.util.HashMap</code>,
        or a List implementation, such as <code>java.util.ArrayList</code>.
    </p>

    <p>
    If you do not supply an initial attribute, numbers will be initialized to 
    0 and objects to <code>null</code>.
    </p>
    
    <p>
    In JSP pages using the original Struts custom tags, attributes of
    <code>DynaActionForm</code> objects can be referenced just like ordinary
    <code>ActionForm</code> objects. Wherever a Struts tag refers to a
    "property", the tags will automatically use the DynaActionForm properties
    just like those of a conventional JavaBean.
    You can even expose DynaActionForm properties using bean:define.
    (Although, tou can't use bean:define to <strong>instantiate</strong> a DynaActionForm,
    since it needs to be setup with the appropriate dyna-properties).
    </p>

    <p>
    If you are using the Struts JSTL EL taglib, the references are different,
    however.
    Only properties of ordinary <code>ActionForm</code> objects can be directly
    accessed through the JSTL expression language syntax.  
    The <code>DynaActionForm</code> properties must be accessed through a
    slightly different syntax. 
    The JSTL EL syntax for referencing a property
    of an <code>ActionForm</code> goes like this:
    </p>
    
<pre>
<code>${formbean.prop}</code>
</pre>
    
    <p>
    The syntax for referencing a property of a <code>DynaActionForm</code>
    would be:
    </p>

<pre>
<code>${dynabean.map.prop}</code>
</pre>

    <p>
    The <code>map</code> property is a property of 
    <code>DynaActionForm</code> which represents the <code>HashMap</code>
    containing the <code>DynaActionForm</code> properties.
    </p>

    <p>
    DynaActionForms are meant as an easy solution to a common problem:
    <em>Your ActionForms use simple properties and standard validations,
    and you just pass these properties over to another JavaBean</em>
    (say using <code>BeanUtils.copyProperties(myBusinessBean,form)</code>).
    </p>

    <p>
    DynaActionForms are <strong>not</strong> a drop-in replacement for ActionForms.
    If you need to access ActionForm properties in your Action, you will need to
    use the map-style accessor, like <code>myForm.get("name")</code>.
    If you actively use the ActionForm object in your Action,
    then you may want to use conventional ActionForms instead.
    </p>

    <p>
    DynaActionForms cannot be instantiated using a no-argument constructor.
    In order to simulate the extra properties,
    there is a lot of machinery involved in their construction.
    You must rely on Struts to instantiate a DynaActionForm for you,
    via the ActionMapping.
    </p>

    <p>
    If need be, you can extend the DynaActionForm to add custom
    validate and reset methods you might need.
    Simply specify your subclass in the struts-config instead.
    However, you cannot mix conventional properties and DynaProperties.
    A conventional getter or setter on a DynaActionForm won't be found
    by the reflection utilities.
    </p>

    <p>
    To use DynaActionForms with the Struts Validator, specify
    <code>org.apache.struts.validator.ValidatorActionForm</code>
    (or your subclass) as the form-bean class.
    </p>

    <p>
    And, of course, while the  DynaActionForm may support various binary types,
    properties used with the <code>html:text</code> tag should still be
    String properties.
    </p>

    <p>
    DynaActionForms relieve developers of maintaining simple ActionForms. For even
    less maintenance, try Niall Pemberton's
    <a href="http://www.niallp.pwp.blueyonder.co.uk/">LazyActionForm</a>.
    </p>

</div>
<h2 id="map_action_form_classes">4.3.2 Map-backed ActionForms</h2>
<div class="indent">

    <p>
    The DynaActionForm classes offer the ability to create ActionForm beans 
    at initialization time, based on a list of properties enumerated in the 
    Struts configuration file. 
    However, many HTML forms are generated dynamically at request time. 
    Since the properties of these forms' ActionForm beans are not all known 
    ahead of time, we need a new approach.
    </p>
    
    <p>
    Struts allows you to make one or more of your ActionForm's properties' 
    values a Map instead of a traditional atomic object. 
    You can then store the data from your form's dynamic fields in that Map. 
    Here is an example of a map-backed ActionForm class:
    </p>
    
<pre>
<code>public FooForm extends ActionForm {

    private final Map values = new HashMap();

    public void setValue(String key, Object value) {
        values.put(key, value);
    }

    public Object getValue(String key) {
        return values.get(key);
    }

}
</code>
</pre>

    <p>
    In its corresponding JSP page, you can access objects stored in the 
    values map using a special notation: <code>mapname(keyname)</code>. 
    The parentheses in the bean property name indicate that:
    </p>
    
    <ul>
    
        <li>
        The bean property named <code>mapname</code> is indexed using Strings
        (probably backed by a Map), and that 
        </li>

        <li>
        Struts should look for get/set methods that take a String key 
        parameter to find the correct sub-property value. 
        Struts will, of course, use the <code>keyname</code> value from the 
        parentheses when it calls the get/set methods.
        </li>
    
    </ul>
    
    <p>
    Here is a simple example:
    </p>

<pre>
<code>&lt;html:text property="value(foo)"/&gt;</code>
</pre>

    <p>
    This will call the <code>getValue</code> method on FooForm with a key 
    value of "<code>foo</code>" to find the property value. 
    To create a form with dynamic field names, you could do the following:
    </p>

<pre>
<code>
&lt;% 
	for (int i = 0; i &lt; 10; i++) {
		String name = "value(foo-" + i + ")";
%&gt;
		&lt;html:text property="&lt;%= name %&gt;"/&gt;
		&lt;br/&gt;
&lt;%
	}
%&gt;
</code>
</pre>

    <p>
    Note that there is nothing special about the name <code>value</code>. 
    Your map-backed property could instead be named <code>property</code>, 
    <code>thingy</code>, or any other bean property name you prefer. 
    You can even have multiple map-backed properties on the same bean.
    </p>
    
    <p>
    In addition to map-backed properties, you can also create list-backed 
    properties. 
    You do so by creating indexed get/set methods on your bean:
    </p>
    
<pre>
<code>public FooForm extends ActionForm {

    private final List values = new ArrayList();

    public void setValue(int key, Object value) {
        values.set(key, value);
    }

    public Object getValue(int key) {
        return values.get(key);
    }
}
</code>
</pre>

    <p>
    In your presentation pages, you access individual entries in a list-backed 
    property by using a different special notation: 
    <code>listname[index]</code>. 
    The braces in the bean property name indicate that the bean property named 
    <code>listname</code> is indexed (probably backed by a List), and that 
    Struts should look for get/set methods that take an index parameter in 
    order to find the correct sub-property value.
    </p>

    <p>
        While map-backed ActionForms provide you with more flexibility, they do not support the same range of syntax
        available to conventional or DynaActionForms.
        You might have difficulty referencing <a href="../faqs/indexedprops.html">indexed or mapped properties</a>
        using a map-backed ActionForm.
        The <code>validwhen</code> validator (since Struts 1.2.1) also does not support map-backed ActionForms.
    </p>

</div>
<h2 id="action_classes">4.4 Action Classes</h2>
<div class="indent">

    <p>
    The <code>Action</code> class defines two methods that could be
    executed depending on your servlet environment:
    </p>

<pre>
<code>public ActionForward execute(ActionMapping mapping,
                     ActionForm form,
                     ServletRequest request,
                     ServletResponse response)
throws Exception;

public ActionForward execute(ActionMapping mapping,

⌨️ 快捷键说明

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