Struts 2 Hello World With Eclipse, Maven and Jetty

I just adapted mkyong’s Struts-2 tutorial [1] to work with embedded Jetty in Eclipse.

How Project Explorer looks like:

struts2_2

– Create a new project in Eclipse via File > New > Dynamic Web Project
– Add “src/main/java” and “src/main/resources” to classpath and change output folder as “target” and click finish
– Right-click the project name in Project Explorer, Configure > Convert to Maven Project

struts2_1

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<%@ taglib prefix="s" uri="/struts-tags"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd&quot;>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Index</title>
</head>
<body>
<h1>Struts 2 - Index</h1>
<s:form action="Welcome">
<s:textfield name="name" label="name" />
<s:submit />
</s:form>
</body>
</html>
view raw index.jsp hosted with ❤ by GitHub
<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>Struts2_HelloWorld</groupId>
<artifactId>Struts2_HelloWorld</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>war</packaging>
<dependencies>
<dependency>
<groupId>org.apache.struts</groupId>
<artifactId>struts2-core</artifactId>
<version>2.3.1.2</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.3</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
<plugin>
<artifactId>maven-war-plugin</artifactId>
<version>2.6</version>
<configuration>
<warSourceDirectory>webapp</warSourceDirectory>
<failOnMissingWebXml>false</failOnMissingWebXml>
</configuration>
</plugin>
<plugin>
<groupId>org.eclipse.jetty</groupId>
<artifactId>jetty-maven-plugin</artifactId>
<version>9.2.11.v20150529</version>
<configuration>
<httpConnector>
<port>9091</port>
</httpConnector>
</configuration>
</plugin>
</plugins>
</build>
</project>
view raw pom.xml hosted with ❤ by GitHub
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
"http://struts.apache.org/dtds/struts-2.0.dtd"&gt;
<struts>
<package name="com.afsinka" namespace="/Struts2-Test" extends="struts-default">
<action name="Index">
<result>/index.jsp</result>
</action>
<action name="Welcome" class="com.afsinka.WelcomeAction">
<result name="true">/welcome_user.jsp</result>
</action>
</package>
</struts>
view raw struts.xml hosted with ❤ by GitHub
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
id="WebApp_ID" version="3.1">
<display-name>Struts_HelloWorld</display-name>
<filter>
<filter-name>struts2</filter-name>
<filter-class>
org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter
</filter-class>
</filter>
<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
</web-app>
view raw web.xml hosted with ❤ by GitHub
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<%@ taglib prefix="s" uri="/struts-tags"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd&quot;>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Welcome User</title>
</head>
<body>
<h1>Struts 2 - Welcome User</h1>
<h2>
Hello
<s:property value="name" />
!
</h2>
</body>
</html>
view raw welcome_user.jsp hosted with ❤ by GitHub
package com.afsinka;
public class WelcomeAction {
private String name;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String execute() {
System.out.println("Executed for: " + name);
return Boolean.TRUE.toString();
}
}
view raw WelcomeAction.java hosted with ❤ by GitHub

To run with Maven:

clean install jetty:run

To test:

http://localhost:9091/Struts2-Test/Index.action

struts2_3struts2_4

Resources:

[1] https://www.mkyong.com/struts2/struts-2-hello-world-example/

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: