Build an SSH framework
I. concepts of the SSH framework
The SSH framework is an open-source collection framework popular in java web development. The SSH framework includes the Struts, Spring, and Hibernate frameworks. The SSH framework can be used to build flexible and scalable multi-layer Web applications.
Ii. Working Principles of the SSH framework
Struts in the SSH framework intercepts user requests. Normally, when a user request arrives at the Struts framework, the core interceptor of the Struts framework intercepts the request. The Struts framework creates an action instance based on the request. In the SSH framework, the Spring container manages Action instances. When a user request arrives at an SSH framework application, the core controller of the Struts framework intercepts the user request and forwards the request to the pseudo Action of the Spring plug-in provided in the Struts framework, then, the pseudo Action obtains the corresponding request Action instance in the Spring container, connects to the Hibernate framework through the relevant business components in the Spring container, and uses the Hibernate framework to manage the database.
Working principle of the SSH framework
Iii. setup of the SSH framework
1. Create a web Project
2. the basic jar file package required to import Struts2 must contain the jar package of the struts2-spring-plugin, which is responsible for managing the pseudo controller and then in the web. configure the struts core interceptor in xml. add the following configuration code to xml:
<! -- Configure the core filter of Struts2 --> <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. Import the jar package required for Spring development: spring-beans.jar, spring-context.jar, spring-core.jar, spring-expression.jar, commons-logging.jar, spring-web.jar
Add the following code to web. xml to configure the Spring container startup listener.
<! -- Configure the Spring listener --> <listener-class> org. springframework. web. context. ContextLoaderListener </listener-class> </listener> <! -- If the following items are not configured, applicationConext under web-inf is loaded by default. xml --> <context-param> <param-name> contextConfigLocation </param-name> <param-value> classpath: applicationContext. xml </param-value> </context-param>
4. Import the Hibernate Development Kit, database driver package, connection pool (c3p0) package, create the jdbc. properties file and applicationContext. xml file in src, and add the following code for configuration.
Jdbc. properties:
jdbc.driver = com.mysql.jdbc.Driverjdbc.url = jdbc:mysql:///shopjdbc.user = rootjdbc.password =123456
ApplicationContext. xml:
<? 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. Xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd "> <! -- Configure the connection pool --> <context: property-placeholder location = "classpath: jdbc. properties "/> <bean id =" dataSource "class =" com. mchange. v2.c3p0. comboPooledDataSource "> <property name =" driverClass "value =" $ {jdbc. driver} "> </property> <property name =" jdbcUrl "value =" $ {jdbc. url} "> </property> <property name =" user "value =" $ {jdbc. user} "> </property> <property name =" password "value =" $ {jdbc. password} "> </property> </bean> <! -- Hibernate-related configuration --> <bean id = "sessionFactory" class = "org. springframework. orm. hibernate3.LocalSessionFactoryBean "> <property name =" dataSource "ref =" dataSource "> </property> <property name =" hibernateProperties "> <props> <prop key =" hibernate. dialect "> org. hibernate. dialect. mySQLDialect </prop> <prop key = "hibernate. show_ SQL "> true </prop> <prop key =" hibernate. format_ SQL "> true </prop> <prop key =" hibernate. con Nection. autocommit "> false </prop> <prop key =" hibernate. hbm2ddl. auto "> update </prop> </props> </property> <! -- Hibernate ing file --> </bean> </beans>
The web. xml configuration after building 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"> <display-name> </display-name> <! -- Struts2 interceptor configuration --> <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> <! -- Spring container start listener Configuration --> <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> </web-app>View Code
Now that the SSH framework has been set up, you can use the SSH framework for related development.