Skip to content

Commit 3780310

Browse files
authored
Merge pull request #456 from apache/fix/file-upload
Adjusts file upload example to action based upload
2 parents df87211 + ad0de21 commit 3780310

File tree

11 files changed

+99
-39
lines changed

11 files changed

+99
-39
lines changed

file-upload/pom.xml

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,9 +29,7 @@
2929
<webApp>
3030
<contextPath>/${project.artifactId}</contextPath>
3131
</webApp>
32-
<stopKey>CTRL+C</stopKey>
33-
<stopPort>8999</stopPort>
34-
<scanIntervalSeconds>10</scanIntervalSeconds>
32+
<scan>10</scan>
3533
</configuration>
3634
</plugin>
3735
</plugins>

file-upload/src/main/java/org/apache/struts/example/Upload.java renamed to file-upload/src/main/java/org/apache/struts/example/UploadAction.java

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -20,13 +20,18 @@
2020
package org.apache.struts.example;
2121

2222
import org.apache.struts2.ActionSupport;
23+
import org.apache.struts2.action.UploadedFilesAware;
24+
import org.apache.struts2.dispatcher.multipart.UploadedFile;
2325

2426
import java.io.File;
27+
import java.util.Arrays;
28+
import java.util.List;
29+
import java.util.stream.Stream;
2530

