struts2.3.14+spring3.1.1+hibernate4.1.0框架搭建新版SSH(中)

        在上一篇文章中主要说了一下需要导入的jar包,此篇文章来讲解具体的配置。

三、具体的配置文件的配置

        1、首先配置一下web.xml。应该是个很主要的方面 

             a、欢迎界面的配置

    <!-- 配置默认欢迎界面 -->
    <welcome-file-list>
        <welcome-file>index.html</welcome-file>
    </welcome-file-list>
             b、log4j文件位置的配置,以前可能都没配置过这个东西,因为默认情况下log4j.properties文件需要放在src目录下,即所谓的classpath:目录。下面还有个配置具体的意思不是很懂,一下配置一下就行了。还有个spring对log4j的支持。具体的配置如下所示

    <context-param>
        <param-name>log4jConfigLocation</param-name>
        <param-value>classpath:/edu/qdlms/logic/resources/log4j.properties</param-value>
    </context-param>
        
    <context-param>
        <param-name>log4jRefreshInterval</param-name>
        <param-value>60000</param-value>
    </context-param>
    
    <!-- 为了防止出现log4j异常 -->
    <listener>  
        <listener-class>  
            org.springframework.web.util.Log4jConfigListener  
        </listener-class>  
    </listener>> 
              c、spring的配置,spring默认的文件路径是src目录下,看着有点不舒服,所以也把他放到resources包中。下面的listener必须配

    <!-- 配置spring的路径 -->
    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>classpath:/edu/qdlms/logic/resources/applicationContext-common.xml</param-value>
    </context-param>
    
    <listener>
      <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
  </listener>

           d、struts的配置

	<!-- struts 的配置 -->
	<filter>
		<filter-name>struts2</filter-name>
		<filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>

		<init-param>
			<param-name>config</param-name>
			<param-value>struts-default.xml,struts-plugin.xml,classpath:/edu/qdlms/logic/resources/struts.xml</param-value>
		</init-param>
	</filter>

	<filter-mapping>
		<filter-name>struts2</filter-name>
		<url-pattern>/*</url-pattern>
	</filter-mapping>

           e、其它错误文件的配置

	<error-page>  
        <error-code>404</error-code>  
        <location>/page/exception/404.jsp</location>  
    </error-page>  
    <error-page>  
        <error-code>500</error-code>  
        <location>/page/exception/500.jsp</location>  
    </error-page> 

          f、这里配置的比较少,下面把整个web.xml文件贴出来

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
	xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
	id="WebApp_ID" version="2.5">
	<display-name>QDLMS</display-name>

	<!-- 配置默认欢迎界面 -->
	<welcome-file-list>
		<welcome-file>index.html</welcome-file>
	</welcome-file-list>

	<context-param>
		<param-name>log4jConfigLocation</param-name>
		<param-value>classpath:/edu/qdlms/logic/resources/log4j.properties</param-value>
	</context-param>
		
	<context-param>
		<param-name>log4jRefreshInterval</param-name>
		<param-value>60000</param-value>
	</context-param>
	
	<!-- 为了防止出现log4j异常 -->
	<listener>  
        <listener-class>  
            org.springframework.web.util.Log4jConfigListener  
        </listener-class>  
    </listener> 

	<!-- 配置spring的路径 -->
	<context-param>
		<param-name>contextConfigLocation</param-name>
		<param-value>classpath:/edu/qdlms/logic/resources/applicationContext-common.xml</param-value>
	</context-param>
	
	<listener>
      <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
  </listener>

	<!-- struts 的配置 -->
	<filter>
		<filter-name>struts2</filter-name>
		<filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>

		<init-param>
			<param-name>config</param-name>
			<param-value>struts-default.xml,struts-plugin.xml,classpath:/edu/qdlms/logic/resources/struts.xml</param-value>
		</init-param>
	</filter>

	<filter-mapping>
		<filter-name>struts2</filter-name>
		<url-pattern>/*</url-pattern>
	</filter-mapping>
	<error-page>  
        <error-code>404</error-code>  
        <location>/page/exception/404.jsp</location>  
    </error-page>  
    <error-page>  
        <error-code>500</error-code>  
        <location>/page/exception/500.jsp</location>  
    </error-page> 
</web-app>

                2、log4j.properties文件的配置,下面直接贴出代码,具体不懂得参考百度或者谷歌吧。

### direct log messages to stdout ###
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.Target=System.out
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%d{ABSOLUTE} %5p %c:%L - %m%n

### direct messages to file hibernate.log ###
#log4j.appender.file=org.apache.log4j.FileAppender
#log4j.appender.file.File=hibernate.log
#log4j.appender.file.layout=org.apache.log4j.PatternLayout
#log4j.appender.file.layout.ConversionPattern=%d{ABSOLUTE} %5p %c{1}:%L - %m%n

### set log levels - for more verbose logging change 'info' to 'debug' ###

log4j.rootLogger=warn, stdout

#log4j.logger.org.hibernate=info
#log4j.logger.org.hibernate=debug

### log HQL query parser activity
#log4j.logger.org.hibernate.hql.ast.AST=debug

### log just the SQL
#log4j.logger.org.hibernate.SQL=debug

### log JDBC bind parameters ###
#log4j.logger.org.hibernate.type=info
#log4j.logger.org.hibernate.type=debug

### log schema export/update ###
log4j.logger.org.hibernate.tool.hbm2ddl=debug

### log HQL parse trees
#log4j.logger.org.hibernate.hql=debug

### log cache activity ###
#log4j.logger.org.hibernate.cache=debug

### log transaction activity
#log4j.logger.org.hibernate.transaction=debug

### log JDBC resource acquisition
#log4j.logger.org.hibernate.jdbc=debug

### enable the following line if you want to track down connection ###
### leakages when using DriverManagerConnectionProvider ###
#log4j.logger.org.hibernate.connection.DriverManagerConnectionProvider=trace

                3、jdbc.properties文件的配置

                  a、hibernate的配置

#Hibernate
hibernate.hbm2ddl.auto=update
hibernate.show_sql=true
hibernate.format_sql=true
hibernate.dialect=org.hibernate.dialect.MySQL5Dialect
hibernate.cache.provider_class=net.sf.ehcache.hibernate.EhCacheProvider
hibernate.current_session_context_class=org.springframework.orm.hibernate4.SpringSessionContext

                 b、数据库连接池(c3p0)的配置

#JDBC
jdbc.driverClass=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/qdlmsl
jdbc.username=root
jdbc.password=admin
#C3P0
#Hibernate4 C3P0
jdbc.miniPoolSize = 1
jdbc.maxPoolSize = 20
jdbc.initialPoolSize = 1
jdbc.maxIdleTime = 25000
jdbc.acquireIncrement = 1

jdbc.acquireRetryAttempts = 30
jdbc.acquireRetryDelay = 1000
jdbc.testConnectionOnCheckin = true
jdbc.automaticTestTable = c3p0TestTable
jdbc.idleConnectionTestPeriod = 18000
jdbc.checkoutTimeout=3000

                c、整个jdbc.properties文件就不贴出来了

            3、struts.xml文件就不用配置了,这里是用的时struts的零配置。所以大家就不用那么麻烦的去配置struts的配置文件了。其中我也在官方下载了一个原版的struts.xml的文件,这里没进行配置。struts的零配置并不是说一点都不需要配置,还有一点配置如下所示(全部注释掉了,回头如果有用到的地方,大家可以百度一下)

<?xml version="1.0" encoding="UTF-8" ?>
<!--
/*
 * $Id: struts-plugin.xml 722219 2008-12-01 20:41:26Z musachy $
 *
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance
 * with the License.  You may obtain a copy of the License at
 *
 *  http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing,
 * software distributed under the License is distributed on an
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 * KIND, either express or implied.  See the License for the
 * specific language governing permissions and limitations
 * under the License.
 */
