Skip to content

Commit

Permalink
Extends example with Sitemesh 3 to support multiple decorators
Browse files Browse the repository at this point in the history
  • Loading branch information
lukaszlenart committed Nov 15, 2024
1 parent f6dcf44 commit 57a6130
Show file tree
Hide file tree
Showing 13 changed files with 148 additions and 36 deletions.
4 changes: 4 additions & 0 deletions sitemesh3/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,10 @@

<packaging>war</packaging>

<properties>
<struts2.version>7.0.0-M11-SNAPSHOT</struts2.version>
</properties>

<dependencies>
<dependency>
<groupId>org.sitemesh</groupId>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package org.apache.struts.examples.sitemesh3;

import org.apache.struts2.ActionSupport;
import org.apache.struts2.interceptor.parameter.StrutsParameter;

import java.util.Collections;
import java.util.Map;
import java.util.TreeMap;

public class HelloAction extends ActionSupport {

private static final Map<String, String> DECORATORS = Collections.unmodifiableSortedMap(new TreeMap<>() {{
put("1", "Decorator 1");
put("2", "Decorator 2");
put("3", "Exclude from decorating");
}});

private String decorator;

@Override
public String execute() throws Exception {
if ("1".equals(decorator)) {
return SUCCESS;
} else if ("2".equals(decorator)) {
return "other";
}
addActionError("Wrong decorator: " + decorator);
return ERROR;
}

public String getDecorator() {
return decorator;
}

@StrutsParameter
public void setDecorator(String decorator) {
this.decorator = decorator;
}

public Map<String, String> getDecorators() {
return DECORATORS;
}
}
14 changes: 8 additions & 6 deletions sitemesh3/src/main/resources/struts.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,21 @@
"https://struts.apache.org/dtds/struts-6.0.dtd">
<struts>
<constant name="struts.devMode" value="true"/>
<constant name="struts.ui.theme" value="simple"/>
<constant name="struts.action.extension" value=""/>
<constant name="struts.custom.i18n.resources" value="DefaultMessages"/>

<package name="default" extends="struts-default">
<default-action-ref name="index"/>
<action name="index">
<result type="redirectAction">
<param name="actionName">hello</param>
</result>
<result>/WEB-INF/index.jsp</result>
</action>

<action name="hello">
<result>/WEB-INF/hello.jsp</result>
</action>
<action name="hello" class="org.apache.struts.examples.sitemesh3.HelloAction">
<result name="success">/WEB-INF/hello1.jsp</result>
<result name="other">/WEB-INF/hello2.jsp</result>
<result name="error">/WEB-INF/error.jsp</result>
</action>

</package>
</struts>
3 changes: 2 additions & 1 deletion sitemesh3/src/main/webapp/WEB-INF/decorators/decorator.html
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
<sitemesh:write property="head"/>
</head>
<body>
<h1>Default Decorator</h1>
<sitemesh:write property="body"/>
</body>
</html>
</html>
10 changes: 10 additions & 0 deletions sitemesh3/src/main/webapp/WEB-INF/decorators/decorator1.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<html>
<head>
<title><sitemesh:write property="title"/></title>
<sitemesh:write property="head"/>
</head>
<body>
<h1>Decorator 1</h1>
<sitemesh:write property="body"/>
</body>
</html>
10 changes: 10 additions & 0 deletions sitemesh3/src/main/webapp/WEB-INF/decorators/decorator2.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<html>
<head>
<title><sitemesh:write property="title"/></title>
<sitemesh:write property="head"/>
</head>
<body>
<h1>Decorator 2</h1>
<sitemesh:write property="body"/>
</body>
</html>
16 changes: 16 additions & 0 deletions sitemesh3/src/main/webapp/WEB-INF/error.jsp
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<%@ page contentType="text/html; charset=UTF-8" %>
<%@ taglib prefix="s" uri="/struts-tags" %>
<html>
<head>
<title>SiteMesh example: error</title>
</head>
<body>
<h2>SiteMesh example: error</h2>
<s:actionerror/>

<s:url var="url" action="hello">
<s:param name="decorator" value="1"/>
</s:url>
<s:a href="%{url}">Hello</s:a>
</body>
</html>
27 changes: 0 additions & 27 deletions sitemesh3/src/main/webapp/WEB-INF/hello.jsp

This file was deleted.

18 changes: 18 additions & 0 deletions sitemesh3/src/main/webapp/WEB-INF/hello1.jsp
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<%@ page contentType="text/html; charset=UTF-8" %>
<%@ taglib prefix="s" uri="/struts-tags" %>
<html>
<head>
<title>SiteMesh example: Hello 1 with Decorator 1</title>
</head>
<body>
<h2>SiteMesh example: Hello 1 with Decorator 1</h2>
<h3>Decorators</h3>
<div>
<s:form action="hello" method="get">
<s:select name="decorator" list="decorators"/>
<s:submit value="Decorate"/>
</s:form>
</div>
<p>Selected decorator: <s:property value="decorator"/></p>
</body>
</html>
18 changes: 18 additions & 0 deletions sitemesh3/src/main/webapp/WEB-INF/hello2.jsp
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<%@ page contentType="text/html; charset=UTF-8" %>
<%@ taglib prefix="s" uri="/struts-tags" %>
<html>
<head>
<title>SiteMesh example: Hello 2 with Decorator 2</title>
</head>
<body>
<h2>SiteMesh example: Hello 2 with Decorator 2</h1>
<h3>Decorators</h3>
<div>
<s:form action="hello" method="get">
<s:select name="decorator" list="decorators"/>
<s:submit value="Decorate"/>
</s:form>
</div>
<p>Selected decorator: <s:property value="decorator"/></p>
</body>
</html>
14 changes: 14 additions & 0 deletions sitemesh3/src/main/webapp/WEB-INF/index.jsp
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<%@ page contentType="text/html; charset=UTF-8" %>
<%@ taglib prefix="s" uri="/struts-tags" %>
<html>
<head>
<title>SiteMesh example: Index</title>
</head>
<body>
<h2>SiteMesh example: Index with Default Decorator</h2>
<s:url var="url" action="hello">
<s:param name="decorator" value="1"/>
</s:url>
<s:a href="%{url}">Hello</s:a>
</body>
</html>
5 changes: 4 additions & 1 deletion sitemesh3/src/main/webapp/WEB-INF/sitemesh3.xml
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
<sitemesh>
<mapping path="/*" decorator="decorator.html"/>
</sitemesh>
<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"/>
</sitemesh>
2 changes: 1 addition & 1 deletion sitemesh3/src/main/webapp/index.html
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html>
<head>
<META HTTP-EQUIV="Refresh" CONTENT="0;URL=example/HelloWorld.action">
<META HTTP-EQUIV="Refresh" CONTENT="0;URL=index.action">
</head>

<body>
Expand Down

0 comments on commit 57a6130

Please sign in to comment.