SSH -- Introduce Spring in Struts2

Source: Internet
Author: User

SSH -- Introduce Spring in Struts2

 

I. Why should I use Spring?

 

1. Assemble JavaBean

 

Without the old new method, spring provides a mechanism for us to create a javaBean and set the javaBean attributes through the configuration file and the Spring framework itself. In this way, you can modify the Spring configuration file when changes are required in some places. In fact, the Spring framework reads the content in the corresponding configuration file and automatically installs the content in the javaBean object according to these configurations to set the attributes of the JavaBean object.

 

2. Integrate the third-party framework.

 

Spring's design philosophy is to integrate third-party frameworks as much as possible to make these integrated technologies easier to use and maintain, thus greatly reducing the difficulty of program development.

 

 

2. Introduce Spring in struts2

 

0. Introduce the jar package of spring.

 

Com.springsource.org. apache. log4j-1.2.15.jar

Spring-beans-3.2.0.RELEASE.jar

Spring-context-3.2.0.RELEASE.jar

Spring-core-3.2.0.RELEASE.jar

Spring-expression-3.2.0.RELEASE.jar

Spring-web-3.2.0.RELEASE.jar

Struts2-spring-plugin-2.3.15.3.jar


1. Configure the spring listener

Add the following configuration in our web. xml:

 

    
    
     
  
   
Org. springframework. web. context. ContextLoaderListener
    
 

 

 

The ContextLoaderListener class is used to automatically assemble the configuration information of ApplicationContext when a Web container is started.

 

2. applicationContext. xml configuration

 

We create the applicationContext. xml configuration file under the WEB-INF directory (of course, it can also be built elsewhere, called another name, where we configure it in the simplest way ).

 

For example:

 

 
 
  
  
   
    
     
{0} + {1 }={ 2}
    
   
  
 

 

 

Our action class is as follows:

 

 

Package net. blogjava. nokiaguy. models; public class SpringCalcAction {private int operand1; private int operand2; // The calculator attribute is automatically assembled by the struts2 plug-in and must have the same name in the spring configuration file
 
  
Element private Calculator calculator; public int getOperand1 () {return operand1;} public void setOperand1 (int operand1) {this. operand1 = operand1;} public int getOperand2 () {return operand2;} public void setOperand2 (int operand2) {this. operand2 = operand2;} public Calculator getCalculator () {return calculator;} public void setCalculator (Calculator calculator) {this. calculator = calculator;} public String execute () {// you only need to call the calc method of the Calculator object. When switching the business model, you do not need to modify the following code int value = calculator. calc (operand1, operand2); return success ;}}
 


 

In the preceding Action class, there is a Calculator attribute of the calculator type. This type is a parent class. In the spring configuration file, we configure the Assembly class for this attribute. when the Action is called, The subclass instance is automatically assembled into the parent class object. Note: In spring, the bean id must be consistent with the attribute name in action.

 

 

 

 

 

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.