Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
49 changes: 49 additions & 0 deletions action-extension/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
# Action Extension

This example shows the three places where an action extension can be set in Apache Struts:
globally, on a `<package>`, and on a single `<action>`. It covers both which request URLs reach
an action and which extension the Struts tags use when they generate a URL to it.

Requires Apache Struts 7.5.0 or later.

## Run

```bash
./mvnw -pl action-extension jetty:run
```

Then open http://localhost:8080/action-extension/

## What is configured

| Layer | Config in `struts.xml` | Action | URL |
|---|---|---|---|
| Global | `<constant name="struts.action.extension" value="action"/>` | `hello` | `/hello.action` |
| Package | `<package name="legacy" namespace="/legacy" extension="do">` | `orders` | `/legacy/orders.do` |
| Action | `<action name="report" extension="">` | `report` | `/legacy/report` |

An action's extensions are taken from the most specific place that declares them:

action → package → parent packages → `struts.action.extension`

Every place uses the same format: a comma-separated list. The first entry is the default, and an
empty entry means "no extension" (`extension=""`, or `action,,`).

## Generated URLs

`<s:url>`, `<s:a>`, `<s:form>` and the `redirectAction` result build the URL from the
**target** action's extensions. The URL keeps the current page's extension when the target
accepts it, otherwise it uses the target's first entry. Every list in this example has a single
entry, so a link to `orders` from `/legacy/report` comes out as `/legacy/orders.do`, and a link to
`hello` from `/legacy/orders.do` comes out as `/hello.action`.

## Wrong extensions

- `/legacy/orders.action` and `/legacy/report.do` reach an action that declares its own
extensions. The `actionExtension` interceptor rejects them and returns the `bad-extension`
result. Without a `bad-extension` result, the response is a 404.
- `/hello.do` is a plain 404. `hello` declares no extension, so only the global list applies to
it, and `.do` is never mapped to it.

Extensions control which URL is the canonical one for an action. They are not access control:
do not use them to hide actions.
51 changes: 51 additions & 0 deletions action-extension/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
<?xml version="1.0"?>
<project
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd"
xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.apache.struts</groupId>
<artifactId>struts-examples</artifactId>
<version>2.0.0</version>
</parent>

<artifactId>action-extension</artifactId>
<name>Action Extension Apache Struts Example Application</name>
<description>Shows global, package-level and action-level action extensions in Apache Struts</description>

<packaging>war</packaging>