-->

<!DOCTYPE struts PUBLIC
        "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
        "http://struts.apache.org/dtds/struts-2.0.dtd">

<struts>
 <!--    
    <constant name="struts.convention.action.suffix" value="Controller"/>
    <constant name="struts.convention.action.mapAllMatches" value="true"/>
    <constant name="struts.convention.default.parent.package" value="rest-default"/>

    <constant name="struts.convention.package.locators" value="action"/> -->
    
</struts>

                4、 下面最重要的配置就是下面applicationContext-common.xml的配置了,一不小心就会出很多的问题

               a、添加注解的支持,还有配置所需要扫描的文件包

	<!-- post-processors for all standard config annotations -->
	<context:annotation-config />

	<!-- Scans within the base package of the application for @Components to 
		configure as beans -->
	<context:component-scan base-package="edu.qdlms.logic.*" />
               b、jdbc.properties文件位置的配置

	<!--  jdbc.properties文件位置的配置 -->	
	<bean
		class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
		<property name="locations"
			value="classpath:/edu/qdlms/logic/resources/jdbc.properties" />
	</bean>
               c、c3p0连接池配置

	<!-- c3p0连接池配置 -->
	<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"
		destroy-method="close">
		<property name="driverClass" value="${jdbc.driverClass}" />
		<property name="jdbcUrl" value="${jdbc.url}" />
		<property name="user" value="${jdbc.username}" />
		<property name="password" value="${jdbc.password}" />
		<property name="minPoolSize" value="${jdbc.miniPoolSize}" />
		<property name="maxPoolSize" value="${jdbc.maxPoolSize}" />
		<property name="initialPoolSize" value="${jdbc.initialPoolSize}" />
		<property name="maxIdleTime" value="${jdbc.maxIdleTime}" />
		<property name="acquireIncrement" value="${jdbc.acquireIncrement}" />

		<property name="acquireRetryAttempts" value="${jdbc.acquireRetryAttempts}" />
		<property name="acquireRetryDelay" value="${jdbc.acquireRetryDelay}" />
		<property name="testConnectionOnCheckin" value="${jdbc.testConnectionOnCheckin}" />
		<property name="automaticTestTable" value="${jdbc.automaticTestTable}" />
		<property name="idleConnectionTestPeriod" value="${jdbc.idleConnectionTestPeriod}" />
		<property name="checkoutTimeout" value="${jdbc.checkoutTimeout}" />
	</bean>
               d、配置SessionFactory

	<!-- 配置SessionFactory,据说这个packagesToScan不能扫描子包 -->
	<bean id="sessionFactory"
		class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
		<property name="dataSource" ref="dataSource" />
		<property name="packagesToScan">
			<list>
				<value>edu.qdlms.logic.model</value>
			</list>
		</property>
		<property name="hibernateProperties">
			<props>
				<prop key="hibernate.dialect">${hibernate.dialect}</prop>
				<prop key="hibernate.current_session_context_class">${hibernate.current_session_context_class}</prop>
				<prop key="hibernate.show_sql">${hibernate.show_sql}</prop>
				<prop key="hibernate.format_sql">${hibernate.format_sql}</prop>
				<prop key="hibernate.hbm2ddl.auto">${hibernate.hbm2ddl.auto}</prop>
			</props>
		</property>
	</bean>
               e、transactionManager的配置

    <!-- transactionManager -->
	<bean id="transactionManager"
		class="org.springframework.orm.hibernate4.HibernateTransactionManager">
		<property name="sessionFactory">
			<ref local="sessionFactory" />
		</property>
	</bean>

	<!--采用注解来管理Bean -->
	<tx:annotation-driven transaction-manager="transactionManager" />


	<bean id="txManager" class="org.springframework.orm.hibernate4.HibernateTransactionManager">
        <property name="sessionFactory" ref="sessionFactory"/>
    </bean>

    <tx:advice id="txAdvice" transaction-manager="txManager">
        <tx:attributes>
            <tx:method name="save*" propagation="REQUIRED" />
            <tx:method name="add*" propagation="REQUIRED" />
            <tx:method name="create*" propagation="REQUIRED" />
            <tx:method name="insert*" propagation="REQUIRED" />
            <tx:method name="update*" propagation="REQUIRED" />
            <tx:method name="merge*" propagation="REQUIRED" />
            <tx:method name="del*" propagation="REQUIRED" />
            <tx:method name="remove*" propagation="REQUIRED" />
            <tx:method name="put*" propagation="REQUIRED" />
            <tx:method name="use*" propagation="REQUIRED"/>
            <!--hibernate4必须配置为开启事务 否则 getCurrentSession()获取不到-->
            <tx:method name="get*" propagation="REQUIRED" read-only="true" />
            <tx:method name="count*" propagation="REQUIRED" read-only="true" />
            <tx:method name="find*" propagation="REQUIRED" read-only="true" />
            <tx:method name="list*" propagation="REQUIRED" read-only="true" />
            <tx:method name="*" read-only="true" />
        </tx:attributes>
    </tx:advice>
    <aop:config expose-proxy="true">
        <!-- 只对业务逻辑层实施事务 -->
        <aop:pointcut id="txPointcut" expression="execution(* edu.qdlms.logic..service..*.*(..))" />
        <aop:advisor advice-ref="txAdvice" pointcut-ref="txPointcut"/>
    </aop:config> 	 	

               f、最后把整个applicationContext-common.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:jee="http://www.springframework.org/schema/jee"
	xmlns:aop="http://www.springframework.org/schema/aop" xmlns:util="http://www.springframework.org/schema/util"
	xmlns:p="http://www.springframework.org/schema/p" xmlns:tx="http://www.springframework.org/schema/tx"
	xmlns:tool="http://www.springframework.org/schema/tool" 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.0.xsd
			http://www.springframework.org/schema/tx
			http://www.springframework.org/schema/tx/spring-tx.xsd
			http://www.springframework.org/schema/aop
			http://www.springframework.org/schema/aop/spring-aop.xsd
			http://www.springframework.org/schema/context
			http://www.springframework.org/schema/context/spring-context.xsd
           http://www.springframework.org/schema/jee
           http://www.springframework.org/schema/jee/spring-jee.xsd
           http://www.springframework.org/schema/util
           http://www.springframework.org/schema/util/spring-util.xsd
           http://www.springframework.org/schema/tool
           http://www.springframework.org/schema/tool/spring-tool.xsd"
	default-lazy-init="true" default-autowire="byName">

	<!-- post-processors for all standard config annotations -->
	<context:annotation-config />

	<!-- Scans within the base package of the application for @Components to 
		configure as beans -->
	<context:component-scan base-package="edu.qdlms.logic.*" />


	<!-- 配置数据库的信息 -->
	<!--  jdbc.properties文件位置的配置 -->	
	<bean
		class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
		<property name="locations"
			value="classpath:/edu/qdlms/logic/resources/jdbc.properties" />
	</bean>

	<!-- c3p0连接池配置 -->
	<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"
		destroy-method="close">
		<property name="driverClass" value="${jdbc.driverClass}" />
		<property name="jdbcUrl" value="${jdbc.url}" />
		<property name="user" value="${jdbc.username}" />
		<property name="password" value="${jdbc.password}" />
		<property name="minPoolSize" value="${jdbc.miniPoolSize}" />
		<property name="maxPoolSize" value="${jdbc.maxPoolSize}" />
		<property name="initialPoolSize" value="${jdbc.initialPoolSize}" />
		<property name="maxIdleTime" value="${jdbc.maxIdleTime}" />
		<property name="acquireIncrement" value="${jdbc.acquireIncrement}" />

		<property name="acquireRetryAttempts" value="${jdbc.acquireRetryAttempts}" />
		<property name="acquireRetryDelay" value="${jdbc.acquireRetryDelay}" />
		<property name="testConnectionOnCheckin" value="${jdbc.testConnectionOnCheckin}" />
		<property name="automaticTestTable" value="${jdbc.automaticTestTable}" />
		<property name="idleConnectionTestPeriod" value="${jdbc.idleConnectionTestPeriod}" />
		<property name="checkoutTimeout" value="${jdbc.checkoutTimeout}" />
	</bean>


	<!-- 配置SessionFactory,据说这个packagesToScan不能扫描子包 -->
	<bean id="sessionFactory"
		class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
		<property name="dataSource" ref="dataSource" />
		<property name="packagesToScan">
			<list>
				<value>edu.qdlms.logic.model</value>
			</list>
		</property>
		<property name="hibernateProperties">
			<props>
				<prop key="hibernate.dialect">${hibernate.dialect}</prop>
				<prop key="hibernate.current_session_context_class">${hibernate.current_session_context_class}</prop>
				<prop key="hibernate.show_sql">${hibernate.show_sql}</prop>
				<prop key="hibernate.format_sql">${hibernate.format_sql}</prop>
				<prop key="hibernate.hbm2ddl.auto">${hibernate.hbm2ddl.auto}</prop>
			</props>
		</property>
	</bean>

	<!-- transactionManager -->
	<bean id="transactionManager"
		class="org.springframework.orm.hibernate4.HibernateTransactionManager">
		<property name="sessionFactory">
			<ref local="sessionFactory" />
		</property>
	</bean>

	<!--采用注解来管理Bean -->
	<tx:annotation-driven transaction-manager="transactionManager" />


	<bean id="txManager" class="org.springframework.orm.hibernate4.HibernateTransactionManager">
        <property name="sessionFactory" ref="sessionFactory"/>
    </bean>

    <tx:advice id="txAdvice" transaction-manager="txManager">
        <tx:attributes>
            <tx:method name="save*" propagation="REQUIRED" />
            <tx:method name="add*" propagation="REQUIRED" />
            <tx:method name="create*" propagation="REQUIRED" />
            <tx:method name="insert*" propagation="REQUIRED" />
            <tx:method name="update*" propagation="REQUIRED" />
            <tx:method name="merge*" propagation="REQUIRED" />
            <tx:method name="del*" propagation="REQUIRED" />
            <tx:method name="remove*" propagation="REQUIRED" />
            <tx:method name="put*" propagation="REQUIRED" />
            <tx:method name="use*" propagation="REQUIRED"/>
            <!--hibernate4必须配置为开启事务 否则 getCurrentSession()获取不到-->
            <tx:method name="get*" propagation="REQUIRED" read-only="true" />
            <tx:method name="count*" propagation="REQUIRED" read-only="true" />
            <tx:method name="find*" propagation="REQUIRED" read-only="true" />
            <tx:method name="list*" propagation="REQUIRED" read-only="true" />
            <tx:method name="*" read-only="true" />
        </tx:attributes>
    </tx:advice>
    <aop:config expose-proxy="true">
        <!-- 只对业务逻辑层实施事务 -->
        <aop:pointcut id="txPointcut" expression="execution(* edu.qdlms.logic..service..*.*(..))" />
        <aop:advisor advice-ref="txAdvice" pointcut-ref="txPointcut"/>
    </aop:config> 	 	
