Back to: Java Struts Tutorials
Struts 2 Annotations with Examples
In this article, I am going to discuss Struts 2 Annotations with Examples. Please read our previous article where we discussed Struts 2 Exception Handling.
What are Annotations in Struts 2?
In Java, annotations are a form of metadata that can be added to code elements such as classes, methods, fields, and parameters. Annotations provide additional information about the code, which can be processed at compile-time, runtime, or during build processes by tools and frameworks. They typically start with the “@” symbol, followed by the annotation name and optional parameters. Annotations serve various purposes, including:
- Information for the Compiler: Annotations can give instructions or additional information to the Java compiler to generate code or perform specific tasks during compilation.
- Configuration and Customization: They can be used to configure and customize the behavior of frameworks, libraries, or third-party tools.
- Documentation: Annotations can be used to generate documentation or other auxiliary files to aid developers in understanding the code.
- Runtime Processing: Some annotations are processed at runtime to modify the behavior of code dynamically or to provide runtime information.
- Code Analysis and Validation: Tools like static code analyzers can use annotations to perform code validation and enforce coding standards.
Struts 2 provides some of its own annotations. By using these annotations, the struts.xml file can be bypassed altogether, as the configuration will be done in the Java code itself.
How to Implement Annotations in Struts 2?
Step 1: Create a new maven project in Eclipse. We will use Maven to manage the dependencies of struts2. This way, we do not have to import JARs.
Remember to set the packaging to war. This is because we shall be deploying a web application.
Finally, click on the finish button to create the project. The following project directories need to be created:
Step 2: Modify pom.xml to add the required dependencies and the build configuration:
<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 https://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.dotnet</groupId> <artifactId>strutex</artifactId> <version>0.0.1-SNAPSHOT</version> <packaging>war</packaging> <dependencies> <dependency> <groupId>org.apache.struts</groupId> <artifactId>struts2-core</artifactId> <version>6.1.2.1</version> </dependency> <dependency> <groupId>org.apache.struts</groupId> <artifactId>struts-taglib</artifactId> <version>1.3.10</version> </dependency> <dependency> <groupId>javax.servlet</groupId> <artifactId>servlet-api</artifactId> <version>2.3</version> </dependency> <dependency> <groupId>taglibs</groupId> <artifactId>standard</artifactId> <version>1.1.2</version> </dependency> <dependency> <groupId>javax.servlet.jsp.jstl</groupId> <artifactId>jstl-api</artifactId> <version>1.2</version> </dependency> <dependency> <groupId>javax.servlet.jsp</groupId> <artifactId>javax.servlet.jsp-api</artifactId> <version>2.3.3</version> <scope>provided</scope> </dependency> <dependency> <groupId>org.apache.struts</groupId> <artifactId>struts2-convention-plugin</artifactId> <version>6.2.0</version> </dependency> <dependency> <groupId>org.ow2.asm</groupId> <artifactId>asm</artifactId> <version>9.5</version> </dependency> <dependency> <groupId>org.antlr</groupId> <artifactId>antlr4</artifactId> <version>4.13.0</version> </dependency> <dependency> <groupId>commons-fileupload</groupId> <artifactId>commons-fileupload</artifactId> <version>1.5</version> </dependency> <dependency> <groupId>commons-io</groupId> <artifactId>commons-io</artifactId> <version>2.13.0</version> </dependency> <dependency> <groupId>org.apache.commons</groupId> <artifactId>commons-lang3</artifactId> <version>3.12.0</version> </dependency> <dependency> <groupId>org.freemarker</groupId> <artifactId>freemarker</artifactId> <version>2.3.32</version> </dependency> <dependency> <groupId>org.javassist</groupId> <artifactId>javassist</artifactId> <version>3.29.2-GA</version> </dependency> <dependency> <groupId>ognl</groupId> <artifactId>ognl</artifactId> <version>3.4.0</version> </dependency> <dependency> <groupId>org.apache.struts.xwork</groupId> <artifactId>xwork-core</artifactId> <version>2.3.37</version> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <version>3.11.0</version> <configuration> <source>17</source> <target>17</target> </configuration> </plugin> </plugins> </build> </project>
Step 3: In the src/main/webapp directory, create a new file called index.jsp. This file asks the user for the name and age belonging to a student. Add the following content to the file:
<%@ 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>Student Form</title> </head> <body> <s:form action = "studentinfo" method = "post"> <s:textfield name = "name" label = "Name: " size = "20" /> <s:textfield name = "age" label = "Age: " size = "20" /> <s:submit name = "submit" label = "Submit" align="center" /> </s:form> </body> </html>
Step 4: In the src/main/webapp directory, create a new file called success.jsp. This file displays a success message. Add the following content to the file:
<%@ 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>Success</title> </head> <body> Student information was saved successfully. </body> </html>
Step 5: In the src/main/java directory, create a new file called StudentAction.java. The file contains the Java action code. Add the following content to the file:
package com.dotnet; import org.apache.struts2.convention.annotation.Action; import org.apache.struts2.convention.annotation.Result; import org.apache.struts2.convention.annotation.Results; import com.opensymphony.xwork2.ActionSupport; import com.opensymphony.xwork2.validator.annotations.RequiredFieldValidator; @Results({ @Result(name = "success", location = "/success.jsp"), @Result(name = "input", location = "/index.jsp") }) public class StudentAction extends ActionSupport { private String name; private int age; @Action(value = "/studentinfo") public String execute() { return SUCCESS; } @RequiredFieldValidator(message = "Name is required!") public String getName() { return name; } public void setName(String name) { this.name = name; } @RequiredFieldValidator(message = "Age must be within 3 and 25!") public int getAge() { return age; } public void setAge(int age) { this.age = age; } }
The following annotations are used:
- @Results and @Result: These annotations are used to mark the result pages.
- @Action: This annotation is used to mark the execute() function of an action class.
- @RequiredFieldValidator: This annotation is used to validate the fields.
Step 6: In the src/main/webapp directory, create a new directory called WEB-INF. In this directory, create a new file called web.xml. This file is responsible for implementing struts2 in the web application. Add the following content to the file:
<?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" xsi:schemalocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/web-app_3_0.xsd" id="apache-struts-config-example" version="3.0"> <display-name>Struts Config XML</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.filter.StrutsPrepareAndExecuteFilter </filter-class> <init-param> <param-name>struts.devMode</param-name> <param-value>true</param-value> </init-param> </filter> <filter-mapping> <filter-name>struts2</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> </web-app>
Step 7: Compile and execute the program on the Tomcat server. The following page must open in your default web browser:
If you enter some wrong details, the following text will be shown:
Once you enter the proper details, the following webpage will be displayed:
Congratulations! You now know how to implement annotations in struts2!
In the next article, I am going to discuss Struts 2 Tag with Examples. Here, in this article, I try to explain Struts 2 Annotations with Examples and I hope you enjoy this Struts 2 Annotations article.