Spring integrated struts, Hibernate----three-frame ssh (Spring, struts, and Hibernate)

Source: Internet
Author: User

The Spring framework accomplishes the design tasks of the business layer, and the struts framework separates the presentation layer from the business layer, while the Hibernate framework provides flexible persistence layer support. The following is a description of the three framework integration environments:

1, configuration Struts2.

I, import the related jar package.

II, modify the Web. xml file. The core control of STRUTS2 is an internship with a filter, so it is necessary to configure the filter in the Web. xml file to load the STRUST2 framework. The code for the Web. xml file is as follows:

    

1<!--Configuring the core of the STRUTS2 framework Filter--2<filter>3<!--Specify the name of the core filter--4<filter-name>struts2</filter-name>5<!--specify the implementation class for the core filter--6<filter-class>7 Org.apache.struts2.dispatcher.FilterDispatcher8</filter-class>9</filter>Ten<!--Configure filter blocking URLs-- One<filter-mapping> A<filter-name>struts2</filter-name> -<url-pattern>/*</url-pattern> - </filter-mapping>

It is important to note that If the Web container is using the Servlet2.4 and above specifications, you do not need to manually load the STRUTS2 tag library in the. xml file because these versions automatically load the tag library definition file, and if the Web container uses a previous version of Servlet2.3, the Struts2 frame's label will not be automatically loaded To sign the file, you need to manually configure it in Web. XML, the configuration code for the file Web. config tag library is as follows:

1 <!--manually configure the STRUTS2 tag Library--2     <taglib>3         <taglib-url>/s</ Taglib-url> <!--Configure the URL of the STRUTS2 tag Library--4         <!--Specify the path to the STRUTS2 tag library definition file--5         < Taglib-location>/web-inf/struts-tags.tld</taglib-location>6     </taglib>

III, in the first two steps we have added support for Struts2 in the Web project, but we can no longer use STRUTS2 functionality in the project. In order to be able to use the STRUTS2 feature, you also need to add Struts2 configuration file Struts.xml file:

    

1<struts>2       3<!--set whether the browser caches static content, the default value is True (used in a production environment), and the development phase is best off--4<constant name= "Struts.serve.static.browserCache" value= "false"/>5<!--when a struts profile is modified, the system automatically reloads the file, the default value is False (used in production), and the development phase is best opened-6<constant name= "Struts.configuration.xml.reload" value= "true"/>7<!--development mode, so you can print out more detailed error messages--8<constant name= "Struts.devmode" value= "true"/>9<!--default View theme--Ten<constant name= "Struts.ui.theme" value= "simple"/> One<!--<constant name= "struts.objectfactory" value= "Spring"/>--> A<!--solving garbled-- -<constant name= "Struts.locale" value= "Zh_cn"/> -<constant name= "struts.i18n.encoding" value= "UTF-8"/> the<!--Specifies the maximum number of bytes that are allowed for uploading files. The default value is 2097152 (2M)-- -<constant name= "struts.multipart.maxSize" value= "10701096"/> -<!--set up a temporary folder to upload files by default Javax.servlet.context.tempdir-- -<constant name= "Struts.multipart.saveDir" value= "d:/tmp"/> +< PackageName= "Struts_interceptor"extends= "Struts-default" > -<interceptors> +<interceptor name= "Havenlogininterceptor"class= "Com.jyw.interceptor.VidatorLoginInterceptor" ></interceptor> A<interceptor-stack name= "Loginstack" > at<interceptor-ref name= "Havenlogininterceptor" ></interceptor-ref> -<interceptor-ref name= "Defaultstack"/> -</interceptor-stack> -</interceptors> -<default-interceptor-ref name= "Loginstack" ></default-interceptor-ref> -</ Package> in      -< PackageName= "Struts_token_interceptor"extends= "Struts-default" > to<interceptors> +<interceptor-stack name= "Tokenstack" > -<interceptor-ref name= "token"/> the<interceptor-ref name= "Token-session"/> *<interceptor-ref name= "Defaultstack"/> $</interceptor-stack>Panax Notoginseng</interceptors> -<default-interceptor-ref name= "Tokenstack"/> the</ Package> +      A<include file= "Struts_base.xml" ></include> the<include file= "Struts_interceptor.xml" ></include> +<include file= "Struts_json.xml" ></include> -<include file= "Struts_token.xml" ></include> $</struts>

We can see that there is a <package> tag in the Struts.xml file, which is a label about the properties of the package that is managed by the package in the STRUTS2 framework to manage action, result, interceptor, Interceptor-stack and other configuration information.

2. Spring Integrated Hibernate

We know that Hibernate's sessionfactory is a very important object, a Java Web application typically corresponds to a database, and also corresponds to a Sessionfactory object, when using hibernate to access the database, The application will first create an sessionfactory instance, and with spring integrated with hibernate, you can manage the Sessionfactory instance declaratively in the spring configuration file, while You can also take advantage of Spring's IOC container to inject a reference to the data source when you configure Sessionfactory. Configure the data source in spring's configuration file Applicationcontext.xml:

  

1<?xml version= "1.0" encoding= "UTF-8"?>2<Beans3Xmlns= "Http://www.springframework.org/schema/beans"4Xmlns:xsi= "Http://www.w3.org/2001/XMLSchema-instance"5xmlns:context= "Http://www.springframework.org/schema/context"6xmlns:aop= "HTTP://WWW.SPRINGFRAMEWORK.ORG/SCHEMA/AOP"7xmlns:tx= "Http://www.springframework.org/schema/tx"8Xsi:schemalocation= "Http://www.springframework.org/schema/beans9http//www.springframework.org/schema/beans/spring-beans-2.5.xsdTenhttp//Www.springframework.org/schema/context Onehttp//www.springframework.org/schema/context/spring-context-2.5.xsd Ahttp//WWW.SPRINGFRAMEWORK.ORG/SCHEMA/AOP -http//www.springframework.org/schema/aop/spring-aop-2.5.xsd -http//Www.springframework.org/schema/txhttp://www.springframework.org/schema/tx/spring-tx-2.5.xsd  the"> -<!--defining data sources-- -<bean id= "DataSource" -         class= "Org.springframework.jndi.JndiObjectFactoryBean" > +<!--Specify the drive to connect to the database -<property name= "Driverclassname" > +<value>com.mysql.jdbc.Driver</value> A</property> at<property name= "url" > -<value>jdbc:mysql://localhost/spring</value> -</property> -<property name= "username" > -<value>root</value> -</property> in<property name= "Password" > -<value>root</value> to</property> +</bean> -  the<bean id= "Sessionfactory" *         class= "Org.springframework.orm.hibernate3.LocalSessionFactoryBean" > $<property name= "DataSource" >Panax Notoginseng<ref bean= "DataSource"/> -</property> the<property name= "Configlocation" > +<value>classpath:hibernate.cfg.xml</value> A</property> the<!--Configure Hibernate Properties-- +<property name= "Hibernateproperties" > -<props> $<!--Configure Hibernate database dialect--- $<prop key= "Hibernate.dialect" >org.hibernate.dialect.MySQLDialect</prop> -<prop key= "Show_sql" >true</prop> -</props> the</property> -</bean>Wuyi</beans>

In the process of integrating hibernate in spring, the main configuration is DataSource and Sessionfactory, where DataSource is primarily the connection properties of the configuration data, which can be placed separately in a file, The sessionfactory is primarily used to manage hibernate configuration, and after the configuration of the sessionfactory is completed in the spring configuration file, the Sessionfactory bean can be injected into other beans. After you have configured sessionfactory in spring, you can use Hibernatetemplate for database access.

3. Spring Integrated Struts

The spring framework allows struts to be seamlessly connected to the spring-based business layer and persistence layer as a web framework. The following describes spring's context loading and spring integration Struts2:

I, load the spring context in the Web. xml file. In order for the spring container to start automatically as the Web application launches, the following two configuration options are available: Servletcontextlistener configuration and Load-on-startup servlet configuration.

Using the Servletcontextlistener configuration is actually done using an implementation class Contextloaderlistener of Servletcontextlistener to complete the configuration, The Contextloaderlistener class can be used as a listener listener, using it to complete the configured code as follows:

1<context-param>2<!--parameter name is Contextconfiglocation--3<param-name>contextConfigLocation</param-name>4<!--specifying Profiles--5<param-value>classpath:config.xml</param-value>6</context-param>7<!--listener--8<listener>9<!--Initialize Spring container with Contextloaderlistener--Ten<listener-class> One Org.springframework.web.context.ContextLoaderListener A</listener-class> -</listener>

Note: (1) Contextloaderlistener is a listener class provided by spring that automatically finds the web-inf/when it is created The Applicationcontext.xml file under the path, so that when there is only one profile and the configuration file name is: Applicationcontext.xml, you only need to configure the listener element in the Web. xml file to create the spring container. (2) using the Context-param element to specify the file name of the configuration file, when Contextloaderlistener loads, an initialization parameter named Contextconfiglocation is automatically found and the configuration file specified by the parameter is used. The. config file is here. If you do not use Contextconfiglocation to specify a configuration file, spring will automatically find the Applicationcontext.xml file, and if it is not found, spring will not start properly. (3) If you have more than one spring configuration file loaded, you also need to use the Context-param element to set it.

II, Spring integrated Struts2. The purpose of the spring integrated Struts2 is to have the instantiation of the action in Struts2 managed by the spring container, while enabling the action instance in Struts2 to access the business logic component resources provided by spring. And the spring container itself has the advantage of the dependency injection can be fully played out. STRUTS2 and spring are integrated with spring's plug-in package, which is released with Struts2. Struts2-spring-plugin-2.2.3.1.jar is the class package that STRUTS2 provides for integration into spring, which contains a struts-plugin.xml configuration file. A bean named Spring is defined in the Struts-plugin.xml configuration file. The Bean's implementation class is org.apache.struts2.spring.StrutsSpringObjectFactory, and the purpose of defining the bean is to conveniently delegate the management of the action class in struts2 to the spring container for processing. In other words, after configuring Strutsspringobjectfactory, the Struts2 configuration file can refer directly to the bean in the spring container.

The Struts-plugin.xml configuration file is as follows:

 <struts> <bean type= "com.opensymphony.xwork2.ObjectFactory" Name= " Spring "class  =" Org.apache.struts2.spring.StrutsSpringObjectFactory "/> & lt;! -Make the Spring object factory the automatic default --<constant name= "Struts.objectfactory" value= "Spring"/> <package  name= "Spring-default" > <!--Configuring Interceptors--<interceptors> <interceptor name= "autowiring" class  = "Com.opensymphony.xwork2.spring.interceptor.ActionAutowiringInterceptor"/> <interceptor nam E= "sessionautowiring" class  = "  Org.apache.struts2.spring.interceptor.SessionContextAutowiringInterceptor "/> </interceptors> </package  > </struts>  

The code first defines a bean named spring, Then use spring's objectfactory to overwrite the STRUTS2 constant struts.objectfactory in the constant setting, and the Value property in the constant element is set to spring, as opposed to the Name property value Spring in the bean configuration. The changes in the Struts.xml file are as follows:

<struts> <constant name= "struts.objectfactory" value= "Spring"/> </struts>

Note: The Struts2 action instance is configured in spring configuration file Applicationcontext.xml

Spring: 1 <bean id= "Helloworldbean" class= "Com.integrationhelloworldbean" > 2 <property name= "message" value= "Hello World" ></property> 3 </bean>

Struts:

1 <struts>2     <packageextends= "Struts-default" >3         Class= "helloworldbeancom.integrationhelloworldbean4             < Result name= "Success" >/jsp/exma/ExmaContext.jsp</result>5         </action>6 </Package >7 </struts>

Spring integrated struts, Hibernate----three-frame ssh (Spring, struts, and Hibernate)

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.