Spring + Struts2 + Hibernate Integration

Source: Internet
Author: User

I think the most important thing to learn about Spring is to integrate Spring + Struts2 + Hibernate. Spring is like a large container. After integrating struts2 and hibernate, the process is switched to spring's applicationContext. in the xml configuration file, paste the integration steps and configuration files here for future reference.

(It may not be clear. I hope you will forgive me! O (Clerk □clerk) o)

First, create a new Web Project with its name as needed, for example, ssh2, and then deploy the Project in the Config directory of the Apache installation directory (haha, this should be well known, so we will not have to make a statement ).

The order of integrating the three frameworks is as follows:

1. First introduce struts2, copy the jar package that struts2 depends to the lib directory of the WEB-INF in WebRoot, the imported jar package is as follows:

Then modify the web. xml configuration file under the WEB-INF and add the following code in it:

  <filter>      <filter-name>struts2</filter-name>      <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>  </filter>    <filter-mapping>      <filter-name>struts2</filter-name>      <url-pattern>/*</url-pattern>  </filter-mapping>

Finally, configure the struts. xml configuration file under the src directory as follows:

<Package name = "ssh2" extends = "struts-default"> <! -- There are several actions in the middle --> </package>

In this way, struts2 is introduced. In this case, start the server and verify whether the configuration is successful.

2. The next step is to introduce the Hibernate framework. directly use the Project Capabilities that comes with MyEclipse, and select Add Hibernate framework in it. Note that,Because this is a web project, the jar package must be placed under the lib directory of the WEB-INF.

After the hibernate framework is introduced, there will be more jar packages in the lib directory, because after hibernate is introduced, it will deal with the database, so in this case, the driver package of the database is also placed under the lib directory (I am using the mysql driver). After hibernate is introduced, there will be an additional hibernate under the src directory. cfg. xml file. This is the main configuration file of hibernate. Because we are using three major SSH frameworks for integration, the configuration database information is stored in the main configuration file of spring, so hibernate. cfg. we do not need to configure xml.

3. finally, we introduced the Spring framework. Just like introducing hibernate, we also used the Project Capabilities that comes with MyEclipse to import the Spring framework. When introducing the spring framework, we must note that when selecting the jar package, here I post an image:

(Note: Because struts and hibernate are integrated, you must introduce these four when introducing the jar package!)

Similarly,

Put all jar packages in the lib directory.

Click next.

In this page, remove the √ of the first box, and then put the applicationContext. xml configuration file below under the WEB-INF directory, and then click finish, so that the spring framework is also added.

4. spring integrates struts and hibernate, including instantiating each action and the configuration file of the database are stored in applicationContext. in the xml file, if you want to hand over the struts2 process to spring, then we need to put a plug-in provided by struts2 (Struts2-spring-plugin-2.3.4.1.jar) Is also added to the lib directory, and the most important step is to come, because struts2 will automatically load the jar package that struts2 depends on when the server is started, and now the process is handed over to spring, so we need to let the server automatically load the spring jar package when the server starts.

This is by adding a listener to web. xml, so the final web. xml configuration file code is as follows:

<?xml version="1.0" encoding="UTF-8"?><web-app version="2.5"     xmlns="http://java.sun.com/xml/ns/javaee"     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"     xsi:schemaLocation="http://java.sun.com/xml/ns/javaee     http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">  <listener>      <listener-class>          org.springframework.web.context.ContextLoaderListener      </listener-class>  </listener>      <filter>      <filter-name>struts2</filter-name>      <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>  </filter>    <filter-mapping>      <filter-name>struts2</filter-name>      <url-pattern>/*</url-pattern>  </filter-mapping>    </web-app>

In this case, we can start the server again to see if the server can be started normally, and then visit a page to test it. If everything is normal, congratulations, spring has integrated struts2. The next step is spring and hibernate.

5. previously, when we configured hibernate, we put all the database configuration files in hibernate. cfg. in the xml file, after the integration of spring, all these configurations are switched to the main configuration file of spring, because in actual projects, we will use the database connection pool for database connection, therefore, we use the DBCP database connection pool of the subproject commons under apache to configure database-related code. Therefore, we need to set dbcp. the jar package is introduced to the lib directory because dbcp depends on the pool again. jar, so put both jar files under the lib directory.

In this way, you can start hibernate-related configuration in applicationContext. xml. The Code is as follows:

<Bean id = "dataSource" class = "org. apache. commons. dbcp. basicDataSource "destroy-method =" close "> <property name =" driverClassName "> <value> com. mysql. jdbc. driver </value> </property> <property name = "url"> <value> jdbc: mysql: // localhost: 3306/ssh2 </value> </property> <property name = "username"> <value> root </value> </property> <property name = "password"> <value> 123 </value> </property> </bean> <bean id = "sessionFactory" class = "org. springframework. orm. hibernate3.LocalSessionFactoryBean "> <property name =" dataSource "ref =" dataSource "> </property>
<! -- If multiple hbm instances exist. xml files are stored in the list set --> <property name = "mappingResources"> <list> <value> com/xiaoluo/bean/User. hbm. xml </value> </list> </property> <property name = "hibernateProperties"> <props> <prop key = "hibernate. dialect "> org. hibernate. dialect. mySQL5Dialect </prop> <prop key = "hibernate. show_ SQL "> true </prop> </props> </property> </bean>

The above two configuration files are the common code used for ssh integration. All you need to do is modify hbm. xml.

 

So far, the three frameworks Struts2 + Spring + Hibernate have been integrated, so you can compile a database for addition, deletion, modification, and query to test.

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.