</beans>







深度学习是机器学习的一个子领域,它基于人工神经网络的研究,特别是利用多层次的神经网络来进行学习和模式识别。深度学习模型能够学习数据的高层次特征,这些特征对于图像和语音识别、自然语言处理、医学图像分析等应用至关重要。以下是深度学习的一些关键概念和组成部分: 1. **神经网络(Neural Networks)**:深度学习的基础是人工神经网络,它是由多个层组成的网络结构,包括输入层、隐藏层和输出层。每个层由多个神经元组成,神经元之间通过权重连接。 2. **前馈神经网络(Feedforward Neural Networks)**:这是最常见的神经网络类型,信息从输入层流向隐藏层,最终到达输出层。 3. **卷积神经网络(Convolutional Neural Networks, CNNs)**:这种网络特别适合处理具有网格结构的数据,如图像。它们使用卷积层来提取图像的特征。 4. **循环神经网络(Recurrent Neural Networks, RNNs)**:这种网络能够处理序列数据,如时间序列或自然语言,因为它们具有记忆功能,能够捕捉数据的时间依赖性。 5. **长短期记忆网络(Long Short-Term Memory, LSTM)**:LSTM 是一种特殊的 RNN,它能够学习长期依赖关系,非常适合复杂的序列预测任务。 6. **生成对抗网络(Generative Adversarial Networks, GANs)**:由两个网络组成,一个生成器和一个判别器,它们相互竞争,生成器生成数据,判别器评估数据的真实性。 7. **深度学习框架**:如 TensorFlow、Keras、PyTorch 等,这些框架提供了构建、训练和部署深度学习模型的工具和库。 8. **激活函数(Activation Functions)**:如 ReLU、Sigmoid、Tanh 等,它们在神经网络用于添加非线性,使得网络能够学习复杂的函数。 9. **损失函数(Loss Functions)**:用于评估模型的预测与真实值之间的差异,常见的损失函数包括均方误差(MSE)、交叉熵(Cross-Entropy)等。 10. **优化算法(Optimization Algorithms)**:如梯度下降(Gradient Descent)、随机梯度下降(SGD)、Adam 等,用于更新网络权重,以最小化损失函数。 11. **正则化(Regularization)**:技术如 Dropout、L1/L2 正则化等,用于防止模型过拟合。 12. **迁移学习(Transfer Learning)**:利用在一个任务上训练好的模型来提高另一个相关任务的性能。 深度学习在许多领域都取得了显著的成就,但它也面临着一些挑战,如对大量数据的依赖、模型的解释性差、计算资源消耗大等。研究人员正在不断探索新的方法来解决这些问题。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值