In this program, we will see how to create a "Hello World!" Example in Structs 2 in Eclipse IDE,
1. Right Click under Project Explorer and select New -> Dynamic Web Project
2. Project Name: HelloWorld, make sure you select the target runtime as the installed Apache Tomcat container version that you have installed on your system.
3. Make sure you add the following jar files in WebContent -> WEB-INF -> lib folder:
commons-fileupload-1.3.1.jar
commons-io-2.2.jar
commons-lang-2.4.jar
commons-lang3-3.2.jar
commons-logging-api-1.1.jar
freemarker-2.3.19.jar
javassist-3.11.0.GA.jar
ognl-3.0.6.jar
struts2-core-2.3.20.1.jar
xwork-core-2.3.20.1.jar
4. Create web.xml under WebContent -> WEB-INF folder,
File: web.xml<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
id="WebApp_ID" version="2.5">
<display-name>HelloWorld</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.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
</web-app>
5. Create a package under Java Resource -> src folder, name it com.code2care.struct2
6. Create a java class file under com.code2care.struts2 called HelloWorldAction.java (this is our action class)
File: HelloWorldAction.javapackage com.code2care.struts2;
public class HelloWorldAction {
private String name;
public String execute() throws Exception {
return "SUCCESS";
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
7. Create index.jsp under WEB-INF folder,
File: index.jsp<%@ 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">
<html>
<head>
<title>Struts2 Hello World!</title>
</head>
<body>
<h1>Struts2 : Hello world example in Eclipse </h1>
<form action="helloform">
<label for="name">Enter your name : </label>
<input type="text" name="name"/><br/>
</form>
</body>
</html>
8. Create greetingsPage.jsp folder under WEB-INF folder,
File: greetingsPage.jsp<%@ page contentType="text/html; charset=UTF-8" %>
<%@ taglib prefix="s" uri="/struts-tags" %>
<html>
<head>
<title>Struts 2 Hello world!</title>
</head>
<body>
Hello <s:property value="name"/> !!
</body>
</html>
9. Create classes folder under WebContent -> WEB-INF folder
10. Create struts.xml file under classes folder,
File: struts.xml<?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">
<struts>
<constant name="struts.devMode" value="true" />
<package name="HelloWorld" extends="struts-default">
<action name="helloform" class="com.code2care.struts2.HelloWorldAction"
method="execute">
<result name="SUCCESS">/greetingsPage.jsp</result>
</action>
</package>
</struts>
- How to get Java Thread name Example [Program]
- Java 8: Get First and Last Date of the Week for Given Date
- Convert String to int in Java
- How to Get List of All Country Codes in Java Using Locale Class
- Convert Multidimensional Array toString In Java
- [Fix] java: incompatible types: incompatible parameter types in lambda expression error
- Display Era details (AD BC) in Java Date using SimpleDateFormat
- Create a Zip file using Java Code programmatically
- [Fix] java.net.MalformedURLException: unknown protocol
- [fix] Java JDBC ConnectException: Connection refused
- Read Json File and Convert to Java Object using Jackson
- list of jars required for hibernate 4.x.x
- Simple Struts 2 Tutorial in eclipse with tomcat 7 server
- Java: The value of the local variable string is not used
- [fix] URI is not registered (Settings | Languages & Frameworks | Schemas and DTDs) IntelliJ
- Java 8+ get Day of the Week Examples with LocalDateTime, DateTime, ZonalDateTime and Instant classes
- Run SQL Script file using Java JDBC Code Example
- Remove Trailing zeros BigDecimal Java
- Java JDBC IN Clause Example with PreparedStatement MySQL
- Convert Java List to Json String using Jackson
- Java 8 foreach loop code examples
- error: file not found: HelloWorld.java
- IntelliJ Keyboard Shortcut to remove unused imports [Java]
- Exception in thread main java.lang.NoClassDefFoundError: package javaClass
- Struts 2 Hello World Example in Eclipse
- SQL: Check if table exists - HowTos
- [fix] Docker - no matching manifest for linux/arm64/v8 in the manifest list entries [M/M2 Mac] - Docker
- How to identify the version of IntelliJ - HowTos
- How to remove or unstage a file from git staged area - Git
- How to Schedule Mails in macOS Ventura - MacOS
- How to check file permissions for your file using Linux/Unix/macOS Terminal Command - Linux
- Display full website URL/address in Safari macOS Browser - MacOS
- How to install homebrew (brew) on M1 Mac - MacOS