Struts 2 Hello World Example

SHARE & 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

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";
	}
}

3. Create Input JSP

Login.jsp


<%@ 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>

4. Configure struts.xml File

<?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>

5. Create Result JSP

Result.jsp

<%@ 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>

6. Configure web.xml File

<?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>

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

Comments

comments

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.

Speak Your Mind

*

Close
Please support the site
By clicking any of these buttons you help our site to get better