First, the introduction of the framework to add the environment:
- Junit
- Struts2
- Hibernate
- Spring
(1) Configuring JUnit
/**-------------------------Add junit-------------------------------*/
Right-click the Web project--->build Path--->add Libraries---> select JUnit in the ADD Libraries window---
--->next---> select version junit 4
/**-------------------------Complete junit! ------------------------------*/
(2) Struts2
/**-------------------------Add Struts-------------------------------*/
1. Add jar to copy all the jars under Path Struts-2.3.16-all\struts-2.3.16\apps\struts2-blank\web-inf\lib to Lib under Project Web-inf.
2. Copy and edit Web. Xml to struts-2.3.16-all\struts-2.3.16\apps\struts2-blank\ the path The code for configuring the STRUTS2 core filter in Web. XML under Web-inf is copied to the Project Web-inf Web. xml:
Edit Web. xml:
<!--configuring STRUTS2 Core Filters-- <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>
3. Copy and edit the Struts.xml, first create a new source folder named Config under the project to store the configuration file.
Copy the Struts.xml file under path Struts-2.3.16-all\struts-2.3.16\apps\struts2-blank\web-inf\src\java to the config package.
Edit as follows:
<?xml version= "1.0" encoding= "UTF-8"? ><! DOCTYPE struts public "-//apache software foundation//dtd struts Configuration 2.0//en" "/http Struts.apache.org/dtds/struts-2.0.dtd "><struts> <!--configured for development mode, once the file is changed, no restart is required, it takes effect immediately-- < Constant Name= "Struts.devmode" value= "true"/> <!--set the extension to action-- <constant name= " Struts.action.extension "value=" action "/> <!--To configure the theme simple-to- <constant name=" Struts.ui.theme "value=" simple "/> <package name=" Default "namespace="/"extends=" Struts-default "> </package></struts>
/**-------------------------Complete struts! -------------------------------*/
(3) Hibernate
- Add a Jar Package
- Master configuration file Hibernate.cfg.xml
- mapping Files *.hbm.xml
/**-------------------------Add Hibernate-------------------------------*/
1. Add Jar Package:
Add Core package: Hibernate3.jar under Path hibernate-distribution-3.6.0.final.
Add a must package: all jars under Path hibernate-distribution-3.6.0.final\lib\required
Add a JPA package: all jars under Path HIBERNATE-DISTRIBUTION-3.6.0.FINAL\LIB\JPA
Add Optional Package: All packages under path HIBERNATE-DISTRIBUTION-3.6.0.FINAL\LIB\OPTIONAL\C3P0:
To add a JDBC driver package:
2. Copy and configure the file Hibernate.cfg.xml
Search and copy the Hibernate.cfg.xml files and log4j.properties files under Hibernate-distribution-3.6.0.final, and place them under the Project Config package for editing and editing later.
<?xml version= ' 1.0 ' encoding= ' utf-8 '? ><! DOCTYPE hibernate-configuration public "-//hibernate/hibernate configuration DTD 3.0//en" "http// Www.hibernate.org/dtd/hibernate-configuration-3.0.dtd ">
3. Copy and configure the mapping file *.hbm.xml
Search and copy a *.hbm.xml file under Hibernate-distribution-3.6.0.final, put it under the domain package, and edit it later.
/**-------------------------Complete hibernate! -------------------------------*/
(4) Spring
- Add a Jar Package
- Configuration file Applicationcontext.xml
/**-------------------------Add Spring-------------------------------*/
1. Add Jar:
Copy the core jar package, Spring.jar under Path spring-framework-2.5.6.sec01\dist.
All jar packages under path SPRING-FRAMEWORK-2.5.6.SEC01\LIB\ASPECTJ:
Jar Package under Path spring-framework-2.5.6.sec01\lib\cglib
Jar Package under Path spring-framework-2.5.6.sec01\lib\jakarta-commons
2, the configuration file, later integration when again.
/**-------------------------Complete spring! The configuration file says later-------------------------------*/
Finally, all the jar packages required for the OA project are shown:
The light import of these jars is still not enough to integrate SSH, the following through the integration of STRUTS2 and spring, Hibernate and spring integration to complete the SSH environment to build.
This blog will integrate struts and spring by modifying the configuration file, and the next blog integrates hibernate and spring to complete the environment, as mentioned above for the three frameworks required to build the SSH environment.
1, declare the bean, new Testaction.java, need to add annotations to the class: @Controller and @Scope ("prototype");
"After using @controller annotations to identify testaction, it means to give testaction to the spring container, and there will be an action named" Testaction "in the Spring container. This name is based on the Testaction class name.
Note: If @controller does not specify its value "@Controller", the default bean name is the first lowercase of the class name for this class, if you specify value "@Controller (value=" testaction ")" or "@ Controller ("Testaction"), use value as the name of the bean.
Using the @scope annotation, the @Scope ("prototype") means that the scope of the action is declared as a prototype, and the container's scope= "prototype" can be used to ensure that each request has a separate action to handle, Avoid thread-safety issues with action in struts. Spring default scope is a singleton mode (scope= "singleton"), so that only one action object is created, each access is the same action object, the data is unsafe, and the STRUTS2 requires a different action for each access. Scope= "Prototype" ensures that an action object is created when a request is made. ”
2, add struts.xml content, under <package>, add the following configuration
<!--configuration test action when integrated with spring, the name of the Class=bean (if the bean does not specify value, the first letter is lowercase))-- <action name= "test" class= "Testaction" > <result name= "Success" >/test.jsp</result> </action>
3. Configure the spring listener in Web. Xml.
<!--configuring spring's listener for initializing container-to-image <listener> <listener-class> org.springframework.web.context.contextloaderlistener</listener-class> </listener> < context-param> <param-name>contextConfigLocation</param-name> <param-value> Classpath:applicationcontext*.xml</param-value> </context-param>
4. Add an integrated jar package.
The integration of spring and STRUTS2 has been successful, requiring only two steps, one step is to add the spring listener, and the other is to add the integrated jar package.
It is important to note that the bean that corresponds to the presentation layer is labeled with the @controller annotation, which is the action, which can be managed by the spring container. Specific attention points above have been said, it is easy to complete the integration of the two.
Build an SSH environment to add the required jar package