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

📄 actionform.html

📁 struts api,学习使用struts必备的文档
💻 HTML
字号:
<!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>Building an ActionForm</title>
<meta content="text/html; charset=utf-8" http-equiv="Content-Type" />
<meta content="Craig R. McClanahan" 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>FAQs</p>
<ul>
        <li>
<a href="kickstart.html">Kickstart</a>
</li>
        <li>
<a href="newbie.html">Newbie</a>
</li>
        <li>
<a href="helping.html">How to Help</a>
</li>
    </ul>

    <p>Howto Guides</p>
<ul>
        <li>
<a href="actionForm.html">Action Forms</a>
</li>
        <li>
<a href="apps.html">Building Apps</a>
</li>
        <li>
<a href="database.html">Database</a>
</li>
        <li>
<a href="indexedprops.html">Indexed Properties</a>
</li>
        <li>
<a href="ssl.html">SSL</a>
</li>

        <li>
<a href="struts-el.html">Struts-EL (JSTL)</a>
</li>
    </ul>

    <p>IDE Guides</p>
<ul>
        <li>
<a href="eclipse.html">Eclipse</a>
</li>
        <li>
<a href="netbeans.html">Netbeans</a>
</li>
    </ul>

    <p>Quick Links</p>
<ul>
        <li>
<a href="../index.html">Welcome</a>
</li>

        <li>
<a href="../userGuide/index.html">User and Developer Guides</a>
</li>
    </ul>

<div class="authors">
<p>
<strong>Contributors</strong>
</p>
<ul>
<li>Craig R. McClanahan</li>
</ul>
</div>
</div>
<!--end menu-->
<div id="main">
<h1 id="actionForm">How to Build an Action Form</h1>
<h2 id="login">Creating a Login Form</h2>
<div class="indent">

    <p>
    This is a simple example of a login form to illustrate how Struts
    makes dealing with forms much less painful than using straight HTML
    and standard JSP facilities.  
    Consider the following page (based on the example MailReader application 
    included  with Struts) named <code>logon.jsp</code>:
    </p>

<hr />

<pre>
<code>
&lt;%@ page language="java" %&gt;
&lt;%@ taglib  uri="/WEB-INF/struts-html.tld" prefix="html" %&gt;
&lt;%@ taglib uri="/WEB-INF/struts-bean.tld" prefix="bean" %&gt;

&lt;html:html&gt;
&lt;head&gt;
&lt;title&gt;
    &lt;bean:message key="logon.title"/&gt;
&lt;/title&gt;
&lt;/head&gt;

&lt;body bgcolor="white"&gt;

&lt;html:errors/&gt;

&lt;html:form action="/logon" focus="username"&gt;
&lt;table border="0" width="100%"&gt;
    &lt;tr&gt;
        &lt;th class="right"&gt;
            &lt;bean:message key="prompt.username"/&gt;
        &lt;/th&gt;
        &lt;td class="left"&gt;
            &lt;html:text  property="username" size="16"/&gt;
        &lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
        &lt;th class="right"&gt;
            &lt;bean:message key="prompt.password"/&gt;
        &lt;/th&gt;
        &lt;td class="left"&gt;
            &lt;html:password property="password" size="16"/&gt;
        &lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
        &lt;td class="right"&gt;
            &lt;html:submit&gt;
                &lt;bean:message key="button.submit"/&gt;
            &lt;/html:submit&gt;
        &lt;/td&gt;
        &lt;td class="right"&gt;
            &lt;html:reset&gt;
                &lt;bean:message key="button.reset"/&gt;
            &lt;/html:reset&gt;
        &lt;/td&gt;
    &lt;/tr&gt;
&lt;/table&gt;

&lt;/html:form&gt;
&lt;/body&gt;
&lt;/html:html&gt;
</code>
</pre>

