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!