@Action( value = "login", results = { @Result(name = "success", location = "/WEB-INF/jsp/Login.jsp") } ) public class LoginAction extends ActionSupport { // empty }Mapped URL: http://localhost:8080/login
public class HelloAction extends ActionSupport{ @Action( value = "/hello", results = { @Result(name = "success", location = "/WEB-INF/jsp/Hello.jsp") } ) public String execute() { return SUCCESS; } }Mapped URL: http://localhost:8080/hello
@Result(name = "success", location = "/WEB-INF/jsp/Hello.jsp") public class HelloAction extends ActionSupport{ @Action(value = "/hello") public String execute() { return SUCCESS; } }Mapped URL: http://localhost:8080/hello
@Action("/order") @ResultPath("/WEB-INF/jsp") @Result(name = "success", location = "Order.jsp") public class OrderAction extends ActionSupport { }Mapped URL: http://localhost:8080/helloView location: /WEB-INF/jsp
@Namespace("/product") @Action("/list") @ResultPath("/WEB-INF/jsp") @Result(name = "success", location = "Products.jsp") public class ProductsAction extends ActionSupport { }Mapped URL: http://localhost:8080/product/listView location: /WEB-INF/jsp/product
@Action( value = "doLogin", results = { @Result(name = "input", location = "/WEB-INF/jsp/Login.jsp"), @Result(name = "success", location = "/WEB-INF/jsp/Hello.jsp") } ) public class DoLoginAction extends ActionSupport { private User user; public String execute() { if ("nimda".equals(user.getPassword())) { return SUCCESS; } else { return INPUT; } } public User getUser() { return user; } public void setUser(User user) { this.user = user; } }Mapped URL: http://localhost:8080/doLogin
public class MultiAction extends ActionSupport { @Action( value = "/bonjour", results = { @Result(name = "success", location = "/WEB-INF/jsp/Hello.jsp") } ) public String hello() { return SUCCESS; } @Action( value = "/listOrder", results = { @Result(name = "success", location = "/WEB-INF/jsp/Order.jsp") } ) public String order() { return SUCCESS; } }Mapped URLs:
<dependency> <groupId>org.apache.struts</groupId> <artifactId>struts2-core</artifactId> <version>2.3.20</version> </dependency>
<dependency> <groupId>org.apache.struts</groupId> <artifactId>struts2-convention-plugin</artifactId> <version>2.3.20</version> </dependency>
Create the Login.jsp file with the following code:
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <%@ 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> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>Login</title> </head> <body> <div align="center"> <h2>Login Form</h2> <s:form action="doLogin" method="post"> <s:textfield name="user.email" label="E-mail" /> <s:password name="user.password" label="Password" /> <s:submit value="Login" /> </s:form> </div> </body> </html>
Here, we use Struts’ form tags to create the login form.
Create the User.java class with the following code:
package net.codejava.struts; public class User { private String email; private String password; public String getEmail() { return email; } public void setEmail(String email) { this.email = email; } public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } }
Struts will create an object of this class in order to map form fields to a Java object in the action class.
Write code for the action class that brings the user to the login form (LoginAction.java):
package net.codejava.struts; import org.apache.struts2.convention.annotation.Action; import org.apache.struts2.convention.annotation.Result; import com.opensymphony.xwork2.ActionSupport; @Action( value = "login", results = { @Result(name = "success", location = "/WEB-INF/jsp/Login.jsp") } ) public class LoginAction extends ActionSupport { // empty }
This action handles the URL: http://localhost:8080/login
Write code for the action class handles login form (DoLoginAction.java):
package net.codejava.struts; import org.apache.struts2.convention.annotation.Action; import org.apache.struts2.convention.annotation.Result; import com.opensymphony.xwork2.ActionSupport; @Action( value = "doLogin", results = { @Result(name = "input", location = "/WEB-INF/jsp/Login.jsp"), @Result(name = "success", location = "/WEB-INF/jsp/Welcome.jsp") } ) public class DoLoginAction extends ActionSupport { private User user; public String execute() { if ("nimda".equals(user.getPassword())) { return SUCCESS; } else { return INPUT; } } public User getUser() { return user; } public void setUser(User user) { this.user = user; } }
This action handles the URL (POST): http://localhost:8080/doLogin
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>Welcome Home</title> </head> <body> <div align="center"> <h1>Welcome Home!</h1> </div> </body> </html>It simply displays a welcome message.
<?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/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0"> <display-name>Struts2AnnotationsExample</display-name> <filter> <filter-name>struts2</filter-name> <filter-class> org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter </filter-class> </filter> <filter-mapping> <filter-name>struts2</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> </web-app>
http://localhost:8080/Struts2AnnotationsExample/login
The login page appears as follows:Enter any email address but the password must be “nimda”, and then hit Enter. The welcome page appears as the following screenshot:Congratulations! You have done a simple Struts application that uses annotations. For your convenience, we attached the sample project (Eclipse) which you can download in the Attachments section. See you in the next Struts tutorial. References: