Introduction of Spring in SSH--STRUTS2

Source: Internet
Author: User


One, why use spring


1, Assembly JavaBean


By abandoning the old New way, Spring provides us with a mechanism for creating javabean and setting the JavaBean property to be done through the configuration file and the spring framework itself. This way, when changes are needed in some places, you can modify the spring configuration file. This process is actually the spring framework that sets the properties of the JavaBean by reading the contents of the corresponding configuration file and automatically loading the JavaBean object according to these configurations.


2, integration of third-party frameworks.


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



Two, introduction of spring in Struts2


0, Introduction of Spring jar Package

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 Spring Listener

in our web. XML, add the following configuration:

    <!--Spring Listener configuration-   <listener>    <listener-class> Org.springframework.web.context.contextloaderlistener</listener-class>  </listener>


Contextloaderlistener This class is responsible for automatically assembling ApplicationContext configuration information when the Web container is started.


2,applicationcontext.xml Configuration


we set up the Applicationcontext.xml configuration file under the Web-inf directory (and of course, it can be built elsewhere, called other names, which we configure in the simplest way).


For example:

<?xml version= "1.0" encoding= "UTF-8"? ><beans xmlns= "Http://www.springframework.org/schema/beans" xmlns: Xsi= "Http://www.w3.org/2001/XMLSchema-instance" xmlns:context= "Http://www.springframework.org/schema/context" xmlns:aop= "HTTP://WWW.SPRINGFRAMEWORK.ORG/SCHEMA/AOP" xmlns:tx= "Http://www.springframework.org/schema/tx" xsi: schemalocation= "Http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/ spring-beans.xsdhttp://www.springframework.org/schema/contexthttp://www.springframework.org/schema/context/ spring-context.xsdhttp://www.springframework.org/schema/aophttp://www.springframework.org/schema/aop/ Spring-aop.xsdhttp://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd " ><!--assembling Additioncalc class--><bean id= "Calculator" class= "Net.blogjava.nokiaguy.models.AdditionCalc" > <property name= "Message" ><value>{0}+{1}={2}</value></property></bean></beans >


our action class is as follows:


Package Net.blogjava.nokiaguy.models;public class Springcalcaction {private int operand1;private int operand2;// The Calculator property is automatically assembled by the STRUTS2 plug-in and requires the same name as the <bean> element private Calculator calculator;public int getOperand1 () in the spring configuration file { 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 () {///only need to invoke 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 action class above, there is an attribute calculator of type calculator. This type is a parent class. In spring's configuration file, we configured the assembly class for this property, and when the action is invoked, the subclass instance is automatically assembled into the parent class object. Note that in spring, the Bean's ID is consistent with the name of the property in the action.






Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Introduction of Spring in SSH--STRUTS2

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.