You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@struts.apache.org by lu...@apache.org on 2025/05/13 18:42:39 UTC
(struts-examples) 01/01: Adjusts file upload example to action based upload
This is an automated email from the ASF dual-hosted git repository.
lukaszlenart pushed a commit to branch fix/file-upload
in repository https://gitbox.apache.org/repos/asf/struts-examples.git
commit ad0de21a2e5b66c9dc340c2e40c51589c004f76a
Author: Lukasz Lenart <lu...@apache.org>
AuthorDate: Tue May 13 20:42:24 2025 +0200
Adjusts file upload example to action based upload
---
file-upload/pom.xml | 4 +---
.../example/{Upload.java => UploadAction.java} | 26 +++++++++++++---------
file-upload/src/main/resources/struts.xml | 13 +++++++----
.../main/webapp/WEB-INF/{upload.jsp => input.jsp} | 12 ++--------
file-upload/src/main/webapp/WEB-INF/upload.jsp | 13 +++++------
file-upload/src/main/webapp/WEB-INF/web.xml | 8 ++++---
file-upload/src/main/webapp/index.html | 10 ---------
sitemesh3/pom.xml | 4 ++++
sitemesh3/src/main/resources/struts.xml | 12 +++++++++-
sitemesh3/src/main/webapp/WEB-INF/admin/hello.jsp | 14 ++++++++++++
sitemesh3/src/main/webapp/WEB-INF/admin/index.jsp | 14 ++++++++++++
11 files changed, 81 insertions(+), 49 deletions(-)
diff --git a/file-upload/pom.xml b/file-upload/pom.xml
index 679e023..278a676 100644
--- a/file-upload/pom.xml
+++ b/file-upload/pom.xml
@@ -29,9 +29,7 @@
<webApp>
<contextPath>/${project.artifactId}</contextPath>
</webApp>
- <stopKey>CTRL+C</stopKey>
- <stopPort>8999</stopPort>
- <scanIntervalSeconds>10</scanIntervalSeconds>
+ <scan>10</scan>
</configuration>
</plugin>
</plugins>
diff --git a/file-upload/src/main/java/org/apache/struts/example/Upload.java b/file-upload/src/main/java/org/apache/struts/example/UploadAction.java
similarity index 65%
rename from file-upload/src/main/java/org/apache/struts/example/Upload.java
rename to file-upload/src/main/java/org/apache/struts/example/UploadAction.java
index a647763..7797c2e 100644
--- a/file-upload/src/main/java/org/apache/struts/example/Upload.java
+++ b/file-upload/src/main/java/org/apache/struts/example/UploadAction.java
@@ -20,13 +20,18 @@
package org.apache.struts.example;
import org.apache.struts2.ActionSupport;
+import org.apache.struts2.action.UploadedFilesAware;
+import org.apache.struts2.dispatcher.multipart.UploadedFile;
import java.io.File;
+import java.util.Arrays;
+import java.util.List;
+import java.util.stream.Stream;
/**
* <code>Allows upload a file</code>
*/
-public class Upload extends ActionSupport {
+public class UploadAction extends ActionSupport implements UploadedFilesAware {
private File[] upload;
private String[] uploadFileName;
@@ -36,27 +41,26 @@ public class Upload extends ActionSupport {
return INPUT;
}
- public File[] getUpload() {
- return upload;
+ public String upload() throws Exception {
+ return SUCCESS;
}
- public void setUpload(File[] upload) {
- this.upload = upload;
+ public File[] getUpload() {
+ return upload;
}
public String[] getUploadFileName() {
return uploadFileName;
}
- public void setUploadFileName(String[] uploadFileName) {
- this.uploadFileName = uploadFileName;
- }
-
public String[] getUploadContentType() {
return uploadContentType;
}
- public void setUploadContentType(String[] uploadContentType) {
- this.uploadContentType = uploadContentType;
+ @Override
+ public void withUploadedFiles(List<UploadedFile> uploadedFiles) {
+ upload = uploadedFiles.stream().map(UploadedFile::getContent).toArray(File[]::new);
+ uploadFileName = uploadedFiles.stream().map(UploadedFile::getName).toArray(String[]::new);
+ uploadContentType = uploadedFiles.stream().map(UploadedFile::getContentType).toArray(String[]::new);
}
}
diff --git a/file-upload/src/main/resources/struts.xml b/file-upload/src/main/resources/struts.xml
index 1008ab6..8e9bf37 100644
--- a/file-upload/src/main/resources/struts.xml
+++ b/file-upload/src/main/resources/struts.xml
@@ -1,7 +1,7 @@
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
- "-//Apache Software Foundation//DTD Struts Configuration 2.5//EN"
- "https://struts.apache.org/dtds/struts-2.5.dtd">
+ "-//Apache Software Foundation//DTD Struts Configuration 6.0//EN"
+ "https://struts.apache.org/dtds/struts-6.0.dtd">
<struts>
<constant name="struts.enable.DynamicMethodInvocation" value="false"/>
<constant name="struts.devMode" value="true"/>
@@ -12,11 +12,16 @@
<action name="index">
<result type="redirectAction">
- <param name="actionName">upload</param>
+ <param name="actionName">input</param>
</result>
</action>
- <action name="upload" class="org.apache.struts.example.Upload">
+ <action name="input" class="org.apache.struts.example.UploadAction">
+ <result name="input">WEB-INF/input.jsp</result>
+ </action>
+
+ <action name="upload" class="org.apache.struts.example.UploadAction" method="upload">
+ <result>WEB-INF/upload.jsp</result>
<result name="input">WEB-INF/upload.jsp</result>
</action>
diff --git a/file-upload/src/main/webapp/WEB-INF/upload.jsp b/file-upload/src/main/webapp/WEB-INF/input.jsp
similarity index 51%
copy from file-upload/src/main/webapp/WEB-INF/upload.jsp
copy to file-upload/src/main/webapp/WEB-INF/input.jsp
index 247293a..02b247b 100644
--- a/file-upload/src/main/webapp/WEB-INF/upload.jsp
+++ b/file-upload/src/main/webapp/WEB-INF/input.jsp
@@ -2,7 +2,7 @@
<%@ taglib prefix="s" uri="/struts-tags" %>
<html>
<head>
- <title>File upload</title>
+ <title>File upload: input</title>
</head>
<body>
@@ -14,15 +14,7 @@
<s:submit/>
</s:form>
-<s:iterator value="upload" var="u">
- <s:property value="u"/><br/>
-</s:iterator>
-<s:iterator value="uploadContentType" var="ct">
- <s:property value="ct"/><br/>
-</s:iterator>
-<s:iterator value="uploadFileName" var="fn">
- <s:property value="fn"/><br/>
-</s:iterator>
+<s:actionerror/>
</body>
</html>
diff --git a/file-upload/src/main/webapp/WEB-INF/upload.jsp b/file-upload/src/main/webapp/WEB-INF/upload.jsp
index 247293a..62cb89c 100644
--- a/file-upload/src/main/webapp/WEB-INF/upload.jsp
+++ b/file-upload/src/main/webapp/WEB-INF/upload.jsp
@@ -2,17 +2,16 @@
<%@ taglib prefix="s" uri="/struts-tags" %>
<html>
<head>
- <title>File upload</title>
+ <title>File upload: result</title>
</head>
<body>
-<s:form action="upload" method="post" enctype="multipart/form-data">
- <s:file name="upload"/>
- <s:file name="upload"/>
- <s:file name="upload"/>
- <s:submit/>
-</s:form>
+<p>
+<s:a action="input">back to input</s:a>
+</p>
+
+<s:actionerror/>
<s:iterator value="upload" var="u">
<s:property value="u"/><br/>
diff --git a/file-upload/src/main/webapp/WEB-INF/web.xml b/file-upload/src/main/webapp/WEB-INF/web.xml
index 81f7594..d8d850c 100644
--- a/file-upload/src/main/webapp/WEB-INF/web.xml
+++ b/file-upload/src/main/webapp/WEB-INF/web.xml
@@ -1,8 +1,10 @@
<?xml version="1.0" encoding="UTF-8"?>
-<web-app id="struts_blank" version="2.4"
- xmlns="http://java.sun.com/xml/ns/j2ee"
+<web-app xmlns="https://jakarta.ee/xml/ns/jakartaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
- xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
+ xsi:schemaLocation="https://jakarta.ee/xml/ns/jakartaee https://jakarta.ee/xml/ns/jakartaee/web-app_6_0.xsd"
+ metadata-complete="false"
+ version="6.0">
+
<display-name>Struts Blank</display-name>
<filter>
diff --git a/file-upload/src/main/webapp/index.html b/file-upload/src/main/webapp/index.html
deleted file mode 100644
index 89ec7f1..0000000
--- a/file-upload/src/main/webapp/index.html
+++ /dev/null
@@ -1,10 +0,0 @@
-<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
-<html>
-<head>
- <META HTTP-EQUIV="Refresh" CONTENT="0;URL=upload.action">
-</head>
-
-<body>
-<p>Loading ...</p>
-</body>
-</html>
diff --git a/sitemesh3/pom.xml b/sitemesh3/pom.xml
index 7f31a85..5e55393 100644
--- a/sitemesh3/pom.xml
+++ b/sitemesh3/pom.xml
@@ -13,6 +13,10 @@
<packaging>war</packaging>
+ <properties>
+ <struts2.version>7.0.4-SNAPSHOT</struts2.version>
+ </properties>
+
<dependencies>
<dependency>
<groupId>org.sitemesh</groupId>
diff --git a/sitemesh3/src/main/resources/struts.xml b/sitemesh3/src/main/resources/struts.xml
index 935d1b8..7048cff 100644
--- a/sitemesh3/src/main/resources/struts.xml
+++ b/sitemesh3/src/main/resources/struts.xml
@@ -8,7 +8,7 @@
<constant name="struts.action.extension" value=""/>
<constant name="struts.custom.i18n.resources" value="DefaultMessages"/>
- <package name="default" extends="struts-default">
+ <package name="default" extends="struts-default" namespace="/">
<default-action-ref name="index"/>
<action name="index">
<result>/WEB-INF/index.jsp</result>
@@ -21,4 +21,14 @@
</action>
</package>
+
+ <package name="default2" extends="struts-default" namespace="/admin">
+ <default-action-ref name="index"/>
+ <action name="index">
+ <result>/WEB-INF/admin/index.jsp</result>
+ </action>
+ <action name="hello">
+ <result>/WEB-INF/admin/hello.jsp</result>
+ </action>
+ </package>
</struts>
diff --git a/sitemesh3/src/main/webapp/WEB-INF/admin/hello.jsp b/sitemesh3/src/main/webapp/WEB-INF/admin/hello.jsp
new file mode 100644
index 0000000..b3e711e
--- /dev/null
+++ b/sitemesh3/src/main/webapp/WEB-INF/admin/hello.jsp
@@ -0,0 +1,14 @@
+<%@ page contentType="text/html; charset=UTF-8" %>
+<%@ taglib prefix="s" uri="/struts-tags" %>
+<html>
+<head>
+ <title>SiteMesh example: Admin Index</title>
+</head>
+<body>
+<h2>SiteMesh example: Admin Index with Default Decorator</h2>
+<s:url var="url" action="hello" namespace="/">
+ <s:param name="decorator" value="1"/>
+</s:url>
+<s:a href="%{url}">Hello</s:a>
+</body>
+</html>
\ No newline at end of file
diff --git a/sitemesh3/src/main/webapp/WEB-INF/admin/index.jsp b/sitemesh3/src/main/webapp/WEB-INF/admin/index.jsp
new file mode 100644
index 0000000..b3e711e
--- /dev/null
+++ b/sitemesh3/src/main/webapp/WEB-INF/admin/index.jsp
@@ -0,0 +1,14 @@
+<%@ page contentType="text/html; charset=UTF-8" %>
+<%@ taglib prefix="s" uri="/struts-tags" %>
+<html>
+<head>
+ <title>SiteMesh example: Admin Index</title>
+</head>
+<body>
+<h2>SiteMesh example: Admin Index with Default Decorator</h2>
+<s:url var="url" action="hello" namespace="/">
+ <s:param name="decorator" value="1"/>
+</s:url>
+<s:a href="%{url}">Hello</s:a>
+</body>
+</html>
\ No newline at end of file