How Spring is used in WEB apps

Source: Internet
Author: User

1. How does Spring work in WEB apps?

1). Additional jar packages are required:

Spring-web-4.0.0.release.jar
Spring-webmvc-4.0.0.release.jar

2). Spring configuration file, no difference

3). How do I create an IOC container?

①. Non-WEB apps are created directly in the Main method
②. The IOC container should be created when the WEB application is loaded by the server:

Create an IOC container in the servletcontextlistener#contextinitialized (Servletcontextevent sce) method.

③. How do I access the IOC container in other components of the WEB app?

After you create an IOC container in the servletcontextlistener#contextinitialized (Servletcontextevent sce) method, you can place it in the
A property of the ServletContext (that is, the application domain).

④. In fact, the name and location of the Spring configuration file should also be configurable! It is more appropriate to configure it to the initialization parameters of the current WEB application.

4). Use Spring in a WEB environment

①. jar packages that require additional addition:

Spring-web-4.0.0.release.jar
Spring-webmvc-4.0.0.release.jar

②. Spring configuration file, not unlike a non-WEB environment

③. You need to include the following configuration in the Web. xml file:

<!--Configure the name and location of the Spring configuration file--
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext.xml</param-value>
</context-param>

<!--launch the IOC container Servletcontextlistener--
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>

2. How does Spring integrate Struts2?

1). Integration Goals? Enable IOC containers to manage Struts2 's action!

2). How do I integrate?

①. Normal Join STRUTS2

②. Configuring the Struts2 Action in the Spring IOC container
Note: When configuring the Struts2 Action in an IOC container, the scope property needs to be configured with a value of prototype

<bean id= "Personaction"
Class= "Com.atguigu.spring.struts2.actions.PersonAction"
Scope= "Prototype" >
<property name= "Personservice" ref= "Personservice" ></property>
</bean>

③. Configuring the Struts2 configuration file: The class attribute of the action node needs to point to the ID of the bean in the IOC container

<action name= "Person-save" class= "Personaction" >
<result>/success.jsp</result>
</action>

④. Join Struts2-spring-plugin-2.3.15.3.jar

3). Integration principle: By adding Struts2-spring-plugin-2.3.15.3.jar, the Struts2 will be first removed from the IOC container
Gets an instance of the Action.

if (Appcontext.containsbean (Beanname)) {
o = Appcontext.getbean (beanname);
} else {
Class Beanclazz = getclassinstance (beanname);
o = Buildbean (Beanclazz, Extracontext);
}

How Spring is used in WEB apps

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.