Struts - SPLessons

Struts 2 Annotations

Home > Lesson > Chapter 21
SPLessons 5 Steps, 3 Clicks
5 Steps - 3 Clicks

Struts 2 Annotations

Struts 2 Annotations

shape Description

Struts 2 Annotations are used to reduce the code of an application and the main advantage of an annotation is that no need to write struts.xml file, Annotation tags will be represented with “@”, The following are the tags which will be used in the code. Annotations will carry meta data that will also be used in the code, In Struts 2 application, one can configure the request to Action class in two types.

By using struts.xml

shape Description

Whenever the request coming to the Action class from browser, then one need to configure the action class in struts.xml file. struts.xml [xml] <struts> <package name="default"> <action name="form action name" class="Action class name"> <result name="success">success.jsp</result> <result name="error">error.jsp</result> </action> </package> </struts> [/xml]

By using Annotations

shape Description

By using the struts Annotations, one can achieve Zero Configuration. Use the Annotations in application and don't write the struts.xml file. The Struts 2 is supported with some default annotations.

shape Example

Struts 2 Example by using Annotations. 1. Create the project directory structure. 2.Add the all jar files in lib folder. 3. Create the view pages and forward the request to action class from View pages. index.jsp [java] <%@ 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> <font color="green"> SPLessons Register page.</font> </head> <body> <s:form action="login" > <s:textfield name="name" label="Name" /> <s:submit value="submit"/> </s:form> </body> </html> [/java] Here the developer just created the text box to enter the Name and also created submit button. result.jsp [java] <%@ 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> <font color="green"> SPLessons Register page.</font> </head> <body> Hai <s:property value="name" />, It is a struts Annotation application. </body> </html> [/java] In Struts 2 property tag is used to get the property value from a class, which will default to the current Action class (top of the stack) property if none is specified. LoginAction.java [java] package com.splessons; import org.apache.struts2.convention.annotation.*; @Action(value="login",results={@Result(name="success",location="/resultPages/result.jsp")}) public class LoginAction { private String name; public String getName() { return name; } public void setName(String name) { this.name = name; } public String execute() { return "success"; } } [/java] While using annotation no need to write any web.xml file why because annotation files will have meta data. 4. Create the property file under the src folder. struts.properties [java]struts.convention.package.locators=com.splessons struts.convention.result.path=/resultPages struts.convention.action.mapAllMatches=true [/java] @ResultPath annotation is used to control where Struts 2 will find the the stored results or JSP pages. By default, it will find the result pages from the “WEB-INF/content/” folder. web.xml [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_3_0.xsd" id="WebApp_ID" version="3.0"> <welcome-file-list> <welcome-file>index.jsp</welcome-file> </welcome-file-list> <filter> <filter-name>struts2</filter-name> <filter-class> org.apache.struts2.dispatcher.FilterDispatcher </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> [/xml] Output 5. Deployee the application in tomcat server and start the server, then the output will be shown as follows. 6. Enter the required fields and click on submit button.

Summary

shape Key Points

  • Struts 2 Annotations - Annotations are utilized to provide less code.
  • Struts 2 Annotations - The single result will be displayed by @Result tag.
  • Struts 2 Annotations - Multiple results will be displayed by @Results tag for an action.