Now create 4 new JSP files header.jsp, menu.jsp , footer.jsp and welcome.jsp and copy following content in each of them.
header.jsp

<div style="padding:16px;background-color:blue">
<h3>This is the Page Header</h3>
</div>

footer.jsp
<div style="padding:16px;background-color:red">
<h3>This is the Page Footer</h3>

menu.jsp
<%@ page contentType="text/html; charset=UTF-8"%>
<%@ taglib prefix="s" uri="/struts-tags"%>
<s:url id="testUrlId" namespace="/" action="logout">
</s:url>
<s:a errorText="Sorry your request had an error."
    href="%{testUrlId}">
    Logout
</s:a>
</div>

welcome.jsp
<%@ page contentType="text/html; charset=UTF-8"%>
<%@ taglib prefix="s" uri="/struts-tags"%>
<html>
 
<head>
<title>Struts 2 - Tiles example Application</title>
</head>
<body>
    <h2>Welcome, <s:property value="username" />!</h2>
       
    Lorem ipsum dolor sit amet, consectetur adipisici elit, sed eiusmod tempor 
    incidunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, 
    quis nostrud exercitation ullamco laboris nisi ut aliquid ex ea commodi 
    consequat. 
    Quis aute iure reprehenderit in voluptate velit esse cillum dolore eu 
    fugiat nulla pariatur. Excepteur sint obcaecat cupiditat non proident,
    sunt in culpa qui officia deserunt mollit anim id est laborum.
</body>
</html>

As you can see the welcome page dumps a property username which is gathered from the following index.jsp page:
index.jsp
<%@ page contentType="text/html; charset=UTF-8"%>
<%@ taglib prefix="s" uri="/struts-tags"%>
<html>
<head>
<title>Struts 2 - Login Application</title>
</head>
<body>
<h2>Struts 2 - Login Application</h2>
 
<s:form action="login.action" method="post">
    <s:textfield name="username" label="Username" size="20" />
    <s:password name="password" label="Password" size="20" />
    <s:submit method="execute" label="Login" align="center" />
</s:form>
</body>
</html>

And here's the corresponding Struts2 Login action so we will test both Struts2 actions and Tiles:
package struts2.example;

import com.opensymphony.xwork2.ActionSupport;

public class LoginAction extends ActionSupport{
    private String username;
    private String password;
 
    public String authenticate() {
 
        if (this.username.equals("admin")
                && this.password.equals("admin")) {
            return "success";
        } else {
            addActionError(getText("error.login"));
            return "error";
        }
    }
 
    public String logout() {
            return "logout";
    }
 
    public String getUsername() {
        return username;
    }
 
    public void setUsername(String username) {
        this.username = username;
    }
 
    public String getPassword() {
        return password;
    }
 
    public void setPassword(String password) {
        this.password = password;
    }
}

And finally the struts.xml file which needs to be placed into the src folder (src/main/resources for Maven projects) of your project so that will be moved after compilation into WEB_INF/classes
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
    "-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
    "http://struts.apache.org/dtds/struts-2.3.dtd">
<struts>
  <constant name="struts.enable.DynamicMethodInvocation" value="false"/>
  <constant name="struts.devMode" value="true"/>
  
    <package name="default" extends="struts-default" namespace="/">
        <result-types>
            <result-type name="tiles"
                class="org.apache.struts2.views.tiles.TilesResult" />
        </result-types>
 
        <action name="login" method="authenticate"
            class="struts2.example.LoginAction">
            <result name="success" type="tiles">/welcome.tiles</result>
            <result name="error" type="tiles">/index.jsp</result>
        </action>
 
        <action name="logout" method="logout" 
            class="struts2.example.LoginAction">
            <result name="logout">/index.jsp</result>
        </action>
    </package>
</struts>

In order to compile and run I strongly suggest using Maven. Here's pom.xml file:
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>struts2example</groupId>
    <artifactId>struts2example</artifactId>
    <version>1.0-SNAPSHOT</version>
    <packaging>war</packaging>
    <name>struts2example</name>

    <properties>
        <struts2.version>2.3.8</struts2.version>
    </properties>

    <dependencies>

        <dependency>
            <groupId>org.apache.struts</groupId>
            <artifactId>struts2-core</artifactId>
            <version>${struts2.version}</version>
        </dependency>

        <dependency>
            <groupId>org.apache.struts</groupId>
            <artifactId>struts2-config-browser-plugin</artifactId>
            <version>${struts2.version}</version>
        </dependency>

        <dependency>
            <groupId>org.apache.struts</groupId>
            <artifactId>struts2-junit-plugin</artifactId>
            <version>${struts2.version}</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.apache.struts</groupId>
            <artifactId>struts-tiles</artifactId>
            <version>1.3.10</version>
        </dependency>
        <dependency>
            <groupId>org.apache.struts</groupId>
            <artifactId>struts2-tiles-plugin</artifactId>
            <version>2.3.8</version>
            <scope>compile</scope>
        </dependency>
        <dependency>
            <groupId>commons-logging</groupId>
            <artifactId>commons-logging</artifactId>
            <version>1.1.1</version>
        </dependency>

        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>servlet-api</artifactId>
            <version>2.4</version>
            <scope>provided</scope>
        </dependency>

        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>jsp-api</artifactId>
            <version>2.0</version>
            <scope>provided</scope>
        </dependency>

    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.mortbay.jetty</groupId>
                <artifactId>maven-jetty-plugin</artifactId>
                <version>6.1.21</version>
                <configuration>
                    <scanIntervalSeconds>10</scanIntervalSeconds>
                    <scanTargets>
                        <scanTarget>src/main/webapp/WEB-INF</scanTarget>
                        <scanTarget>src/main/webapp/WEB-INF/web.xml</scanTarget>
                        <scanTarget>src/main/resources/struts.xml</scanTarget>

                    </scanTargets>
                </configuration>
            </plugin>
        </plugins>
    </build>
</project>

If you are enough masochist :-) then here are all the library required, in case you want to add them manually:

 

struts2 tutorial tiles jboss

And here's your Struts2 - Tiles application in action on JBoss AS 7.1.1

index.jsp

struts2 tutorial tiles jboss
And here's welcome.jsp which contains the Apache tiles layout:

struts2 tutorial tiles jboss

0
0
0
s2smodern