2631
/**
2732
* <code>Allows upload a file</code>
2833
*/
29-
public class Upload extends ActionSupport {
34+
public class UploadAction extends ActionSupport implements UploadedFilesAware {
3035

3136
private File[] upload;
3237
private String[] uploadFileName;
@@ -36,27 +41,26 @@ public String execute() throws Exception {
3641
return INPUT;
3742
}
3843

39-
public File[] getUpload() {
40-
return upload;
44+
public String upload() throws Exception {
45+
return SUCCESS;
4146
}
4247

43-
public void setUpload(File[] upload) {
44-
this.upload = upload;
48+
public File[] getUpload() {
49+
return upload;
4550
}
4651

4752
public String[] getUploadFileName() {
4853
return uploadFileName;
4954
}
5055

51-
public void setUploadFileName(String[] uploadFileName) {
52-
this.uploadFileName = uploadFileName;
53-
}
54-
5556
public String[] getUploadContentType() {
5657
return uploadContentType;
5758
}
5859

59-
public void setUploadContentType(String[] uploadContentType) {
60-
this.uploadContentType = uploadContentType;
60+
@Override
61+
public void withUploadedFiles(List<UploadedFile> uploadedFiles) {
62+
upload = uploadedFiles.stream().map(UploadedFile::getContent).toArray(File[]::new);
63+
uploadFileName = uploadedFiles.stream().map(UploadedFile::getName).toArray(String[]::new);
64+
uploadContentType = uploadedFiles.stream().map(UploadedFile::getContentType).toArray(String[]::new);
6165
}
6266
}

file-upload/src/main/resources/struts.xml

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<?xml version="1.0" encoding="UTF-8" ?>
22
<!DOCTYPE struts PUBLIC
3-
"-//Apache Software Foundation//DTD Struts Configuration 2.5//EN"
4-
"https://struts.apache.org/dtds/struts-2.5.dtd">
3+
"-//Apache Software Foundation//DTD Struts Configuration 6.0//EN"
4+
"https://struts.apache.org/dtds/struts-6.0.dtd">
55
<struts>
66
<constant name="struts.enable.DynamicMethodInvocation" value="false"/>
77
<constant name="struts.devMode" value="true"/>
@@ -12,11 +12,16 @@
1212

1313
<action name="index">
1414
<result type="redirectAction">
15-
<param name="actionName">upload</param>
15+
<param name="actionName">input</param>
1616
</result>
1717
</action>
1818

19-
<action name="upload" class="org.apache.struts.example.Upload">
19+
<action name="input" class="org.apache.struts.example.UploadAction">
20+
<result name="input">WEB-INF/input.jsp</result>
21+
</action>
22+
23+
<action name="upload" class="org.apache.struts.example.UploadAction" method="upload">
24+
<result>WEB-INF/upload.jsp</result>
2025
<result name="input">WEB-INF/upload.jsp</result>
2126
</action>
2227

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
<%@ page contentType="text/html; charset=UTF-8" %>
2+
<%@ taglib prefix="s" uri="/struts-tags" %>
3+
<html>
4+
<head>
5+
<title>File upload: input</title>
6+
</head>
7+
8+
<body>
9+
10+
<s:form action="upload" method="post" enctype="multipart/form-data">
11+
<s:file name="upload"/>
12+
<s:file name="upload"/>
13+
<s:file name="upload"/>
14+
<s:submit/>
15+
</s:form>
16+
17+
<s:actionerror/>
18+
19+
</body>
20+
</html>

file-upload/src/main/webapp/WEB-INF/upload.jsp

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,16 @@
22
<%@ taglib prefix="s" uri="/struts-tags" %>
33
<html>
44
<head>
5-
<title>File upload</title>
5+
<title>File upload: result</title>
66
</head>
77

88
<body>
99

10-
<s:form action="upload" method="post" enctype="multipart/form-data">
11-
<s:file name="upload"/>
12-
<s:file name="upload"/>
13-
<s:file name="upload"/>
14-
<s:submit/>
15-
</s:form>
10+
<p>
11+
<s:a action="input">back to input</s:a>
12+
</p>
13+
14+
<s:actionerror/>
1615

1716
<s:iterator value="upload" var="u">
1817
<s:property value="u"/><br/>

file-upload/src/main/webapp/WEB-INF/web.xml

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
<?xml version="1.0" encoding="UTF-8"?>
2-
<web-app id="struts_blank" version="2.4"
3-
xmlns="http://java.sun.com/xml/ns/j2ee"
2+
<web-app xmlns="https://jakarta.ee/xml/ns/jakartaee"
43
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
5-
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
4+
xsi:schemaLocation="https://jakarta.ee/xml/ns/jakartaee https://jakarta.ee/xml/ns/jakartaee/web-app_6_0.xsd"
5+
metadata-complete="false"
6+
version="6.0">
7+
68
<display-name>Struts Blank</display-name>
79

810
<filter>

file-upload/src/main/webapp/index.html

Lines changed: 0 additions & 10 deletions
This file was deleted.

sitemesh3/pom.xml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,10 @@
1313

1414
<packaging>war</packaging>
1515

16+
<properties>
17+
<struts2.version>7.0.4-SNAPSHOT</struts2.version>
18+
</properties>
19+
1620
<dependencies>
1721
<dependency>
1822
<groupId>org.sitemesh</groupId>

sitemesh3/src/main/resources/struts.xml

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
<constant name="struts.action.extension" value=""/>
99
<constant name="struts.custom.i18n.resources" value="DefaultMessages"/>
1010

11-
<package name="default" extends="struts-default">
11+
<package name="default" extends="struts-default" namespace="/">
1212
<default-action-ref name="index"/>
1313
<action name="index">
1414
<result>/WEB-INF/index.jsp</result>
@@ -21,4 +21,14 @@
2121
</action>
2222

2323
</package>
24+
25+
<package name="default2" extends="struts-default" namespace="/admin">
26+
<default-action-ref name="index"/>
27+
<action name="index">
28+
<result>/WEB-INF/admin/index.jsp</result>
29+
</action>
30+
<action name="hello">
31+
<result>/WEB-INF/admin/hello.jsp</result>
32+
</action>
33+
</package>
2434
</struts>
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<%@ page contentType="text/html; charset=UTF-8" %>
2+
<%@ taglib prefix="s" uri="/struts-tags" %>
3+
<html>
4+
<head>
5+
<title>SiteMesh example: Admin Index</title>
6+
</head>
7+
<body>
8+
<h2>SiteMesh example: Admin Index with Default Decorator</h2>
9+
<s:url var="url" action="hello" namespace="/">
10+
<s:param name="decorator" value="1"/>
11+
</s:url>
12+
<s:a href="%{url}">Hello</s:a>
13+
</body>
14+
</html>

0 commit comments

Comments
 (0)