Struts 2 Example by using Annotations.
1. Create the project directory structure.
2.Add the all jar files in lib folder.
- struts2-convention-plugin.2.3.jar
- asm-3.1.jar
- antlr-2.7.2.jar
- commons-fileupload-1.2.1.jar
- commons-io-1.3.2
- commons-lang-2.3
- commons-logging-1.0.4
- commons-logging-api-1.1.jar
- javasist.jar
- ognl-2.7.3.jar
- struts-core-2.1.8.jar
- xwork-core-2.1.6.jar
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.