Struts2 File Upload instance

Source: Internet
Author: User
Tags i18n

Struts2 does not provide its own request parser. That is to say, struts2 does not process multipart/form-data requests by itself. It needs to call other request Resolvers, parse the form fields in the HTTP request. However, struts2 further encapsulates the form fields in the original upload parser, further simplifying file upload and struts of Struts2. in the properties configuration file, configure struts. multipart. parser = jakarta (default srtuts2), or common cos and pell. Project Structure: web. xml file content: <? Xml version = "1.0" encoding = "UTF-8"?>
<Web-app version = "2.5" xmlns = "http://java.sun.com/xml/ns/javaee"
Xmlns: xsi = "http://www.w3.org/2001/XMLSchema-instance"
Xsi: schemaLocation = "http://java.sun.com/xml/ns/javaee
Http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd>
<Filter>
<Filter-name> struts2 </filter-name>
<Filter-class> org. apache. struts2.dispatcher. FilterDispatcher </filter-class>
</Filter>
<Filter-mapping>
<Filter-name> struts2 </filter-name>
<Url-pattern>/* </url-pattern>
</Filter-mapping>


<Filter>
<Filter-name> struts-cleanup </filter-name>
<Filter-class> org. apache. struts2.dispatcher. ActionContextCleanUp </filter-class>
</Filter>
<Filter-mapping>
<Filter-name> struts-cleanup </filter-name>
<Url-pattern>/* </url-pattern>
</Filter-mapping>

<Welcome-file-list>
<Welcome-file> index. jsp </welcome-file>
</Welcome-file-list>
</Web-app>


Here web. xml configures an ActionContextCleanUp configuration. This class is a filter, which facilitates the integration of strut2 and sitemesh. It has nothing to do with the file upload, but does not load this filter, an inexplicable exception may occur in the uploading process. After adding the exception, it becomes stable. It may be a bug in strut2. Index. jsp file content: <% @ page language = "java" pageEncoding = "GBK" %> <% @ taglib prefix = "s" uri = "/struts-tags" %> <% @ taglib prefix = "c" uri =" http://java.sun.com/jstl/core_rt "%> <! Doctype html public "-// W3C // dtd html 4.01 Transitional // EN"> <% @ Taglib prefix = "s" uri = "/struts-tags" %>
<! Doctype html public "-// W3C // dtd html 4.01 Transitional // EN">
<Html>
<Head>
<Meta http-equiv = "pragma" content = "no-cache">
<Meta http-equiv = "cache-control" content = "no-cache">
<Meta http-equiv = "expires" content = "0">
</Head> <body> upload successful! <Br> file title: <s: property value = "+ title"/> <br> file: "/> <br> </body> 1. xxx attribute of the File type: Used to encapsulate the File content corresponding to the page File domain.
2. xxxFileName attribute of the String type: Used to encapsulate the file name corresponding to the file field.
3. xxxContentType attribute of the String type: the file type used to encapsulate the files in the file domain. The above program has been debugged!

Related Article

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: info-contact@alibabacloud.com 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.