<action name="processOrder" class="ProcessOrderBean">Here, the class name ProcessOrderBean is ID of a bean declared in Spring’s configuration:
<bean id="ProcessOrderBean" class="net.codejava.ProcessOrderDAO">In addition, we can also use the @Autowired annotation of Spring to let Spring automatically injects dependencies into a class, just like in normal Spring application.Well, that’s enough for the theory. Let’s move forward to the practice part: developing a web application with login function using Spring and Struts 2.To make you follow this tutorial easily, here’s a list of technologies and pieces of software we use:
<properties> <java-version>1.8</java-version> <org.springframework-version>4.1.2.RELEASE</org.springframework-version> </properties>
<dependency> <groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> <version>${org.springframework-version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context-support</artifactId> <version>${org.springframework-version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-webmvc</artifactId> <version>${org.springframework-version}</version> </dependency>
<dependency> <groupId>org.apache.struts</groupId> <artifactId>struts2-core</artifactId> <version>2.3.16.3</version> </dependency>
<dependency> <groupId>org.apache.struts</groupId> <artifactId>struts2-spring-plugin</artifactId> <version>2.3.16.3</version> </dependency>
<dependency> <groupId>javax.servlet</groupId> <artifactId>javax.servlet-api</artifactId> <version>3.1.0</version> <scope>provided</scope> </dependency> <dependency> <groupId>javax.servlet.jsp</groupId> <artifactId>javax.servlet.jsp-api</artifactId> <version>2.3.1</version> <scope>provided</scope> </dependency>
package net.codejava.web; public class User { private String username; private String email; private String password; // getters and setters }NOTE: getters and setters are removed for brevity. You should write getters and setters completely.
package net.codejava.web; public class UserDAO { public boolean checkLogin(User user) { return user.getUsername().equals("admin") && user.getPassword().equals("nimda"); } }As you can see, this DAO class has a very simple business method checkLogin()that is for authenticating the user. The valid username is “admin” and password is “nimda”.
package net.codejava.web; import com.opensymphony.xwork2.ActionSupport; public class LoginAction extends ActionSupport { private UserDAO userDAO; private User user; public void setUserDAO(UserDAO userDAO) { this.userDAO = userDAO; } public void setUser(User user) { this.user = user; } public User getUser() { return user; } public String execute() { if (userDAO.checkLogin(user)) { return SUCCESS; } return ERROR; } }Although this is a simple Struts 2 action class, we still need to understand the following points:
<%@ 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>Spring and Struts Integration Demo</title> </head> <body> <div align="center"> <h1>Spring and Struts Integration Demo</h1> <h2>Users Login</h2> <s:form action="login" method="post"> <s:textfield label="Username" name="user.username" /> <s:password label="Password" name="user.password" /> <s:submit value="Login" /> </s:form> </div> </body> </html>This is a simple login form using Struts’ form tags.
<%@ 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>Login Success</title> </head> <body> <div align="center"> <h1>Welcome to CodeJava.net</h1> </div> </body> </html>And an error page (LoginError.jsp) will be displayed if the user is not authenticated:
<%@ 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>Login Error</title> </head> <body> <div align="center"> <h1>Error login. Wrong username/password</h1> </div> </body> </html>These two pages simply display relevant text, one for login successfully, and another for login failed.
<?xml version="1.0" encoding="UTF-8"?> <web-app version="3.0" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"> <context-param> <param-name>contextConfigLocation</param-name> <param-value>/WEB-INF/spring/appContext.xml</param-value> </context-param> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> <filter> <filter-name>DispatcherFilter</filter-name> <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class> </filter> <filter-mapping> <filter-name>DispatcherFilter</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> </web-app>Here, we configure our web application to load Spring and Struts differently:
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN" "http://struts.apache.org/dtds/struts-2.0.dtd"> <struts> <package name="Struts2SpringDemo" extends="struts-default"> <action name="login" class="loginActionBean"> <result name="input">/LoginForm.jsp</result> <result name="success">/LoginSuccess.jsp</result> <result name="error">/LoginError.jsp</result> </action> </package> </struts>As we can see, we declare an action called “login” - note that this name must match the action property of the form in the LoginForm.jsp file. Inside this action’s body, we declare mappings of JSP pages with view names for the INPUT, SUCCESS, and ERROR pages.The interesting point here is the class property of the action doesn’t point to a fully qualified class name as usual. Instead it points to ID of a Spring’s bean. So we see how to configure beans in Spring right now.
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"> <bean id="loginActionBean" class="net.codejava.web.LoginAction"> <property name="userDAO" ref="userDAO" /> </bean> <bean id="userDAO" class="net.codejava.web.UserDAO" /> </beans>In this Spring application context configuration file, we declare two beans:
http://localhost:8080/SpringStrutsIntegrationDemo/LoginForm.jsp
The login form appears like in the following screenshot:Type “admin” for username and “nimda” for password, and then hit Enter. The success page appears:In case you typed wrong username/password, the error page gets displayed:Congratulations! You have successfully built a Spring-Struts type of application. Come back often to check out next tutorial in this series.