Learn extjs5 with me (27 -- adding the Server web, spring, and hibernate configuration files ),
Learn extjs5 with me (27 -- adding the Server web, spring, and hibernate configuration files)
We created a java web project before. Now we need to add the jar packages connected by spring, hibernate, and sqlserver to the project and add the configuration files. I am using spring 3.1 and hibernate 3.0. For specific jar packages and some additional packages, please download them and add them to the project. (All jar packages are included in the demo software provided in my previous blog)
1. Add some packages to src of java Resources. Create the hibernate. cfg. xml and log4j. properties files in the src directory.
The configuration file of hibenate is as follows:/application5.01/src/hibernate. cfg. xml (the SQL server database is used, and other databases can also be used .)
<?xml version="1.0" encoding="UTF-8"?><!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
System log configuration file:/application5.01/src/log4j. properties.
log4j.rootCategory=error, consolelog4j.appender.console=org.apache.log4j.ConsoleAppenderlog4j.appender.console.layout=org.apache.log4j.PatternLayoutlog4j.appender.console.layout.ConversionPattern=%-4r [%t] %-5p %c %x - %m%nlog4j.logger.org.hibernate=errorlog4j.logger.org.hibernate.SQL=errorlog4j.logger.com.jfok=errorlog4j.logger.org.springframework=error#log4j.category.org.hibernate.type=debug
2. Modify the web. xml file and add spring settings. You need to modify web. xml in the WEB-INF directory and add the two spring configuration files applicationContext. xml and dispatcherServlet-servlet.xml.
Content of the web. xml file:
<? Xml version = "1.0" encoding = "UTF-8"?> <Web-app 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" version = "2.5" xmlns = "http://java.sun.com/xml/ns/javaee"> <! -- Spring listener --> <listener-class> org. springframework. web. context. ContextLoaderListener </listener-class> </listener> <! -- Spring configuration file address --> <context-param> <param-name> ContextConfigLocation </param-name> <param-value>/WEB-INF/applicationContext. xml </param-value> </context-param> <! -- Servlets --> <! -- Spring MVC. Its configuration file is <servlet-name>-servlet. xml, here is the "dispatcherServlet-servlet.xml" --> <servlet-name> dispatcherServlet </servlet-name> <servlet-class> org. springframework. web. servlet. dispatcherServlet </servlet-class> <load-on-startup> 1 </load-on-startup> </servlet> <servlet-mapping> <servlet-name> dispatcherServlet </ servlet-name> <url-pattern> *. do </url-pattern> <! -- Used to match/rest/module/remove. do/{id} and other calls --> <url-pattern>/rest/* </url-pattern> </servlet-mapping> <! -- Character set encoding filtering to prevent garbled characters --> <filter-name> encodingFilter </filter-name> <filter-class> org. springframework. web. filter. characterEncodingFilter </filter-class> <init-param> <param-name> encoding </param-name> <param-value> UTF-8 </param-value> </init- param> <init-param> <param-name> forceEncoding </param-name> <param-value> true </param-value> </init-param> </filter> <filter-mapping> <filter-name> encodingFilter </filter-nam E> <url-pattern>/* </url-pattern> </filter-mapping> <! -- Default page to serve 180 --> <welcome-file-list> <welcome-file>/index. jsp </welcome-file> </welcome-file-list> <session-config> <session-timeout> 180 </session-timeout> </session-config> <jsp -config> <jsp-property-group> <url-pattern> *. jsp </url-pattern> <trim-directive-whitespaces> true </trim-directive-whitespaces> </jsp-property-group> </jsp-config> </web -app>
Content of 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: p = "http://www.springframework.org/schema/p" xmlns: tx = "http://www.springframework.org/schema/tx" xmlns: context = "http://www.springframework.org/schema/context" xsi: schemaLocation = "http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xs Dhttp: // composer http://www.springframework.org/schema/context/spring-context-3.1.xsd "> <context: annotation-config/> <context: component-scan base-package =" com. jfok "> <! -- Exclude the control layer and only scan the DAO class of the service layer --> <context: exclude-filter type = "annotation" expression = "org. springframework. stereotype. controller "/> </context: component-scan> <tx: annotation-driven transaction-manager =" transactionManager "proxy-target-class =" true "/> <! -- Spring is used to replace hibernateSessionFactory. java sessionFactory --> <bean id = "sessionFactory" class = "org. springframework. orm. hibernate3.LocalSessionFactoryBean "> <property name =" configLocation "value =" classpath: hibernate. cfg. xml "> </property> </bean> <! -- Transaction Manager --> <bean id = "transactionManager" class = "org. springframework. orm. hibernate3.HibernateTransactionManager "> <property name =" sessionFactory "> <ref local =" sessionFactory "/> </property> </bean> </beans>
DispatcherServlet-servlet.xml content:
<? 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: p = "http://www.springframework.org/schema/p" xmlns: mvc = "http://www.springframework.org/schema/mvc" xmlns: context = "http://www.springframework.org/schema/context" xsi: schemaLocation = "http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1. Xsdhttp: // your http://www.springframework.org/schema/context/spring-context-3.1.xsd "> <! -- ①: Scans all classes in the web package to complete Bean creation and automatic dependency injection. Only the classes marked as @ Controller are divided, these classes are the control layer of mvc --> <context: component-scan base-package = "com. jfok "use-default-filters =" false "> <context: include-filter type =" annotation "expression =" org. springframework. stereotype. controller "/> </context: component-scan> <! -- ②: Start the annotation function of Spring MVC to map requests and annotation POJO --> <mvc: annotation-driven/> <beanclass = "org. springframework. web. servlet. mvc. annotation. defaultAnnotationHandlerMapping "/> <beanclass =" org. springframework. web. servlet. mvc. annotation. annotationMethodHandlerAdapter "/> <beanclass =" org. springframework. web. servlet. mvc. simpleControllerHandlerAdapter "> </bean> <! -- Configure the multipart resolver --> <bean id = "multipartResolver" class = "org. springframework. web. multipart. commons. CommonsMultipartResolver"> <! -- One of the properties available; the maximum file size in bytes --> <property name = "maxUploadSize" value = "10000000"/> </bean> </beans>
(Because the annotation of spring is used for development, you do not need to modify the configuration file later .)
Rename index.html to index. jsp.
4. Publish the project in tomcat. If no error message is displayed during running, all configurations and jar packages are correct.
5. Enter the URL http: // localhost: 8888/app/in the browser to display the previous page.
Now that the development environment is basically set up, we need to add functions below.
PS: Since I only use a simple configuration file for spring and spring mvc and use the annotation language for development, I will not solve this problem, if any problem occurs during the setup process, try to solve it on your own.