This is an automated email from the ASF dual-hosted git repository. lukaszlenart pushed a commit to branch feature/sitemesh3-upload in repository https://gitbox.apache.org/repos/asf/struts-examples.git
commit 51baa1194064be94afa5114adeef0c9872bfe9c1 Author: Lukasz Lenart <lukaszlen...@apache.org> AuthorDate: Tue May 20 06:57:40 2025 +0200 Integrates upload example with Sitemesh 3 It doesn't work with the latest changes but works with 7.0.3 --- file-upload/src/main/webapp/WEB-INF/web.xml | 8 +-- sitemesh3/pom.xml | 21 +++----- .../struts/examples/sitemesh3/UploadAction.java | 60 ++++++++++++++++++++++ sitemesh3/src/main/resources/log4j2.xml | 4 +- sitemesh3/src/main/resources/struts.xml | 3 ++ sitemesh3/src/main/webapp/WEB-INF/admin/hello.jsp | 3 +- sitemesh3/src/main/webapp/WEB-INF/admin/index.jsp | 3 +- .../main/webapp/WEB-INF/decorators/decorator.html | 2 +- .../main/webapp/WEB-INF/decorators/decorator1.html | 2 +- .../main/webapp/WEB-INF/decorators/decorator2.html | 2 +- .../{decorator2.html => upload-decorator.html} | 4 +- sitemesh3/src/main/webapp/WEB-INF/hello1.jsp | 1 + sitemesh3/src/main/webapp/WEB-INF/hello2.jsp | 3 +- sitemesh3/src/main/webapp/WEB-INF/index.jsp | 2 + sitemesh3/src/main/webapp/WEB-INF/sitemesh3.xml | 1 + sitemesh3/src/main/webapp/WEB-INF/upload.jsp | 30 +++++++++++ sitemesh3/src/main/webapp/WEB-INF/web.xml | 4 +- sitemesh3/src/main/webapp/index.html | 10 ---- 18 files changed, 123 insertions(+), 40 deletions(-) diff --git a/file-upload/src/main/webapp/WEB-INF/web.xml b/file-upload/src/main/webapp/WEB-INF/web.xml index d8d850c..34471f9 100644 --- a/file-upload/src/main/webapp/WEB-INF/web.xml +++ b/file-upload/src/main/webapp/WEB-INF/web.xml @@ -9,14 +9,16 @@ <filter> <filter-name>struts2</filter-name> - <filter-class> - org.apache.struts2.dispatcher.filter.StrutsPrepareAndExecuteFilter - </filter-class> + <filter-class>org.apache.struts2.dispatcher.filter.StrutsPrepareAndExecuteFilter</filter-class> + <dispatcher>FORWARD</dispatcher> + <dispatcher>REQUEST</dispatcher> </filter> <filter-mapping> <filter-name>struts2</filter-name> <url-pattern>/*</url-pattern> + <dispatcher>FORWARD</dispatcher> + <dispatcher>REQUEST</dispatcher> </filter-mapping> <welcome-file-list> diff --git a/sitemesh3/pom.xml b/sitemesh3/pom.xml index 5e55393..e07af91 100644 --- a/sitemesh3/pom.xml +++ b/sitemesh3/pom.xml @@ -14,7 +14,13 @@ <packaging>war</packaging> <properties> + <!-- + it doesn't work with latest changes + https://github.com/apache/struts/pull/1265 <struts2.version>7.0.4-SNAPSHOT</struts2.version> + --> + <!-- it works with version where FORWARD_SERVLET_PATH is updated --> + <struts2.version>7.0.3</struts2.version> </properties> <dependencies> @@ -35,21 +41,6 @@ <artifactId>log4j-slf4j-impl</artifactId> <version>${log4j2.version}</version> </dependency> - - <dependency> - <groupId>org.apache.struts</groupId> - <artifactId>struts2-junit-plugin</artifactId> - <version>${struts2.version}</version> - <scope>test</scope> - </dependency> - - <dependency> - <groupId>junit</groupId> - <artifactId>junit</artifactId> - <version>4.13.2</version> - <scope>test</scope> - </dependency> - </dependencies> <build> diff --git a/sitemesh3/src/main/java/org/apache/struts/examples/sitemesh3/UploadAction.java b/sitemesh3/src/main/java/org/apache/struts/examples/sitemesh3/UploadAction.java new file mode 100644 index 0000000..c2b3f85 --- /dev/null +++ b/sitemesh3/src/main/java/org/apache/struts/examples/sitemesh3/UploadAction.java @@ -0,0 +1,60 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +package org.apache.struts.examples.sitemesh3; + +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.List; + +/** + * <code>Allows upload a file</code> + */ +public class UploadAction extends ActionSupport implements UploadedFilesAware { + + private File[] upload; + private String[] uploadFileName; + private String[] uploadContentType; + + public String execute() throws Exception { + return INPUT; + } + + public File[] getUpload() { + return upload; + } + + public String[] getUploadFileName() { + return uploadFileName; + } + + public String[] getUploadContentType() { + return 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/sitemesh3/src/main/resources/log4j2.xml b/sitemesh3/src/main/resources/log4j2.xml index 1f4e3df..9793da5 100644 --- a/sitemesh3/src/main/resources/log4j2.xml +++ b/sitemesh3/src/main/resources/log4j2.xml @@ -2,13 +2,13 @@ <Configuration> <Appenders> <Console name="STDOUT" target="SYSTEM_OUT"> - <PatternLayout pattern="[%-5p] %C{2} (%F:%L) - %m%n"/> + <PatternLayout pattern="[%p] %C{2} (%F:%L): %m%n"/> </Console> </Appenders> <Loggers> <Root level="info"> <AppenderRef ref="STDOUT"/> </Root> - <Logger name="org.apache.struts2.tiles" level="trace"/> + <Logger name="org.apache.struts2" level="info"/> </Loggers> </Configuration> diff --git a/sitemesh3/src/main/resources/struts.xml b/sitemesh3/src/main/resources/struts.xml index 7048cff..875a3db 100644 --- a/sitemesh3/src/main/resources/struts.xml +++ b/sitemesh3/src/main/resources/struts.xml @@ -20,6 +20,9 @@ <result name="error">/WEB-INF/error.jsp</result> </action> + <action name="upload" class="org.apache.struts.examples.sitemesh3.UploadAction"> + <result name="input">/WEB-INF/upload.jsp</result> + </action> </package> <package name="default2" extends="struts-default" namespace="/admin"> diff --git a/sitemesh3/src/main/webapp/WEB-INF/admin/hello.jsp b/sitemesh3/src/main/webapp/WEB-INF/admin/hello.jsp index b3e711e..c1ded3a 100644 --- a/sitemesh3/src/main/webapp/WEB-INF/admin/hello.jsp +++ b/sitemesh3/src/main/webapp/WEB-INF/admin/hello.jsp @@ -6,9 +6,10 @@ </head> <body> <h2>SiteMesh example: Admin Index with Default Decorator</h2> -<s:url var="url" action="hello" namespace="/"> +<s:url var="url" action="hello" namespace="/admin"> <s:param name="decorator" value="1"/> </s:url> <s:a href="%{url}">Hello</s:a> +<s:a action="index" namespace="/admin">Admin Index</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 index b3e711e..3494e57 100644 --- a/sitemesh3/src/main/webapp/WEB-INF/admin/index.jsp +++ b/sitemesh3/src/main/webapp/WEB-INF/admin/index.jsp @@ -6,7 +6,8 @@ </head> <body> <h2>SiteMesh example: Admin Index with Default Decorator</h2> -<s:url var="url" action="hello" namespace="/"> +<s:a action="index" namespace="/">Index</s:a> +<s:url var="url" action="hello" namespace="/admin"> <s:param name="decorator" value="1"/> </s:url> <s:a href="%{url}">Hello</s:a> diff --git a/sitemesh3/src/main/webapp/WEB-INF/decorators/decorator.html b/sitemesh3/src/main/webapp/WEB-INF/decorators/decorator.html index 2d7835d..3088e6c 100644 --- a/sitemesh3/src/main/webapp/WEB-INF/decorators/decorator.html +++ b/sitemesh3/src/main/webapp/WEB-INF/decorators/decorator.html @@ -1,4 +1,4 @@ -<html> +<html lang="en"> <head> <title><sitemesh:write property="title"/></title> <sitemesh:write property="head"/> diff --git a/sitemesh3/src/main/webapp/WEB-INF/decorators/decorator1.html b/sitemesh3/src/main/webapp/WEB-INF/decorators/decorator1.html index bbaf908..fd43401 100644 --- a/sitemesh3/src/main/webapp/WEB-INF/decorators/decorator1.html +++ b/sitemesh3/src/main/webapp/WEB-INF/decorators/decorator1.html @@ -1,4 +1,4 @@ -<html> +<html lang="en"> <head> <title><sitemesh:write property="title"/></title> <sitemesh:write property="head"/> diff --git a/sitemesh3/src/main/webapp/WEB-INF/decorators/decorator2.html b/sitemesh3/src/main/webapp/WEB-INF/decorators/decorator2.html index 57a92c5..db542cd 100644 --- a/sitemesh3/src/main/webapp/WEB-INF/decorators/decorator2.html +++ b/sitemesh3/src/main/webapp/WEB-INF/decorators/decorator2.html @@ -1,4 +1,4 @@ -<html> +<html lang="en"> <head> <title><sitemesh:write property="title"/></title> <sitemesh:write property="head"/> diff --git a/sitemesh3/src/main/webapp/WEB-INF/decorators/decorator2.html b/sitemesh3/src/main/webapp/WEB-INF/decorators/upload-decorator.html similarity index 79% copy from sitemesh3/src/main/webapp/WEB-INF/decorators/decorator2.html copy to sitemesh3/src/main/webapp/WEB-INF/decorators/upload-decorator.html index 57a92c5..bdeb46e 100644 --- a/sitemesh3/src/main/webapp/WEB-INF/decorators/decorator2.html +++ b/sitemesh3/src/main/webapp/WEB-INF/decorators/upload-decorator.html @@ -1,10 +1,10 @@ -<html> +<html lang="en"> <head> <title><sitemesh:write property="title"/></title> <sitemesh:write property="head"/> </head> <body> -<h1>Decorator 2</h1> +<h1>Upload Decorator</h1> <sitemesh:write property="body"/> </body> </html> diff --git a/sitemesh3/src/main/webapp/WEB-INF/hello1.jsp b/sitemesh3/src/main/webapp/WEB-INF/hello1.jsp index dee6bed..91928fd 100644 --- a/sitemesh3/src/main/webapp/WEB-INF/hello1.jsp +++ b/sitemesh3/src/main/webapp/WEB-INF/hello1.jsp @@ -14,5 +14,6 @@ </s:form> </div> <p>Selected decorator: <s:property value="decorator"/></p> +<s:a action="index">Index</s:a> </body> </html> \ No newline at end of file diff --git a/sitemesh3/src/main/webapp/WEB-INF/hello2.jsp b/sitemesh3/src/main/webapp/WEB-INF/hello2.jsp index f9a2da9..124f389 100644 --- a/sitemesh3/src/main/webapp/WEB-INF/hello2.jsp +++ b/sitemesh3/src/main/webapp/WEB-INF/hello2.jsp @@ -5,7 +5,7 @@ <title>SiteMesh example: Hello 2 with Decorator 2</title> </head> <body> -<h2>SiteMesh example: Hello 2 with Decorator 2</h1> +<h2>SiteMesh example: Hello 2 with Decorator 2</h2> <h3>Decorators</h3> <div> <s:form action="hello" method="get"> @@ -14,5 +14,6 @@ </s:form> </div> <p>Selected decorator: <s:property value="decorator"/></p> +<s:a action="index">Index</s:a> </body> </html> \ No newline at end of file diff --git a/sitemesh3/src/main/webapp/WEB-INF/index.jsp b/sitemesh3/src/main/webapp/WEB-INF/index.jsp index 0bbc7cf..1c49bda 100644 --- a/sitemesh3/src/main/webapp/WEB-INF/index.jsp +++ b/sitemesh3/src/main/webapp/WEB-INF/index.jsp @@ -10,5 +10,7 @@ <s:param name="decorator" value="1"/> </s:url> <s:a href="%{url}">Hello</s:a> +<s:a action="index" namespace="/admin">Admin</s:a> +<s:a action="upload">Upload</s:a> </body> </html> \ No newline at end of file diff --git a/sitemesh3/src/main/webapp/WEB-INF/sitemesh3.xml b/sitemesh3/src/main/webapp/WEB-INF/sitemesh3.xml index f880362..3491a25 100644 --- a/sitemesh3/src/main/webapp/WEB-INF/sitemesh3.xml +++ b/sitemesh3/src/main/webapp/WEB-INF/sitemesh3.xml @@ -3,4 +3,5 @@ <mapping path="/WEB-INF/error.jsp" exclude="true"/> <mapping path="/WEB-INF/hello1.jsp" decorator="decorator1.html"/> <mapping path="/WEB-INF/hello2.jsp" decorator="decorator2.html"/> + <mapping path="/WEB-INF/upload.jsp" decorator="upload-decorator.html"/> </sitemesh> diff --git a/sitemesh3/src/main/webapp/WEB-INF/upload.jsp b/sitemesh3/src/main/webapp/WEB-INF/upload.jsp new file mode 100644 index 0000000..0674986 --- /dev/null +++ b/sitemesh3/src/main/webapp/WEB-INF/upload.jsp @@ -0,0 +1,30 @@ +<%@ page contentType="text/html; charset=UTF-8" %> +<%@ taglib prefix="s" uri="/struts-tags" %> +<html> +<head> + <title>Sitemesh example: File upload</title> +</head> +<body> +<h2>Sitemesh example: Upload with Upload Decorator</h2> +<s:form action="upload" method="post" enctype="multipart/form-data" theme="xhtml"> + <s:file name="upload"/> + <s:file name="upload"/> + <s:file name="upload"/> + <s:submit/> +</s:form> + +<s:actionerror/> + +<s:iterator value="upload" var="u"> + File: <s:property value="u"/><br/> +</s:iterator> +<s:iterator value="uploadContentType" var="ct"> + Content-Type: <s:property value="ct"/><br/> +</s:iterator> +<s:iterator value="uploadFileName" var="fn"> + File name: <s:property value="fn"/><br/> +</s:iterator> + +<s:a action="index">Index</s:a> +</body> +</html> diff --git a/sitemesh3/src/main/webapp/WEB-INF/web.xml b/sitemesh3/src/main/webapp/WEB-INF/web.xml index ff5bac7..a09fa7b 100644 --- a/sitemesh3/src/main/webapp/WEB-INF/web.xml +++ b/sitemesh3/src/main/webapp/WEB-INF/web.xml @@ -5,7 +5,7 @@ http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" version="3.1"> - <display-name>Struts Tiles 2 Demo</display-name> + <display-name>Struts Sitemesh 3 Demo</display-name> <filter> <filter-name>struts2-prepare</filter-name> @@ -38,7 +38,7 @@ </filter-mapping> <welcome-file-list> - <welcome-file>/index.html</welcome-file> + <welcome-file>/index</welcome-file> </welcome-file-list> </web-app> diff --git a/sitemesh3/src/main/webapp/index.html b/sitemesh3/src/main/webapp/index.html deleted file mode 100644 index abdee36..0000000 --- a/sitemesh3/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=index.action"> -</head> - -<body> -<p>Loading ...</p> -</body> -</html>