Create Maven Project
On the Eclipse, create a Maven project
Click Next button and select Workspace location for project
Click Next button and select maven-archetype-webapp
Click Next button and enter Project Information:
- GroupId: LearnStrutsFrameworkWithRealApps
- Artifact Id: LearnStrutsFrameworkWithRealApps
- Package: com.demo
Click Finish button to finish create Maven project
Add Tomcat Server
Select current project, Right click and select Properties menu. In Properties dialog, select Targeted Runtime in left side after select Tomcat server from server list in right side
Click Ok button to finish
Add JRE System Library
Select current project, Right click and select Build Path\Configure Build Path menu Select JRE System Library in Libraries tab:
Click Edit button and select JRE System Library you need use as below:
Click Finish button to finish
Configure pom.xml
Open pom.xml file and add configurations for Struts 2 as below:
<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/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>LearnStrutsFrameworkWithRealApps</groupId>
<artifactId>LearnStrutsFrameworkWithRealApps</artifactId>
<packaging>war</packaging>
<version>0.0.1-SNAPSHOT</version>
<name>LearnStrutsFrameworkWithRealApps Maven Webapp</name>
<url>http://maven.apache.org</url>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>javax.servlet.jsp.jstl</groupId>
<artifactId>javax.servlet.jsp.jstl-api</artifactId>
<version>1.2.1</version>
</dependency>
<dependency>
<groupId>taglibs</groupId>
<artifactId>standard</artifactId>
<version>1.1.2</version>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>3.1.0</version>
<scope>provided</scope>
</dependency>
<!-- Struts 2 Framework -->
<dependency>
<groupId>org.apache.struts</groupId>
<artifactId>struts2-core</artifactId>
<version>2.5.20</version>
</dependency>
<dependency>
<groupId>org.apache.struts</groupId>
<artifactId>struts2-convention-plugin</artifactId>
<version>2.5.20</version>
</dependency>
<!-- Tiles -->
<dependency>
<groupId>org.apache.struts</groupId>
<artifactId>struts2-tiles-plugin</artifactId>
<version>2.5.20</version>
</dependency>
<dependency>
<groupId>org.apache.tiles</groupId>
<artifactId>tiles-extras</artifactId>
<version>3.0.8</version>
</dependency>
</dependencies>
<build>
<finalName>LearnStrutsFrameworkWithRealApps</finalName>
</build>
</project>
Configure web.xml
Open web.xml file in src\main\webapp\WEB-INF folder and add configurations for Struts 2 as below:
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee
http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
version="3.1">
<display-name>Learn Struts 2 Framework with Real Apps</display-name>
<context-param>
<param-name>tilesDefinitions</param-name>
<param-value>
/WEB-INF/tiles.xml,
/WEB-INF/tiles_news.xml,
/WEB-INF/tiles_home.xml,
/WEB-INF/tiles_about_us.xml
</param-value>
</context-param>
<filter>
<filter-name>struts2</filter-name>
<filter-class>org.apache.struts2.dispatcher.filter.StrutsPrepareAndExecuteFilter</filter-class>
<init-param>
<param-name>struts.devMode</param-name>
<param-value>true</param-value>
</init-param>
<init-param>
<param-name>struts.action.extension</param-name>
<param-value>html</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>/*</url-pattern>
<dispatcher>REQUEST</dispatcher>
<dispatcher>FORWARD</dispatcher>
<dispatcher>INCLUDE</dispatcher>
</filter-mapping>
<listener>
<listener-class>org.apache.tiles.extras.complete.CompleteAutoloadTilesListener</listener-class>
</listener>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
</web-app>
Create Configuration File
Create new xml file named struts.xml in src/main/resources folder as below:
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
"http://struts.apache.org/dtds/struts-2.0.dtd">
<struts>
<constant name="struts.convention.default.parent.package" value="controllers"/>
<package name="controllers" extends="struts-default">
<result-types>
<result-type name="tiles" class="org.apache.struts2.views.tiles.TilesResult" />
</result-types>
</package>
</struts>
Create Actions
Create new package named controllers.action in src/main/java folder. The action class in Struts 2 must be put in a package named action. In this package, create new actions as below:
Home Action
In controllers.action package, create new java class named HomeAction.java as below:
package controllers.action;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.Result;
import com.opensymphony.xwork2.ActionSupport;
@Namespace("/home")
public class HomeAction extends ActionSupport {
private static final long serialVersionUID = 1L;
@Action(value = "index", results = {
@Result(name = SUCCESS, location = "home.index", type = "tiles")
})
public String index() {
return SUCCESS;
}
}
About Us Action
In controllers.action package, create new java class named AboutUsAction.java as below:
package controllers.action;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.Result;
import com.opensymphony.xwork2.ActionSupport;
@Namespace("/aboutus")
public class AboutUsAction extends ActionSupport {
private static final long serialVersionUID = 1L;
@Action(value = "index", results = {
@Result(name = SUCCESS, location = "aboutus.index", type = "tiles")
})
public String index() {
return SUCCESS;
}
@Action(value = "about1", results = {
@Result(name = SUCCESS, location = "aboutus.about1", type = "tiles")
})
public String about1() {
return SUCCESS;
}
@Action(value = "about2", results = {
@Result(name = SUCCESS, location = "aboutus.about2", type = "tiles")
})
public String about2() {
return SUCCESS;
}
}
News Action
In controllers.action package, create new java class named NewsAction.java as below:
package controllers.action;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.Result;
import com.opensymphony.xwork2.ActionSupport;
@Namespace("/news")
public class NewsAction extends ActionSupport {
private static final long serialVersionUID = 1L;
@Action(value = "index", results = {
@Result(name = SUCCESS, location = "news.index", type = "tiles")
})
public String index() {
return SUCCESS;
}
@Action(value = "news1", results = {
@Result(name = SUCCESS, location = "news.news1", type = "tiles")
})
public String news1() {
return SUCCESS;
}
@Action(value = "news2", results = {
@Result(name = SUCCESS, location = "news.news2", type = "tiles")
})
public String news2() {
return SUCCESS;
}
@Action(value = "news3", results = {
@Result(name = SUCCESS, location = "news.news3", type = "tiles")
})
public String news3() {
return SUCCESS;
}
}
Create Views
Create new folder named views in src\main\webapp\WEB-INF folder. In views folder, create new views as below:
Master Template View
Create new folder named templates. Create new jsp file named masterTemplate.jsp as below:
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1" isELIgnored="false" %>
<%@ taglib prefix="s" uri="/struts-tags" %>
<%@ taglib prefix="tiles" uri="http://tiles.apache.org/tags-tiles" %>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Master Page Template</title>
</head>
<body>
<s:a namespace="/home" action="index">Home</s:a> |
<s:a namespace="/aboutus" action="index">About Us</s:a> |
<s:a namespace="/news" action="index">News</s:a>
<br><br>
<tiles:insertAttribute name="content"></tiles:insertAttribute>
<br><br>
Copyright PMK Lab
</body>
</html>
About Us Template View
In templates folder, create new jsp file named aboutUsTemplate.jsp as below:
<%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1" isELIgnored="false"%>
<%@ taglib prefix="tiles" uri="http://tiles.apache.org/tags-tiles" %>
<%@ taglib prefix="s" uri="/struts-tags" %>
<br>
<s:a namespace="/aboutus" action="about1">About 1</s:a> |
<s:a namespace="/aboutus" action="about2">About 2</s:a>
<br><br>
<tiles:insertAttribute name="aboutUsContent"></tiles:insertAttribute>
News Template View
In templates folder, create new jsp file named newsTemplate.jsp as below:
<%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1" isELIgnored="false"%>
<%@ taglib prefix="s" uri="/struts-tags" %>
<%@ taglib prefix="tiles" uri="http://tiles.apache.org/tags-tiles" %>
<br>
<s:a namespace="/news" action="news1">News 1</s:a> |
<s:a namespace="/news" action="news2">News 2</s:a> |
<s:a namespace="/news" action="news3">News 3</s:a>
<br><br>
<tiles:insertAttribute name="newsContent"></tiles:insertAttribute>
Home View
Create new folder named home. Create new jsp file named index.jsp as below:
<%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%>
<h3>Home Page</h3>
About Us Views
Create new folder named aboutus. Create new jsp files as below:
Index View
In aboutus folder, create new jsp file named index.jsp as below:
<%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%>
<h3>About Us Page</h3>
About 1 View
In aboutus folder, create new jsp file named about1.jsp
<%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%>
<h3>About 1 Page</h3>
About 2 View
In aboutus folder, create new jsp file named about2.jsp
<%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%>
<h3>About 2 Page</h3>
News Views
Create new folder named news. Create new jsp files as below:
Index View
In news folder, create new jsp file named index.jsp as below:
<%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%>
<h3>News Page</h3>
News 1 View
In news folder, create new jsp file named news1.jsp
<%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%>
<h3>News 1 Page</h3>
News 2 View
In news folder, create new jsp file named news2.jsp
<%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%>
<h3>News 2 Page</h3>
News 3 View
In news folder, create new jsp file named news3.jsp
<%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%>
<h3>News 3 Page</h3>
Tile Definitions
In src\main\webapp\WEB-INF folder, create new xml files as below:
Tile Definitions for Master Page Template
In src\main\webapp\WEB-INF folder, create new xml file named tiles.xml as below:
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE tiles-definitions PUBLIC
"-//Apache Software Foundation//DTD Tiles Configuration 2.0//EN"
"http://tiles.apache.org/dtds/tiles-config_2_0.dtd">
<tiles-definitions>
<definition name="masterTemplate" template="/WEB-INF/views/templates/masterTemplate.jsp">
<put-attribute name="content" value=""></put-attribute>
</definition>
</tiles-definitions>
Tiles Definitions for Home Action
In src\main\webapp\WEB-INF folder, create new xml file named tiles_home.xml as below:
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE tiles-definitions PUBLIC
"-//Apache Software Foundation//DTD Tiles Configuration 2.0//EN"
"http://tiles.apache.org/dtds/tiles-config_2_0.dtd">
<tiles-definitions>
<definition name="home.index" extends="masterTemplate">
<put-attribute name="content" value="/WEB-INF/views/home/index.jsp"></put-attribute>
</definition>
</tiles-definitions>
Tile Definitions for About Us Action
In src\main\webapp\WEB-INF folder, create new xml file named tiles_about_us.xml as below:
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE tiles-definitions PUBLIC
"-//Apache Software Foundation//DTD Tiles Configuration 2.0//EN"
"http://tiles.apache.org/dtds/tiles-config_2_0.dtd">
<tiles-definitions>
<definition name="aboutus.index" extends="masterTemplate">
<put-attribute name="content">
<definition name="aboutUsTemplate" template="/WEB-INF/views/templates/aboutUsTemplate.jsp">
<put-attribute name="aboutUsContent" value="/WEB-INF/views/aboutus/index.jsp"></put-attribute>
</definition>
</put-attribute>
</definition>
<definition name="aboutus.about1" extends="masterTemplate">
<put-attribute name="content">
<definition extends="aboutUsTemplate">
<put-attribute name="aboutUsContent" value="/WEB-INF/views/aboutus/about1.jsp"></put-attribute>
</definition>
</put-attribute>
</definition>
<definition name="aboutus.about2" extends="masterTemplate">
<put-attribute name="content">
<definition extends="aboutUsTemplate">
<put-attribute name="aboutUsContent" value="/WEB-INF/views/aboutus/about2.jsp"></put-attribute>
</definition>
</put-attribute>
</definition>
</tiles-definitions>
Tiles Definitions for News Action
In src\main\webapp\WEB-INF folder, create new xml file named tiles_news.xml as below:
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE tiles-definitions PUBLIC
"-//Apache Software Foundation//DTD Tiles Configuration 2.0//EN"
"http://tiles.apache.org/dtds/tiles-config_2_0.dtd">
<tiles-definitions>
<definition name="news.index" extends="masterTemplate">
<put-attribute name="content">
<definition name="newsTemplate" template="/WEB-INF/views/templates/newsTemplate.jsp">
<put-attribute name="newsContent" value="/WEB-INF/views/news/index.jsp"></put-attribute>
</definition>
</put-attribute>
</definition>
<definition name="news.news1" extends="masterTemplate">
<put-attribute name="content">
<definition extends="newsTemplate">
<put-attribute name="newsContent" value="/WEB-INF/views/news/news1.jsp"></put-attribute>
</definition>
</put-attribute>
</definition>
<definition name="news.news2" extends="masterTemplate">
<put-attribute name="content">
<definition extends="newsTemplate">
<put-attribute name="newsContent" value="/WEB-INF/views/news/news2.jsp"></put-attribute>
</definition>
</put-attribute>
</definition>
<definition name="news.news3" extends="masterTemplate">
<put-attribute name="content">
<definition extends="newsTemplate">
<put-attribute name="newsContent" value="/WEB-INF/views/news/news3.jsp"></put-attribute>
</definition>
</put-attribute>
</definition>
</tiles-definitions>
Create Redirect Page
Create new jsp file named index.jsp in src\main\webapp folder as below:
<%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%>
<jsp:forward page="home/index.html"></jsp:forward>
Structure of Project
Run Application
Select project, right click and select Run on Server menu
Access index method in home action with following url:
http://localhost:8080/LearnStrutsFrameworkWithRealApps/home/index.html
Output
Click About Us link from menu
Output
Click About 1 link from menu
Output
Click About 2 link from menu
Output
Click News link from menu
Output
Click News 1 link from menu
Output
Click News 2 link from menu
Output
Click News 3 link from menu
Output