跟我一起學extjs5(27--服務端web,spring,hibernate設定檔的加入),

來源:互聯網
上載者:User

跟我一起學extjs5(27--服務端web,spring,hibernate設定檔的加入),
跟我一起學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, 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

二、修改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.xsdhttp://www.springframework.org/schema/txhttp://www.springframework.org/schema/tx/spring-tx-3.1.xsdhttp://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.xsdhttp://www.springframework.org/schema/mvchttp://www.springframework.org/schema/mvc/spring-mvc-3.1.xsdhttp://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 /><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>
        (由於使用的spring的annotation註解進行開發,因此設定檔在以後開發過程中基本上不用修改。)
三、將index.html改名為index.jsp。
四、在tomcat中發布此工程。如果運行時沒有出錯資訊,則說明所有的配置和jar包都正確了。
五、在瀏覽器中輸入網址:http://localhost:8888/app/,應該顯示原來做過的頁面。

        至此開發環境基本搭建完成,下面就是要加入一個個功能了。
        PS:由於我對於spring和spring mvc 也只是用做一個簡單的設定檔並且用標註語言來進行開發,對於這方面比較深的問題我也不會,如果搭建過程中有問題,請盡量自行解決。




聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.