JavaBeat

  • Home
  • Java
    • Java 7
    • Java 8
    • Java EE
    • Servlets
  • Spring Framework
    • Spring Tutorials
    • Spring 4 Tutorials
    • Spring Boot
  • JSF Tutorials
  • Most Popular
    • Binary Search Tree Traversal
    • Spring Batch Tutorial
    • AngularJS + Spring MVC
    • Spring Data JPA Tutorial
    • Packaging and Deploying Node.js
  • About Us
    • Join Us (JBC)
  • Privacy

Struts 2 Hello World Example

December 13, 2013 by Krishna Srinivasan Leave a Comment

This tutorials explains how to create a simple struts 2 hello world example and deploy in your server. We are manually creating the required files and running the struts 2 application.

Struts 2 Components

  • Action: Create Action class to write your business logic and work as controller. Action classes are used for interaction from the user and send the response back. It is the cort part of the struts architecture.
  • Interceptors: Interceptors are part of action classes and it is special version of controller. One example is exception interceptors where it handles if any exception is thrown by your application. Interceptors are used only when it is necessary.
  • Views: Create views to take input from the users and display the results.
  • Configurations files: Write configuration files to load the action classes and mappings in your application. This includes web.xml, struts.xml, struts.properties.

1. Create The Directory Structure

  1. Create Java packages for writing Actions classes
  2. Create JSP or any other view pages under the web content folder
  3. Create web.xml under WEB-INF folder
  4. Create struts.xml file inside the Java classes. It should be maintained in the class path.

Struts 2 Project Structure

Get Struts 2 Libraries from here

2. Create Struts 2 Action

[code lang=”java”]
package javabeat.net.struts2;

<b>Struts2HelloWorldAction.java</b>

public class Struts2HelloWorldAction {
private String userName;

public String getUserName() {
return userName;
}

public void setUserName(String userName) {
this.userName = userName;
}
public String execute(){
System.out.println(this.userName);
return "success";
}
}

[/code]

3. Create Input JSP

Login.jsp

[code lang=”xml”]

<%@ page contentType="text/html; charset=UTF-8"%>
<%@ taglib prefix="s" uri="/struts-tags"%>
<html>
<head></head>
<body>
<h1>Struts 2 Hello World Example</h1>

<s:form action="Welcome">
<s:textfield name="userName" label="Username" />
<s:password name="password" label="Password" />
<s:submit />
</s:form>

</body>
</html>
[/code]

4. Configure struts.xml File

[code lang=”xml”]
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts
Configuration 2.1//EN" "http://struts.apache.org/dtds/struts-2.1.dtd">
<struts>
<constant name="struts.devMode" value="true" />
<package name="hello" extends="struts-default">
<action name="Welcome" class="javabeat.net.struts2.Struts2HelloWorldAction" >
<result name="success">Result.jsp</result>
</action>
</package>
</struts>
[/code]

5. Create Result JSP

Result.jsp

[code lang=”xml”]
<%@ page contentType="text/html; charset=UTF-8"%>
<%@ taglib prefix="s" uri="/struts-tags"%>
<html>
<head></head>
<body>
<h1>Struts 2 Hello World Example</h1>
<h4>
Hello <s:property value="userName" />
</h4>

</body>
</html>
[/code]

6. Configure web.xml File

[code lang=”xml”]
<?xml version="1.0" encoding="UTF-8"?>
<web-app>
<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>

[/code]

6. Run the application

If you access the application http://localhost:8080/Struts2App/Login.jsp. You would see the following output in your screen.

Struts 2 Hello World Example Input Fields

Struts 2 Hello World Example Result

Download Source Code

Filed Under: Struts Tagged With: Struts 2 Tutorials

About Krishna Srinivasan

He is Founder and Chief Editor of JavaBeat. He has more than 8+ years of experience on developing Web applications. He writes about Spring, DOJO, JSF, Hibernate and many other emerging technologies in this blog.

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Follow Us

  • Facebook
  • Pinterest

As a participant in the Amazon Services LLC Associates Program, this site may earn from qualifying purchases. We may also earn commissions on purchases from other retail websites.

JavaBeat

FEATURED TUTORIALS

Answered: Using Java to Convert Int to String

What is new in Java 6.0 Collections API?

The Java 6.0 Compiler API

Copyright © by JavaBeat · All rights reserved