Spring 4 and Struts 2 project XML-based integration combat CHSZS, All rights reserved, without consent, not reproduced. Blogger Home: Http://blog.csdn.net/chszs
Why write this Hello world level article. About 10 years ago, I started developing web apps using Spring 2 and Struts 1, and the build tools were using Ant. In the early years, it was not easy to integrate multiple frameworks, and the compatibility between frameworks was not as good as it is now. In any case, these basic frameworks accompany us for many years. Now, 10 years later, the spring framework and the struts framework have undergone countless iterations and refactorings, and I re-made the integration practice of using MAVEN to govern the project, both to commemorate the past and to provide a ladder for the latter to get started quickly.
This article focuses on the development of Web projects by integrating the spring Framework version 4.2.1 with the Struts version 2.3.24.1 through an XML configuration.
First, why to integrate
Spring is an Enterprise Java Application Development framework, and struts is a Java Web application Development framework, which means they can be combined to develop Web applications.
Spring MVC is part of the spring framework for Building Java Web applications, and struts is based on the Java Servlet API and uses the MVC architecture. Therefore, when spring and struts are integrated, the MVC part should not overlap but should complement each other.
The spring framework has an excellent dependency injection management, so it can be used to manage business logic beans and struts ' action beans. On the other hand, we can use struts to be responsible for the MVC part of Web applications.
Second, how to integrate
The Struts 2 framework provides a plug-in called Spring plugin that can be integrated with the spring framework. This plugin provides such a jar package: Struts2-spring-plugin-version.jar, put this file into a Web project.
To enable the spring plugin plug-in, let the spring framework manage all of struts ' action beans through its inversion control container and declare it in the Spring app persistence context profile. In this way, the name of the bean can be used instead of the original "package name + class name" in the Struts configuration file. For example, this can be:
<action name="processOrder" class="ProcessOrderBean">
And the original is this:
<bean id="ProcessOrderBean" class="net.codejava.ProcessOrderDAO">
In addition, you can use the Spring Framework's @autowired annotation to inject a spring field into a dependency class.
Here we begin the actual combat.
The following tools are used in this article:
- Eclipse mars.1 Release (4.5.1): http://www.eclipse.org/downloads
- Maven 3.3.3:http://maven.apache.org/download.cgi
- Java SE 8u60:http://www.oracle.com/technetwork/java/javase/downloads/jdk8-downloads-2133151.html
- Spring Framework 4.2.1.release:http://projects.spring.io/spring-framework
- Struts 2 Framework 2.3.24.1:http://struts.apache.org/download.cgi
- Tomcat 8.0.27:http://tomcat.apache.org/download-80.cgi
Iii. creating MAVEN projects with Eclipse 1, creating a MAVEN project
Modify the facet of the project, adjust to dynamic Web Module (3.0) and Java (1.8)
2. Add Maven Dependency
Declaring versions of Java and spring frameworks
<properties> <java-version>1.8</java-version> <org.springframework-version>4.2.1.RELEASE</org.springframework-version></properties>
-
Add dependencies for the Spring framework
<dependency> <groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> <version>${org.springframework-version}</version > </dependency> <dependency> <groupId>org.springframework</groupId> <art Ifactid>spring-context-support</artifactid> <version>${org.springframework-version}</version > </dependency> <dependency> <groupId>org.springframework</groupId> <art Ifactid>spring-webmvc</artifactid> <version>${org.springframework-version}</version> </d Ependency>
Add a dependency on the Struts 2 framework
<dependency> <groupId>org.apache.struts</groupId> <artifactId>struts2-core</artifactId> <version>2.3.24.1</version> </dependency>
Adding a dependency on the spring plugin plugin
<dependency> <groupId>org.apache.struts</groupId> <artifactId>struts2-spring-plugin</artifactId> <version>2.3.24.1</version> </dependency>
Add Java Servlet & JSP dependencies required at compile time
<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>
3. Writing model Classes
To create a model class named User.java
package com.ch.web;public class User { private String username; private String email; private String password; public String getUsername() { return username; } public void setUsername(String username) { this.username = username; } 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; }}
4. Writing Business logic classes
Create a business logic class named Userdao.java:
package com.ch.web;public class UserDAO { public boolean checkLogin(User user){ return user.getUsername().equals("admin") && user.getPassword().equals("pwd"); }}
Visible, the valid user name is "admin", the password is "pwd".
5. Write the action class of struts
Write Struts's action class Loginaction.java
package com.ch.web;import com.opensymphony.xwork2.ActionSupport;public class LoginAction extends ActionSupport { private static final long serialVersionUID = 5852143705799268668L; private UserDAO userDAO; private User user; public void setUserDAO(UserDAO userDAO) { this.userDAO = userDAO; } public User getUser() { return user; } public void setUser(User user) { this.user = user; } public String execute(){ if (userDAO.checkLogin(user)) return SUCCESS; return ERROR; }}
Need to understand:
- Userdao's Setter method is for spring to inject the actual implementation class
- The getter and setter methods of the member variable user are used for the one by one correspondence between the form and the Model object user
- Method Execute () simply redirects to the success page or the error page
6. Write the login page
Create a page named loginform.jsp
<%@ 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">
7. Create a successful login and login failed pageLogin successful loginsuccess.jsp page
<%@ 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">
Login failed loginerror.jsp page
<%@ 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">
8. Configuring spring and Struts in Web. xmlThe contents of Web. XML are as follows
<?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=" Springstrutsidemo "version=" 3.0 "> <display-name> Springstrutsidemo</display-name> <context-param> <param-name>contextconfiglocation</param-n ame> <param-value>/WEB-INF/spring/appContext.xml</param-value> </context-param> <list Ener> <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> <welcome-file-list> <welcome-file>LoginForm.jsp</welcome-file> </welcome-file-list></web-app>
9. Writing Struts ConfigurationCreate a Struts.xml configuration file under Project src
<?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" namespace="/" 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>
10. Write Spring configuration fileCreate a configuration file named Appcontext.xml under the/web-inf/spring directory.
<?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="com.ch.web.UserDAO" /></beans>
11, the final project structureFinal Project structure:
Iv. Test ProjectAfter the project is deployed, the browser accesses: http://localhost:8080/SpringStrutsIntegrationDemo/LoginForm.jsp
Can see
After entering "admin" and "pwd", you can see
If you enter an error, you can see:
Visible, the project is fully functional. The XML-based integration of the Spring 4 and Struts 2 projects is successful.
Copyright NOTICE: This article for Bo Master Chszs original article, without Bo Master permission not reproduced.
Spring 4 and Struts 2 project XML-based integration combat