<dependencies>
<dependency>
<groupId>org.apache.struts</groupId>
<artifactId>struts2-junit-plugin</artifactId>
<version>${struts2.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>${spring-web.version}</version>
<scope>test</scope>
</dependency>
</dependencies>

<build>
<finalName>action-extension</finalName>
<plugins>
<plugin>
<groupId>org.eclipse.jetty</groupId>
<artifactId>jetty-maven-plugin</artifactId>
<version>${jetty-plugin.version}</version>
<configuration>
<webApp>
<contextPath>/${project.artifactId}</contextPath>
</webApp>
<stopKey>CTRL+C</stopKey>
<stopPort>8999</stopPort>
<scan>10</scan>
</configuration>
</plugin>
</plugins>
</build>
</project>
15 changes: 15 additions & 0 deletions action-extension/src/main/resources/log4j2.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?xml version="1.0" encoding="UTF-8"?>
<Configuration>
<Appenders>
<Console name="STDOUT" target="SYSTEM_OUT">
<PatternLayout pattern="%d %-5p [%t] %C{2} (%F:%L) - %m%n"/>
</Console>
</Appenders>
<Loggers>
<Logger name="org.apache.struts2" level="info"/>
<Logger name="org.apache.struts2.helloworld" level="debug"/>
<Root level="warn">
<AppenderRef ref="STDOUT"/>
</Root>
</Loggers>
</Configuration>
41 changes: 41 additions & 0 deletions action-extension/src/main/resources/struts.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 7.5//EN"
"https://struts.apache.org/dtds/struts-7.5.dtd">
<struts>
<constant name="struts.enable.DynamicMethodInvocation" value="false"/>
<constant name="struts.devMode" value="true"/>

<!-- Global: actions that declare nothing answer only on .action -->
<constant name="struts.action.extension" value="action"/>

<package name="default" namespace="/" extends="struts-default">
<global-results>
<result name="bad-extension">/WEB-INF/jsp/badExtension.jsp</result>
</global-results>

<action name="index">
<result>/WEB-INF/jsp/index.jsp</result>
</action>

<action name="hello">
<result>/WEB-INF/jsp/hello.jsp</result>
</action>
</package>

<!-- Package override: every action in this package answers on .do -->
<package name="legacy" namespace="/legacy" extends="struts-default" extension="do">
<global-results>
<result name="bad-extension">/WEB-INF/jsp/badExtension.jsp</result>
</global-results>

<action name="orders">
<result>/WEB-INF/jsp/orders.jsp</result>
</action>

<!-- Action override: an empty extension means no extension at all -->
<action name="report" extension="">
<result>/WEB-INF/jsp/report.jsp</result>
</action>
</package>
</struts>
16 changes: 16 additions & 0 deletions action-extension/src/main/webapp/WEB-INF/jsp/badExtension.jsp
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<!DOCTYPE html>
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8" %>
<%@ taglib prefix="s" uri="/struts-tags" %>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Action Extension - Wrong extension</title>
</head>
<body>
<h1>Wrong extension</h1>
<p>The request to <code><s:property value="#request['jakarta.servlet.forward.request_uri']"/></code> was rejected
by the <code>actionExtension</code> interceptor. The action exists, but its declared extensions do not include the one in this URL.</p>
<p>The interceptor returned the <code>bad-extension</code> result, which this example maps to this page. Without that result, the response would be a 404.</p>
<p><a href="<s:url action='index' namespace='/'/>">Back to the start page</a></p>
</body>
</html>
15 changes: 15 additions & 0 deletions action-extension/src/main/webapp/WEB-INF/jsp/hello.jsp
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<!DOCTYPE html>
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8" %>
<%@ taglib prefix="s" uri="/struts-tags" %>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Action Extension - Global</title>
</head>
<body>
<h1>Global extension</h1>
<p>You reached <code>hello</code> in the <code>default</code> package. Neither the action nor its package declares an extension, so it answers on the global <code>struts.action.extension</code> value: <code>.action</code>.</p>
<%@ include file="links.jspf" %>
<p><a href="<s:url action='index' namespace='/'/>">Back to the start page</a></p>
</body>
</html>
20 changes: 20 additions & 0 deletions action-extension/src/main/webapp/WEB-INF/jsp/index.jsp
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<!DOCTYPE html>
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8" %>
<%@ taglib prefix="s" uri="/struts-tags" %>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Action Extension - Welcome</title>
</head>
<body>
<h1>Action extensions in Apache Struts</h1>
<p>An action extension can be set in three places. Struts uses the most specific one: the action, then its package, then parent packages, then the global <code>struts.action.extension</code> constant. See <code>struts.xml</code>.</p>
<p>The wrong-extension links are plain <code>&lt;a href&gt;</code> links, because <code>&lt;s:url&gt;</code> would always fix the extension.</p>
<ul>
<li><code>orders.action</code> and <code>report.do</code> reach an action that declares its own extensions, so the <code>actionExtension</code> interceptor rejects them and shows the <code>bad-extension</code> result.</li>
<li><code>hello.do</code> is a plain 404. <code>hello</code> declares no extension, so only the global list (<code>action</code>) applies to it, and <code>.do</code> is never mapped to it.</li>
</ul>
<%@ include file="links.jspf" %>
<p><a href="<s:url action='index' namespace='/'/>">Back to the start page</a></p>
</body>
</html>
28 changes: 28 additions & 0 deletions action-extension/src/main/webapp/WEB-INF/jsp/links.jspf
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<%@ page pageEncoding="UTF-8" %>
<%@ taglib prefix="s" uri="/struts-tags" %>
<s:url var="helloUrl" action="hello" namespace="/"/>
<s:url var="ordersUrl" action="orders" namespace="/legacy"/>
<s:url var="reportUrl" action="report" namespace="/legacy"/>
<h2>Generated links</h2>
<p>Each link is built with <code>&lt;s:url&gt;</code>, which uses the target action's extensions: it keeps this page's extension only when the target accepts it, otherwise it uses the target's first entry.</p>
<table>
<tr><th>Layer</th><th>Link</th><th>Generated URL</th><th>Wrong extension</th></tr>
<tr>
<td>Global (<code>struts.action.extension</code>)</td>
<td><s:a href="%{#helloUrl}">hello</s:a></td>
<td><code><s:property value="#helloUrl"/></code></td>
<td><a href="${pageContext.request.contextPath}/hello.do">hello.do</a> (plain 404)</td>
</tr>
<tr>
<td>Package (<code>&lt;package extension="do"&gt;</code>)</td>
<td><s:a href="%{#ordersUrl}">orders</s:a></td>
<td><code><s:property value="#ordersUrl"/></code></td>
<td><a href="${pageContext.request.contextPath}/legacy/orders.action">orders.action</a> (rejected)</td>
</tr>
<tr>
<td>Action (<code>&lt;action extension=""&gt;</code>)</td>
<td><s:a href="%{#reportUrl}">report</s:a></td>
<td><code><s:property value="#reportUrl"/></code></td>
<td><a href="${pageContext.request.contextPath}/legacy/report.do">report.do</a> (rejected)</td>
</tr>
</table>
15 changes: 15 additions & 0 deletions action-extension/src/main/webapp/WEB-INF/jsp/orders.jsp
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<!DOCTYPE html>
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8" %>
<%@ taglib prefix="s" uri="/struts-tags" %>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Action Extension - Package</title>
</head>
<body>
<h1>Package extension</h1>
<p>You reached <code>orders</code> in the <code>legacy</code> package. The package declares <code>extension="do"</code>, so every action in it answers on <code>.do</code> unless the action declares its own.</p>
<%@ include file="links.jspf" %>
<p><a href="<s:url action='index' namespace='/'/>">Back to the start page</a></p>
</body>
</html>
15 changes: 15 additions & 0 deletions action-extension/src/main/webapp/WEB-INF/jsp/report.jsp
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<!DOCTYPE html>
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8" %>
<%@ taglib prefix="s" uri="/struts-tags" %>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Action Extension - Action</title>
</head>
<body>
<h1>Action extension</h1>
<p>You reached <code>report</code> in the <code>legacy</code> package. The action declares <code>extension=""</code>, which overrides the package's <code>do</code>. An empty entry means no extension, so the URL is <code>/legacy/report</code>.</p>
<%@ include file="links.jspf" %>
<p><a href="<s:url action='index' namespace='/'/>">Back to the start page</a></p>
</body>
</html>
21 changes: 21 additions & 0 deletions action-extension/src/main/webapp/WEB-INF/web.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?xml version="1.0" encoding="UTF-8"?>
<web-app id="WebApp_ID" version="6.0"
xmlns="https://jakarta.ee/xml/ns/jakartaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="https://jakarta.ee/xml/ns/jakartaee https://jakarta.ee/xml/ns/jakartaee/web-app_6_0.xsd">
<display-name>Action Extension Apache Struts Example</display-name>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>

<filter>
<filter-name>struts2</filter-name>
<filter-class>org.apache.struts2.dispatcher.filter.StrutsPrepareAndExecuteFilter</filter-class>
</filter>

<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>

</web-app>
1 change: 1 addition & 0 deletions action-extension/src/main/webapp/index.jsp
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<% response.sendRedirect("index.action"); %>
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
/*
* 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_example;

import org.apache.struts2.ActionSupport;
import org.apache.struts2.dispatcher.mapper.ActionMapping;
import org.apache.struts2.junit.StrutsJUnit4TestCase;
import org.junit.Test;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNull;

public class ActionExtensionTest extends StrutsJUnit4TestCase<Object> {

@Test
public void testGlobalExtensionMapsHello() {
ActionMapping mapping = getActionMapping("/hello.action");

assertNotNull(mapping);
assertEquals("hello", mapping.getName());
assertEquals("/", mapping.getNamespace());
assertEquals("action", mapping.getExtension());
}

@Test
public void testPackageExtensionMapsOrders() {
ActionMapping mapping = getActionMapping("/legacy/orders.do");

assertNotNull(mapping);
assertEquals("orders", mapping.getName());
assertEquals("/legacy", mapping.getNamespace());
assertEquals("do", mapping.getExtension());
}

@Test
public void testActionExtensionMapsReport() {
ActionMapping mapping = getActionMapping("/legacy/report");

assertNotNull(mapping);
assertEquals("report", mapping.getName());
assertEquals("/legacy", mapping.getNamespace());
}

@Test
public void testUndeclaredActionDoesNotClaimDo() {
assertNull(getActionMapping("/hello.do"));
}

@Test
public void testHelloExecutesOnAction() throws Exception {
assertEquals(ActionSupport.SUCCESS, getActionProxy("/hello.action").execute());
}

@Test
public void testOrdersExecutesOnDo() throws Exception {
assertEquals(ActionSupport.SUCCESS, getActionProxy("/legacy/orders.do").execute());
}

@Test
public void testReportExecutesWithoutExtension() throws Exception {
assertEquals(ActionSupport.SUCCESS, getActionProxy("/legacy/report").execute());
}

@Test
public void testOrdersRejectsActionExtension() throws Exception {
assertEquals("bad-extension", getActionProxy("/legacy/orders.action").execute());
}

@Test
public void testReportRejectsDoExtension() throws Exception {
assertEquals("bad-extension", getActionProxy("/legacy/report.do").execute());
}
}
Loading
Loading