跟我一起学extjs5(27--服务端web,spring,hibernate配置文件的加入)
我们前面创建项目的时候是一个java web project,现在在项目中需要加入spring,hibernate,sqlserver连接的jar包以及加入配置文件。spring我现在使用的是3.1版本,hibernate使用的是3.0,具体的jar包以及一些附加包,请自行下载后加到工程中。(所有的jar包在我的前一个博客中提供的演示软件中有)
一、在java Resources的src中加入一些package。在src目录下建立文件hibernate.cfg.xml和log4j.properties。
hibenate的配置文件如下:/application5.01/src/hibernate.cfg.xml
(使用的是sql server数据库,用其他数据库也可以。)
<?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">
<hibernate-configuration>
<session-factory>
<property name="hibernate.connection.driver_class">com.microsoft.sqlserver.jdbc.SQLServerDriver</property>
<property name="hibernate.connection.url">jdbc:sqlserver://10.211.55.5:1433;databaseName=haiyu</property>
<property name="hibernate.dialect">org.hibernate.dialect.SQLServerDialect</property>
<property name="hibernate.connection.username">sa</property>
<property name="hibernate.connection.password"></property>
<property name="hibernate.connection.autocommit">false</property>
<property name="format_sql">true</property>
<property name="show_sql">true</property>
</session-factory>
</hibernate-configuration>
系统日志配置文件:/application5.01/src/log4j.properties 。
log4j.rootCategory=error, console
log4j.appender.console=org.apache.log4j.ConsoleAppender
log4j.appender.console.layout=org.apache.log4j.PatternLayout
log4j.appender.console.layout.ConversionPattern=%-4r [%t] %-5p %c %x - %m%n
log4j.logger.org.hibernate=error
log4j.logger.org.hibernate.SQL=error
log4j.logger.com.jfok=error
log4j.logger.org.springframework=error
#log4j.category.org.hibernate.type=debug
二、修改web.xml文件,加入spring的设置。
在WEB-INF目录下需要修改web.xml ,并加入二个spring 的配置文件applicationContext.xml和dispatcherServlet-servlet.xml。
web.xml的文件内容:
<?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 配置文件地址 -->
<context-param>
<param-name>ContextConfigLocation</param-name>
<param-value>
/WEB-INF/applicationContext.xml
</param-value>
</context-param>
<!-- Servlets -->
<!-- spring MVC,其配置文件为 <servlet-name>-servlet.xml,这里为“dispatcherServlet-servlet.xml” -->
<servlet>
<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>
<!-- 用于匹配/rest/module/remove.do/{id} 之类的调用 -->
<url-pattern>/rest/*</url-pattern>
</servlet-mapping>
<!-- 字符集编码过滤,防止某些乱码 -->
<filter>
<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-name>
<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>
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.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.1.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.1.xsd">
<context:annotation-config />
<context:component-scan base-package="com.jfok">
<!-- 将控制层排除在外,只扫描生成服务层的DAO层的类 -->
<context:exclude-filter type="annotation"
expression="org.springframework.stereotype.Controller" />
</context:component-scan>
<tx:annotation-driven transaction-manager="transactionManager" proxy-target-class="true"/>
<!-- spring用于替换hibernateSessionFactory.java的sessionFactory -->
<bean id="sessionFactory"
class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="configLocation" value="classpath:hibernate.cfg.xml">
</property>
</bean>
<!-- 事务管理器 -->
<bean id="transactionManager"
class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory">
<ref local="sessionFactory" />
</property>
</bean>
</beans>
dispatcherServlet-servlet.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: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.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-3.1.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.1.xsd
">
<!-- ①:对web包中的所有类进行扫描,以完成Bean创建和自动依赖注入的功能 , 只对标注为@Controller 的类进行分成,这些类是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>
<!-- ②:启动Spring MVC的注解功能,完成请求和注解POJO的映射 -->
<mvc:annotation-driven />
<bean
class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping" />
<bean
class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter" />
<bean
class="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>
(由于使用的spring的annotation注解进行开发,因此配置文件在以后开发过程中基本上不用修改。)
三、将index.html改名为index.jsp。
四、在tomcat中发布此工程。如果运行时没有出错信息,则说明所有的配置和jar包都正确了。
五、在浏览器中输入网址:http://localhost:8888/app/,应该显示原来做过的页面。
至此开发环境基本搭建完成,下面就是要加入一个个功能了。
PS:由于我对于spring和spring mvc 也只是用做一个简单的配置文件并且用标注语言来进行开发,对于这方面比较深的问题我也不会,如果搭建过程中有问题,请尽量自行解决。