XML-based integration of Spring 4 and Struts 2

Source: Internet
Author: User

XML-based integration of Spring 4 and Struts 2
Author of XML-based integration between Spring 4 and Struts 2: chszs. Copyright. It cannot be reproduced without consent. Blogger home: http://blog.csdn.net/chszs

Why write this article at the Hello World level. About 10 years ago, I began to use Spring 2 and Struts 1 to develop Web applications. The build tool uses Ant. In earlier 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 have been with us for years. Now, over ten years, the Spring framework and Struts framework have been iterated and reconstructed in countless versions. I have re-implemented an Integration practice to use Maven to manage projects, both to commemorate the past, it also provides a step for later users to help beginners get started quickly.

This article describes how to integrate Spring Framework 4.2.1 with Struts 2.3.24.1 through XML configuration for Web project development.

I. Why integration?

Spring is an Enterprise Java application development framework, while Struts is a Java Web application development framework, which means that the two can be combined to develop Web applications.

Spring MVC is part of the Spring framework and is used to build Java Web applications. Struts also uses the MVC Architecture Based on the Java Servlet API. 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 take charge of the MVC part of Web applications.

Ii. How to Integrate

The Struts 2 Framework provides a plug-in named Spring Plugin that can be integrated with the Spring framework. This plug-in provides a JAR package: struts2-spring-plugin-VERSION.jar that puts this file into a Web project.

To enable the Spring plug-in, let the Spring framework use its reverse control container to manage all Action beans of Struts and declare them in the Spring application continuous context configuration file. In this way, the Bean name can be used in the Struts configuration file to replace the original "package name + class name" method. For example:

This was originally the case:


  

In addition, you can use the @ Autowired annotation of the Spring framework to inject Spring fields into the class.

Next we will start to practice.

The tool used in this article is as follows:

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: Release

Modify the project Facet to Dynamic Web Module (3.0) and Java (1.8)

2. Add Maven dependency

Declare the Java and Spring frameworks


      
   
    1.8
       
   
    4.2.1.RELEASE
   
  

Add Spring framework Dependencies

    
          
   
    org.springframework
           spring-context        
   
    ${org.springframework-version}
       
      
          
   
    org.springframework
           spring-context-support        
   
    ${org.springframework-version}
       
      
          
   
    org.springframework
           spring-webmvc        
   
    ${org.springframework-version}
       
  

Add Struts 2 Framework Dependencies

    
          
   
    org.apache.struts
           struts2-core        
   
    2.3.24.1
       
  

Add Spring plug-in Dependencies

    
          
   
    org.apache.struts
           struts2-spring-plugin        
   
    2.3.24.1
       
  

Add Java Servlet & JSP dependencies required for compilation

    
          
   
    javax.servlet
           javax.servlet-api        
   
    3.1.0
           
   
    provided
       
      
          
   
    javax.servlet.jsp
           javax.servlet.jsp-api        
   
    2.3.1
           
   
    provided
       
  
3. Compile model classes

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. Write Business Logic

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);    }}

The valid user name is "admin" and the password is "pwd ".

5. Compile the Struts Action class.

Write the Struts 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:

The Setter method of userDao is used for Spring injection to implement the getter and setter methods of the class member variable user. It is used to execute () the one-to-one correspondence method between the form and the model object User () simple redirection to the SUCCESS page or ERROR page 6. Write the logon 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%>
Spring and Struts Integration Demo Users Login 7. Create a logon success or logon Failure page

Log on to the LoginSuccess. jsp page.

<%@ page language=java contentType=text/html; charset=UTF-8    pageEncoding=UTF-8%>
Login successful. Welcome!

LoginError. jsp page for Logon Failure

<%@ page language=java contentType=text/html; charset=UTF-8    pageEncoding=UTF-8%>
Logon Failed! Incorrect username or password8. Configure Spring and Struts in web. xml

The content of web. xml is as follows:


  
      
   
    SpringStrutsIDemo
       
           
    
     contextConfigLocation
            
    
     /WEB-INF/spring/appContext.xml
        
       
           
    
     org.springframework.web.context.ContextLoaderListener
        
       
           
    
     DispatcherFilter
            
    
     org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter
        
       
           
    
     DispatcherFilter
            
    
     /*
        
       
           
    
     LoginForm.jsp
        
   
  
9. Compile Struts Configuration

Create a struts. xml configuration file under the src Project


  
      
                       
    
     /LoginForm.jsp
                
    
     /LoginSuccess.jsp
                
    
     /LoginError.jsp
                
   
  
10. Compile the Spring configuration file

Create a configuration file named appContext. xml under the/WEB-INF/spring directory.


  
      
           
        
    
       
   
  
11. Final project structure

Final project structure:

Iv. Test Items

After the project is deployed, visit http: // localhost: 8080/SpringStrutsIntegrationDemo/LoginForm. jsp in the browser.

We can see that

After entering "admin" and "pwd", you can see

If the input is incorrect, you can see:

It can be seen that the project works properly. XML-based integration between Spring 4 and Struts 2 is successful.

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.