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

📄 newbie.fml

📁 ActionServlet源码 struts的一个步骤都有 知道本来有视频的太大了 就没有上传了
💻 FML
📖 第 1 页 / 共 4 页
字号:
                    by declaring an instance of the same form bean name.
                </p>

                <p>The
                    <em>MailReader</em>
                    application, part of the Struts Applications
                    subrpoject, illustrates this design pattern nicely. Note
                    the following
                    definitions from the
                    <code>struts-config.xml</code>
                    file:
                </p>
                <pre>
                    ...
                    &lt;form-beans&gt;
                    ...
                    &lt;-- Registration form bean --&gt;
                    &lt;form-bean name="registrationForm"
                    type="org.apache.struts.webapp.example.RegistrationForm"/&gt;
                    ...
                    &lt;/form-beans&gt;
                    ...
                    &lt;action-mappings&gt;
                    ...
                    &lt;-- Edit user registration --&gt;
                    &lt;action path="/editRegistration"
                    type="org.apache.struts.webapp.example.EditRegistrationAction"
                    name="registrationForm"
                    scope="request"
                    validate="false"/&gt;
                    ...
                    &lt;-- Save user registration --&gt;
                    &lt;action path="/saveRegistration"
                    type="org.apache.struts.webapp.example.SaveRegistrationAction"
                    name="registrationForm"
                    input="registration"
                    scope="request"/&gt;
                    ...
                    &lt;/action-mappings&gt;
                </pre>

                <p>Note the following features of this approach:</p>
                <ul>
                    <li>Both the
                        <code>/editRegistration</code>
                        and
                        <code>/saveRegistration</code>
                        actions use the same form bean.
                    </li>
                    <li>When the
                        <code>/editRegistration</code>
                        action is entered, the
                        framework will have pre-created an empty form bean
                        instance, and passed it to
                        the
                        <code>execute()</code>
                        method. The setup action is free to
                        preconfigure the values that will be displayed when
                        the form is
                        rendered, simply by setting the corresponding form
                        bean properties.
                    </li>
                    <li>When the setup action completes configuring the
                        properties of the
                        form bean, it should return an
                        <code>ActionForm</code>
                        that points
                        at the page which will display this form. If you are
                        using the
                        Struts JSP tag library, the
                        <code>action</code>
                        attribute on your
                        &lt;html:form&gt; tag will be set to
                        <code>/saveRegistration</code>
                        in order for the form to be submitted to the
                        processing action.
                    </li>
                    <li>Note that the setup action (
                        <code>/editRegistration</code>
                        ) turns off
                        validation on the form that is being set up. You will
                        normally want
                        to include this attribute in the configuration of your
                        setup actions,
                        because you are not planning to actually process the
                        results -- you
                        simply want to take advantage of the fact that the
                        framework will precreate
                        a form bean instance of the correct class for you.
                    </li>
                    <li>The processing action (
                        <code>/saveRegistration</code>
                        ), on the other
                        hand, leaves out the
                        <code>validate</code>
                        attribute, which defaults
                        to
                        <code>true</code>
                        . This tells the framework to perform the validations
                        associated with this form bean before invoking the
                        processing action
                        at all. If any validation errors have occurred, the
                        framework will forward
                        back to your input page (technically, it forwards back
                        to an
                        <code>ActionForward</code>
                        named "registration" in this case, because
                        the example webapp uses the
                        <code>inputForward</code>
                        attribute in the
                        <code>&lt;controller&gt;</code>
                        element -- see the documentation
                        describing
                        <code>struts-config.xml</code>
                        for more information)
                        instead of calling your processing action.
                    </li>
                </ul>

            </answer>
            </faq>

            <faq id="noForm">
            <question>Can I have an Action without a form?</question>
            <answer>
                <p>
                    Yes. If your
                    <code>Action</code>
                    does not need any data and it does not need to make any
                    data available to the view or controller component that it
                    forwards to, it doesn't need
                    a form. A good example of an
                    <code>Action</code>
                    with no
                    <code>ActionForm</code>
                    is the
                    <code>LogoffAction</code>
                    in the Struts MailReader application:
                </p>
                <pre>
                    &lt;action path="/logoff"
                    type="org.apache.struts.webapp.example.LogoffAction"&gt;
                    &lt;forward name="success" path="/index.jsp"/&gt;
                    &lt;/action&gt;
                </pre>
                <p>
                    This action needs no data other than the user's session,
                    which it can get from the
                    <code>Request</code>
                    , and it doesn't need to prepare any view elements for
                    display,
                    so it does not need a form.
                </p>

                <p>
                    However, you cannot use the &lt;html:form&gt;
                    <strong>tag</strong>
                    without an ActionForm.
                    Even if you want to use the &lt;html:form&gt; tag with a
                    simple Action that does not require input,
                    the tag will expect you to use some type of ActionForm,
                    even if it is an empty subclass without any properties.
                </p>

            </answer>
            </faq>

            <faq id="requiredif">
            <question>Can you give me a simple example of using the requiredif Validator rule?</question>
            <answer>
                <p>First off, there's an even newer Validator rule called
                    <code>validwhen</code>
                    ,
                    which is almost certainly what you want to use, since it
                    is much easier and
                    more powerful. It will be available in the first release
                    after 1.1 ships.
                    The example shown below could be coded with validwhen as:
                </p>
                <pre>
                    &lt;form name="medicalStatusForm"&gt;

                    &lt;field
                    property="pregnancyTest" depends="validwhen"&gt;
                    &lt;arg0 key="medicalStatusForm.pregnancyTest.label"/&gt;
                    &lt;var&gt;
                    &lt;var-name&gt;test&lt;/var-name&gt;
                    &lt;var-value&gt;((((sex == 'm') OR (sex == 'M')) AND
                    (*this* == null)) OR (*this* != null))&lt;/test&gt;
                    &lt;/var&gt;
                    &lt;/field&gt;
                </pre>
                <p>Let's assume you have a medical information form with three
                    fields, sex, pregnancyTest, and testResult.
                    If sex is 'f' or 'F', pregnancyTest is required. If
                    pregnancyTest is not blank, testResult is required.
                    The entry in your validation.xml file would look like
                    this:
                </p>
                <pre>
                    &lt;form name="medicalStatusForm"&gt;

                    &lt;field
                    property="pregnancyTest" depends="requiredif"&gt;
                    &lt;arg0 key="medicalStatusForm.pregnancyTest.label"/&gt;
                    &lt;var&gt;
                    &lt;var-name&gt;field[0]&lt;/var-name&gt;
                    &lt;var-value&gt;sex&lt;/var-value&gt;
                    &lt;/var&gt;
                    &lt;var&gt;
                    &lt;var-name&gt;fieldTest[0]&lt;/var-name&gt;
                    &lt;var-value&gt;EQUAL&lt;/var-value&gt;
                    &lt;/var&gt;
                    &lt;var&gt;
                    &lt;var-name&gt;fieldValue[0]&lt;/var-name&gt;
                    &lt;var-value&gt;F&lt;/var-value&gt;
                    &lt;/var&gt;
                    &lt;var&gt;
                    &lt;var-name&gt;field[1]&lt;/var-name&gt;
                    &lt;var-value&gt;sex&lt;/var-value&gt;
                    &lt;/var&gt;
                    &lt;var&gt;
                    &lt;var-name&gt;fieldTest[1]&lt;/var-name&gt;
                    &lt;var-value&gt;EQUAL&lt;/var-value&gt;
                    &lt;/var&gt;
                    &lt;var&gt;
                    &lt;var-name&gt;fieldValue[1]&lt;/var-name&gt;
                    &lt;var-value&gt;f&lt;/var-value&gt;
                    &lt;/var&gt;
                    &lt;var&gt;
                    &lt;var-name&gt;fieldJoin&lt;/var-name&gt;
                    &lt;var-value&gt;OR&lt;/var-value&gt;
                    &lt;/var&gt;
                    &lt;/field&gt;

                    &lt;field
                    property="testResult" depends="requiredif"&gt;
                    &lt;arg0 key="medicalStatusForm.testResult.label"/&gt;
                    &lt;var&gt;
                    &lt;var-name&gt;field[0]&lt;/var-name&gt;
                    &lt;var-value&gt;pregnancyTest&lt;/var-value&gt;
                    &lt;/var&gt;
                    &lt;var&gt;
                    &lt;var-name&gt;fieldTest[0]&lt;/var-name&gt;
                    &lt;var-value&gt;NOTNULL&lt;/var-value&gt;
                    &lt;/var&gt;
                    &lt;/field&gt;
                    &lt;/form&gt;
                </pre>
            </answer>
            </faq>

            <faq id="validate">
            <question>When is the best time to validate input?</question>
            <answer>
                <p>
                    This is an excellent question. Let's step back a second
                    and think about a
                    typical mid to large size application. If we start from
                    the back end and work
                    toward the view we have:
                </p>
                <p>
                    1) Database: Most modern databases are going to validate
                    for required
                    fields, duplicate records, security constraints, etc.</p>
                <p>
                    2) Business Logic: Here you are going to check for valid
                    data relationships
                    and things that make sense for the particular problem you
                    are triing to
                    solve.</p>
                <p>
                    ... This is where the framework comes into the picture, by
                    now the system should be
                    pretty well bulletproof. What we are going to do is make
                    validation friendlier
                    and informative. Rember it is OK to have duplicate
                    validations...</p>
                <p>
                    3)
                    <code>ActionErrors validate(ActionMapping map,
                        HttpServletRequest req)</code>
                    is where you can do your validation and feed back to the
                    view,
                    information required to correct any errors.
                    <code>validate</code>
                    is run after
                    the form has been
                    <code>reset</code>
                    and after the
                    <code>ActionForm</code>
                    properties have been set from corresponding view based
                    input. Also remember you
                    can turn validation off with
                    <code>validate="false"</code>
                    in the
                    <code>action</code>
                    mapping in the
                    <code>struts-config.xml</code>
                    . This is done
                    by returning an
                    <code>ActionErrors</code>
                    collection with messages from your
                    <code>ApplicationResources.properties</code>
                    file.
                </p>
                <p>
                    Here you have access to the request so you can see what
                    kinds of action is
                    being requested to fine tune your validations. The &lt;html:error&gt;
                    tag
                    allows you to dump all errors on your page or a particular
                    error associated
                    with a particular property. The
                    <code>input</code>
                    attribute of the
                    <code>struts-config.xml</code>
                    <code>action</code>
                    allows you to send
                    validation errors to a particular jsp / html / tile page.
                </p>
                <p>
                    4) You can have the system perform low level validations
                    and client side
                    feedback using a
                    <code>ValidatorForm</code>
                    or its derivatives. This will
                    generate javascript and give instant feedback to the user
                    for simple data entry

⌨️ 快捷键说明

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