struts.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>
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
<%@ 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>
Here the developer just created the text box to enter the Name and also created submit button.
result.jsp
<%@ 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>
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
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"; } }
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
struts.convention.package.locators=com.splessons struts.convention.result.path=/resultPages struts.convention.action.mapAllMatches=true
@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 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>
Output
5. Deployee the application in tomcat server and start the server, then the output will be shown as follows.