struts2 File upload Download form duplicate commit custom interceptor

Source: Internet
Author: User
Tags html form

Preparation of the cross-form on the file

To upload one or more files using an HTML form

  The Enctype property of the HTML form must be set to Multipart/form-data

The method property of the HTML form must be set to post

You need to add <input type= "file" > field.

Support for file uploads by Struts

In the Struts application, FileUpload interceptors and Jakarta Commons FileUpload components can upload files.

Step: 1. Use the file tag in the document upload form on the JSP page. If you need to upload multiple files at once, you must use multiple file tags, but their names must be the same

2. Add 3 new and file upload related properties to the Action. The names of these 3 properties must be in the following format
      [File Name]: file-the document being uploaded. For example: Data
[File Name] Contenttype:string-The file type of the uploaded file. Example: Datacontenttype
[File Name] Filename:string-file name of the uploaded file. Example: DataFileName

    If you upload multiple files, you can use the List

Configuring the FileUpload Interceptor

The FileUpload Interceptor has 3 properties that can be set.

MaximumSize: Maximum length (in bytes) for uploading a single file, default value is 2 MB

Allowedtypes: The type of file that is allowed to be uploaded, separated by commas between each type

Allowedextensions: Allow file extensions to be uploaded, separated by commas between extensions

You can override these 3 properties in a Struts.xml file

The Commons FileUpload component defaults to the total maximum value of 2M, and can be modified by configuring constants in the Struts configuration file

Error messages related to file uploads are predefined in the file under Org.apache.struts2. You can redefine the error message in the resource file that corresponds to the file upload Action

Download the file

1). Use the result of type= "stream" in Struts2 to download

2). Detailed use details see struts-

3). You can set the following parameters for the stream's result

ContentType: The MIME type of the file being downloaded. The default value is Text/plain
ContentLength: The size, in bytes, of the file being downloaded
Contentdisposition: You can set the Contentdispositon response header for the download file name, the default value is inline, which is usually set to the following format: Attachment;filename= "Document.pdf".
The input stream for the file provided in the inputname:action. The default value is InputStream
BufferSize: The size of the buffer when the file is downloaded. The default value is 1024
Allowcaching: Whether the cache is allowed when the file is downloaded. The default value is True
Contentcharset: Character encoding at file download time.

The parameters of the Stream result type can be overridden by the Action as an attribute

Recurring Submission Issues for forms

1). What is a duplicate submission of a form

> Without refreshing the form page:
>> Multiple Click submit button
>> has been submitted successfully, press "fallback", then click "Submit Button".
>> in the form of the Controller response page as a forwarding case, if it has been submitted successfully, then click "Refresh (F5)"

> Note:
>> If the form page is refreshed, then submitting the form does not count duplicate submissions
>> If you are using the redirect response type, you have successfully submitted, then click "Refresh", not the form of duplicate submissions

2). The drawback of repeated submissions: increased burden on the server; May cause an error operation.

3). Struts2 Resolve Duplicate commit issues for forms:

I. Add the S:token sub-label in the S:form. It inserts a hidden field in the table dropdowns and stores the tagged value (the value of the field in the hidden field) in the HttpSession object.

II. Use Token or tokensession interceptors.

> Both interceptors are not in the default interceptor stack, so they need to be manually configured
> If you use the Token interceptor, you need to configure a token.valid result
> If you use the Tokensession interceptor, you do not need to configure any other result

III. Token VS tokensession

> is all about resolving form repeat submissions.
> Using the token blocker will go to token.valid this result
> Using the Tokensession Interceptor will also respond to that target page, but will not execute subsequent interceptors for tokensession. It's like nothing ever happened!

Iv. You can use the S:actionerror label to display duplicate-submitted error messages.

The error message can be overridden in an internationalized resource file. The message can be found in the file

Custom Interceptors

1). Specific steps

I. Defining a class for an interceptor

> can implement Interceptor interface
> Inheritance Abstractinterceptor abstract class

II. In the Struts.xml file configuration.

    <interceptors>                    <interceptor name= "Hello" class= "Com.atguigu.struts2.interceptors.MyInterceptor" > </interceptor>            </interceptors>        <action name= "Testtoken" class= " ">        <interceptor-ref name=" Hello "></interceptor-ref>        <interceptor-ref name= "Defaultstack" ></interceptor-ref>        <result>/success.jsp</ result>        <result name= "Invalid.token" >/token-error.jsp</result>    </action>

III. Note: the Invoke () method that does not invoke Actioninvocation can be selected in a custom interceptor. Then the subsequent interceptors and Action methods will not be called. Struts renders custom interceptors intercept method return value corresponding to result

struts2 File upload Download form duplicate commit custom interceptor

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.