<hr />

    <p>
    The following items illustrate the key features of form handling in Struts,
    based on this example:
    </p>

    <ul>

        <li>
        The <code>taglib</code> directive tells the JSP page compiler where to
        find the <em>tag library descriptor</em> for the Struts tag library.  
        In this case, we are using <code>bean</code> as the prefix that 
        identifies tags from the struts-bean library, and "html" as the prefix  
        that identifies tags from the struts-html library.  
        Any desired prefix can be used.
        </li>
          
        <li>
        This page uses several occurrences of the
        <strong>message</strong> tag to look up internationalized
        message strings from a <code>MessageResources</code> object containing
        all the resources for this application.  
        For this page to work, the following message keys must be defined in 
        these resources:
          
        <ul>
        
            <li>
            <strong>logon.title</strong> - Title of the logon page
            </li>

            <li>
            <strong>prompt.username</strong> - A "Username:" prompt string
            </li>

            <li>
            <strong>prompt.password</strong> - A "Password:" prompt string
            </li>

            <li>
            <strong>button.submit</strong> - "Submit" for the button label
            </li>

            <li>
            <strong>button.reset</strong> - "Reset" for the button label
        </li>
        
        </ul>

        When the user logs on, the application can store a <code>Locale</code>
        object in the user's session.  
        This <code>Locale</code> will be used to select messages in the 
        appropriate language.  
        This makes it easy to implement giving the user an option to switch 
        languages -- simply change the stored <code>Locale</code> object, and 
        all messages are switched automatically.
        </li>
          
        <li>
        The <strong>errors</strong> tag displays any error messages that have been 
        stored by a business logic component, or nothing if no errors have been 
        stored.  
        This tag will be described further below.
        </li>
          
        <li>
        The <strong>form</strong> tag renders an HTML <code>&lt;form&gt;</code> element, 
        based on the specified attributes.
        It also associates all of the fields within this form with a 
        <code>ActionForm</code> bean 
        [<code>org.apache.struts.action.ActionForm</code>].
        The tag looks up the <code>/logon</code> action mapping in the Struts
        configuration.
        The <code>logon</code> mapping tells the tag that the form bean is
        stored in the session context under the key <code>logonForm</code>.
        The Struts developer provides the Java implementation of the 
        ActionForm bean, subclassing the Struts class <code>ActionForm</code>
        (see <a href="../userGuide/building_controller.html#action_form_classes">Building
        Controller</a> components).
        This bean is used to provide initial values for all of the input
        fields that have names matching the property names of the bean.
        If an appropriate bean is not found, a new one will be created
        automatically, using the Java class name specified through the 
        action mapping.
        </li>

        <li>
        The form bean can also be specified in the tag by providing
        <code>name</code> and <code>type</code> attributes. But most often,
        everything is specified in the
        <a href="../userGuide/building_controller.html#config">Struts Configuration 
        File</a>.
        </li>

        <li>
        The <strong>text</strong> tag renders an HTML <code>&lt;input&gt;</code> 
        element of type "text".  
        In this case, the number of character positions to occupy on the 
        browser's screen has been specified as well. 
        When this page is executed, the current value of the 
        <code>username</code> property of the corresponding bean (that is, 
        the value returned by <code>getUsername</code>).
        </li>

        <li>
        The <strong>password</strong> tag is used similarly.
        The difference is that the browser will echo asterisk characters,
        instead of the input value, as the user types their password.
        </li>

        <li>
        The <strong>submit</strong> and<strong>reset</strong> tags generate the corresponding
        buttons at the bottom of the form.  
        The text labels for each button are created using message tags,
        as with the prompts, so that these values are internationalized.
        </li>
          
    </ul>

    <p>
    Handling multipart forms is also easy.  
    Obviously when you create a multipart form you're creating a form that  
    has at least one input of type "file".  
    The first step to creating a multipart form is to utilize the struts-html 
    taglib to create the presentation page:
    </p>

<hr />

<pre>
<code>
&lt;%@page language="java"&gt;
&lt;%@taglib 
    uri="/WEB-INF/struts-html.tld"
   prefix="html"&gt;

&lt;html:form action="uploadAction.do" enctype="multipart/form-data"&gt;

    Please Input Text: &lt;html:text property="myText"&gt;
    Please Input The File You Wish to Upload: &lt;html:file property="myFile"&gt;
   
   &lt;html:submit /&gt;

&lt;/html:form&gt;
</code>
</pre>

<hr />

    <p>
    The next step is to create your ActionForm bean:
    </p>


<hr />

<pre>
<code>
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.upload.FormFile;

public class UploadForm extends ActionForm {

    protected String myText;
    protected FormFile myFile;
    
    public void setMyText(String text) {
        myText = text;
    }
    
    public String getMyText() {
        return myText;
    }
    
    public void setMyFile(FormFile file) {
        myFile = file;
    }
    
    public FormFile getMyFile() {
        return myFile;
    }

}
</code>
</pre>

<hr />

    <p>
    Look at the Javadocs for 
    <code>
<a href="../api/org/apache/struts/upload/FormFile.html">
    FormFile</a>
</code> to see the methods it exposes to manipulate files in 
    file uploading.
    Also look at the Javadocs for
    <a href="../api/org/apache/struts/action/ActionServlet.html">
    ActionServlet</a> and
    <a href="../api/org/apache/struts/action/ActionMapping.html">
    ActionMapping</a> for the various parameters you can specify to change 
    how files are uploaded.  
    Basically in your <code>execute</code> method in your action class you 
    would call <code>((UploadForm) form).getMyFile()</code> to retrieve the 
    FormFile and do what you want with it.
    </p>

</div>
 </div>
<!--end main-->
</div>
<!--end content-->
<div id="footer">
<img id="powered-logo" alt="Powered by Struts" src="../images/struts-power.gif" />
        Copyright (c) 2000-2005, The Apache Software Foundation <span class="noprint">- 
        <a href="http://wiki.apache.org/struts/StrutsDocComments">Comments?</a>
</span>
</div>
<!--end footer-->
</body>
</html>

⌨️ 快捷键说明

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