Struts 2.x : [A Framework from ASF] :: Struts 2.x with Annotation
Struts 2.x with Annotation |
struts.xml :: No need to create struts.xml file, all actions and results will get annotated in action class only. hence below code is not required
<package name="user" namespace="/User" extends="struts-default">
<action name="Welcome" class="com.javaskool.action.WelcomeUserAction">
<result name="SUCCESS">pages/welcome_user.jsp</result>
</action>
</package>
Naming Converter
Struts 2 convention plugin will convert all the annotated action file name to a specified format.
The Struts 2 convention plugin’s “scanning methodology” and “naming converter” features are really bring a lot of conveniences and benefits, only if your Struts 2 project is following the naming convention properly; otherwise it will be a total disaster.
action,actions,struts,struts2 :: Packages whose name end with one of these strings will be scanned for actions
Click here to know more about conversion
Click here to know more examples for Struts2 from apache
As for Example
Now that the Convention plugin has been added to your application, let’s start with a very simple example. This example will use an actionless result that is identified by the URL. By default, the Convention plugin assumes that all of the results are stored in WEB-INF/content. This can be changed by setting the property struts.convention.result.path in the Struts properties file to the new location. Don’t worry about trailing slashes, the Convention plugin handles this for you. Here is our hello-world.jsp:
<html>
<body>
Hello world!
</body>
</html>
If you start Tomcat (or whichever J2EE container you are using) and type in http://localhost:8080/hello-world into your browser you should get this result:
WEB-INF/content/hello-world.jsp
Output
Hello world!
Example for Struts 2.x with Annotation |
Project Structure
Passenger.java
package com.javaskool.model;
public class Passenger
{
private String firstName;
private String lastName;
private String email;
private int age;
public String getFirstName()
{
return firstName;
}
public void setFirstName(String firstName)
{
this.firstName = firstName;
}
public String getLastName()
{
return lastName;
}
public void setLastName(String lastName)
{
this.lastName = lastName;
}
public String getEmail()
{
return email;
}
public void setEmail(String email)
{
this.email = email;
}
public int getAge()
{
return age;
}
public void setAge( int age)
{
this.age = age;
}
public String toString()
{
return "First Name: " + getFirstName() + " Last Name: " + getLastName() +
" Email: " + getEmail() + " Age: " + getAge() ;
}
}
HelloAction.java
package com.javaskool.actions;
import org.apache.log4j.Logger;
import org.apache.struts2.convention.annotation.Action;
import com.opensymphony.xwork2.ActionSupport;
public class HelloAction extends ActionSupport {
private static final long serialVersionUID = 1L;
private static final Logger logger = Logger.getLogger( HelloAction.class.getName() );
private String message;
@Action("hello-success")
public String execute() throws Exception {
logger.info("In execute method of class Hello");
message = "Hello from Struts 2 with no XML configuration.";
return SUCCESS;
}
public void setMessage(String message) {
this.message = message;
}
public String getMessage() {
return message;
}
}
RegisterAction.java
package com.javaskool.actions;
import org.apache.log4j.Logger;
import org.apache.struts2.convention.annotation.Action;
import com.opensymphony.xwork2.ActionSupport;
import com.javaskool.model.Passenger;
public class RegisterAction extends ActionSupport {
private static final long serialVersionUID = 1L;
private static final Logger logger = Logger.getLogger( RegisterAction.class.getName() );
private Passenger passengerBean;
@Action("register-input")
public String input() throws Exception {
logger.info("In input method of class RegisterAction");
return INPUT;
}
@Action("register-success")
public String execute() throws Exception {
logger.info("In execute method of class RegisterAction");
System.out.println("anuj "+passengerBean);
//call Service class to store passengerBean's state in database
return SUCCESS;
}
public Passenger getPassengerBean() {
return passengerBean;
}
public void setPassengerBean(Passenger passengerBean) {
this.passengerBean = passengerBean;
}
}
index.jsp
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<%@ taglib prefix="s" uri="/struts-tags" %>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Basic Struts 2.x Application - Welcome</title>
</head>
<body>
<h1>Welcome To Struts 2.x! with Annotation</h1>
<p><a href="<s:url action='hello-success' />" >Get your hello
Information.</a></p>
<p><a href="<s:url action='register-input' />" >Registration Form.</a></p>
</body>
</html>
hello-success.jsp
<%@ taglib prefix="s" uri="/struts-tags" %>
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<html>
<head>
<title>Hello</title>
</head>
<body>
<h3><s:property value="message" /></h3>
<h2>Welcome to the world of Struts 2.x with Annotation</h2>
<p><a href="index.jsp" >Click here to Return to home page</a>.</p>
<a href="http://javaskool.com">http://javaskool.com</a>
</body>
</html>
register-input.jsp
<%@ taglib prefix="s" uri="/struts-tags" %>
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<html>
<head>
<title>Register</title>
<s:head />
</head>
<body>
<h3>Register for a Gift by completing this form.</h3>
<s:form action="register-success">
<s:textfield name="passengerBean.firstName" label="First name" />
<s:textfield name="passengerBean.lastName" label="Last name" />
<s:textfield name="passengerBean.email" label ="Email"/>
<s:textfield name="passengerBean.age" label="Age" />
<s:submit/>
</s:form>
</body>
</html>
register-success.jsp.jsp
<%@ taglib prefix="s" uri="/struts-tags" %>
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<html>
<head>
<title>Registration Successful</title>
</head>
<body>
<h3>Thank you for registering for a Gift.</h3>
<p>Your registration information: <s:property value="passengerBean" /> </p>
<p><a href="index.jsp" >Return to home page</a>.</p>
</body>
</html>
struts.xml
This file is not required. since all the action and results have been annotated in action class.
jar files required in lib folder
Note: List of jar files differ as per project
antlr-2.7.2.jar
asm-3.3.jar
commons-beanutils-1.8.0.jar
commons-fileupload-1.2.2
commons-io-2.0.1
commons-lang-2.4
commons-lang3-3.1
commons-logging-1.1.1
commons-logging-api-1.1
freemarker-2.3.19
javassist-3.11.0.GA
log4j-1.2.11.jar
ognl-3.0.5
struts2-convention-plugin-2.3.15.3.jar
struts2-core-2.3.4
xwork-core-2.3.4
Click Here to download all jar files required for basic Struts 2.x Application
web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app id="WebApp_ID" version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee
http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
<display-name>12_Struts2.x_WithAnnotation3</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.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
<init-param>
<param-name>struts.devMode</param-name>
<param-value>false</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
</web-app>
Output
Click here to download complete code with jar files [5.3 MB]
Recent Comments