DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports Events Over 2 million developers have joined DZone. Join Today! Thanks for visiting DZone today,
Edit Profile Manage Email Subscriptions Moderation Admin Console How to Post to DZone Article Submission Guidelines
View Profile
Sign Out
Refcards
Trend Reports
Events
Zones
Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones AWS Cloud
by AWS Developer Relations
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones
AWS Cloud
by AWS Developer Relations
11 Monitoring and Observability Tools for 2023
Learn more
  1. DZone
  2. Coding
  3. Languages
  4. Struts 2 Annotation Tutorial 2

Struts 2 Annotation Tutorial 2

Meyyappan Muthuraman user avatar by
Meyyappan Muthuraman
·
Jun. 14, 12 · Tutorial
Like (0)
Save
Tweet
Share
64.35K Views

Join the DZone community and get the full member experience.

Join For Free

This example is the continuation of the previous annotation example. If you are new to Struts 2 annotations then go through that example first ( Struts 2 Annotations - Part 1 ). Here we will see the same hello user example with the following changes.

  • Our Action class ends with the world Action and does not implement com.opensymphony.xwork2.Action.
  • We use /results directory for storing our result pages instead of WEB-INF/content.

The directory structure of the hello user example is shown below.

Our WelcomeUserAction class is a simple pojo class. The important thing to note here is that our Action classs name ends with the word Action.

import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Result;

public class WelcomeUserAction {
	private String userName;
	private String message;


	@Action(value="/welcome",results={ @Result(name="success",location="/results/successPage.jsp")})
	public String execute() {
		message = "Welcome " + userName + " !";
		return "success";
	}

	public void setUserName(String userName) {
		this.userName = userName;
	}

	public void setMessage(String message) {
		this.message = message;
	}

	public String getUserName() {
		return userName;
	}

	public String getMessage() {
		return message;
	}
}

Here we use Action and Result annotations just to show you how to use them, for simple example like this you can use the intelligent defaults provided by the Convention plug-in.

The Convention plug-in uses the Action class name to map the action URL. The Convention plug-in first removes the world Action at the end of the class name and then converts the camel case name to dashes. So by default our WelcomeUserAction will be invoked for the request URL welcome-user. But if you want the Action to be invoked for a different URL then you can do this by using the Action annotation.

The value of the Action annotation is "/welcome", this means that the action will be invoked for the request URL "/welcome". You can change the default action and URL mapping using the Action annotation.

Now based on the result code from action the Convention plug-in will look for the result name welcome-resultcode in the directory WEB-INF/content. You can change this to a different location by setting the property struts.convention.result.path to a new value in the Struts properties file. In this example we store the result pages in /results directory.

struts.properties file
-----------------------
struts.convention.result.path=/results

Our result page name is successPage.jsp, the Convention plug-in will look for a page like welcome.jsp ( the file can even be a freemaker or velocity file ) since our URL is "/welcome". In this case it will give an error, if we are not specifying which result it should invoke when the result is "success". To do this we use the Result annotation.

The Result annotation maps the result code with the result page. Here the result code "success" is mapped to the result "/results/successPage.jsp".

The annotations needs to be specified only when you are not using the default naming conventions, if you use them you can keep writing action classes and result pages without any configuration and the framework know exactly when to invoke them.

You can download the Struts 2 annotation example by clicking the download link below.

Source :Download
 
Source + Lib :Download
Annotation

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • How To Handle Secrets in Docker
  • Building the Next-Generation Data Lakehouse: 10X Performance
  • The Power of Zero-Knowledge Proofs: Exploring the New ConsenSys zkEVM
  • Authenticate With OpenID Connect and Apache APISIX

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com
  • +1 (919) 678-0300

Let's be friends: