26
2017
09

SSH框架整合的MAVEN项目之配置文件整合

在最近的SSH框架知识回顾之后,整合了一些所需的配置文件,并将配置文件中的配置项记录下来,避免在以后的开发中进行重复性编码

首先是web项目的核心配置文件 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">


  <welcome-file-list>
  <!--web项目欢迎页面的设置-->
    <welcome-file>index.jsp</welcome-file>
  </welcome-file-list>
  <filter>
    <filter-name>OpenSessionInViewFilter</filter-name>
    <filter-class>org.springframework.orm.hibernate4.support.OpenSessionInViewFilter</filter-class>
  </filter>
  <filter-mapping>
    <filter-name>OpenSessionInViewFilter</filter-name>
    <url-pattern>/*</url-pattern>
  </filter-mapping>

  <filter>
    <filter-name>struts2</filter-name>
    <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
    <init-param>
      <param-name>config</param-name>
      <!-- 改变struts配置文件的相对位置 -->
      <param-value>struts-default.xml,struts-plugin.xml,struts.xml</param-value>
    </init-param>
  </filter>
   <!-- 过滤后缀 -->
  <filter-mapping>
    <filter-name>struts2</filter-name>
    <url-pattern>*.action</url-pattern>
  </filter-mapping>

  <filter-mapping>
    <filter-name>struts2</filter-name>
    <url-pattern>*.jsp</url-pattern>
  </filter-mapping>
   <!--设置spring监听器-->
  <listener>
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
  </listener>
  <!--设置spring配置文件的位置-->
  <context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>classpath:beans.xml</param-value>
  </context-param>
</web-app>

然后是spring的配置文件bean.xml

<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-3.2.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.2.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.2.xsd ">
    <!-- 加载数据库连接配置文件 -->
    <context:property-placeholder  location="classpath:jdbc.properties" />

    <!--配置数据库链接 使用c3p0连接池 -->
    <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
        <property name="driverClass" value="${jdbc.driver}" />
        <property name="jdbcUrl" value="${jdbc.url}" />
        <property name="user" value="${jdbc.username}" />
        <property name="password" value="${jdbc.password}" />

    </bean>

    <!--配置sessionFactory -->

    <bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
        <property name="dataSource" ref="dataSource" />
        <property name="configLocation" value="classpath:hibernate.cfg.xml" /> <!-- 加载hibernate配置文件 -->
    </bean>

    <!--配置事物管理 -->
    <bean id="transactionManager" class="org.springframework.orm.hibernate4.HibernateTransactionManager">
        <property name="sessionFactory" ref="sessionFactory" />
</bean>
    <!-- 通知 -->
    <tx:advice id="advice" transaction-manager="transactionManager">
        <tx:attributes>
            <tx:method name="save*" propagation="REQUIRED" />
            <tx:method name="update*" propagation="REQUIRED" />
            <tx:method name="delete*" propagation="REQUIRED" />
            <tx:method name="get*" propagation="SUPPORTS" read-only="true"/>
            <tx:method name="*"/>
        </tx:attributes>
    </tx:advice>

    <!-- AOP切面 -->
    <!-- aop:pointcut要写在aop:advisor上面,否则会报错 -->
    <!-- 配置哪些包的类要切入事务 service 层切入事务 -->

    <aop:config>
        <aop:pointcut id="pointcut" expression="execution(* service.impl.*.*(..))" />
        <aop:advisor advice-ref="advice" pointcut-ref="pointcut" />
    </aop:config>

    <!-- 注解扫描 -->
    <context:component-scan base-package="entity,dao,dao.impl,service,service.impl,action,entity"/>
</beans>

hibernate配置文件hibernate.cfg.xml

<?xml version='1.0' encoding='UTF-8'?>
<!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD 3.0//EN" "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
<!-- Generated by MyEclipse Hibernate Tools. -->
<hibernate-configuration>

    <session-factory>

        <property name="myeclipse.connection.profile">mysql</property>
        <!--设置数据库方言类-->
        <property name="dialect">org.hibernate.dialect.MySQLDialect</property>
        <!--对sql语句格式化-->
        <property name="show_sql">false</property>
        <property name="format_sql">true</property>
        <!-- <property name="hibernate.event.merge.entity_copy_observer">allow</property> -->
        <!-- <property name="hbm2ddl.auto">update</property> -->
        <!-- <property name="current_session_context_class">org.springframework.orm.hibernate4.SpringSessionContext</property>-->
        <!--映射 实体类注册-->
        <mapping class="entity.User"/>
        <mapping class="entity.News"/>

    </session-factory>

</hibernate-configuration>

maven项目的核心配置 pom.xml

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">

  <modelVersion>4.0.0</modelVersion>
  <groupId>com.ruanku</groupId>
  <artifactId>newsManager</artifactId>
  <packaging>war</packaging>
  <version>1.0-SNAPSHOT</version>
  <name>newsManager Maven Webapp</name>
  <url>http://maven.apache.org</url>

  <properties>
    <!-- 文件拷贝编码 -->
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <!-- 输出编码 -->
    <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
    <!-- 编译编码 -->
    <maven.compiler.encoding>UTF-8</maven.compiler.encoding>
    <!-- junit -->
    <junit.version>4.12</junit.version>
    <!-- log4j -->
    <log4j.version>1.2.17</log4j.version>
    <!-- servlet-api -->
    <servlet-api.version>2.5</servlet-api.version>
    <!-- mysql驱动 -->
    <mysql.version>5.1.24</mysql.version>
    <!-- struts2 -->
    <struts.version>2.3.14</struts.version>
    <!-- spring -->
    <spring.version>4.3.7.RELEASE</spring.version>
    <!-- hibernate -->
    <hibernate.version>4.2.0.Final</hibernate.version>
  </properties>


  <dependencies>
    <!-- jUnit -->
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>${junit.version}</version>
      <scope>test</scope>
    </dependency>

    <!-- JSTL -->
    <dependency>
      <groupId>javax.servlet</groupId>
      <artifactId>jstl</artifactId>
      <version>1.2</version>
    </dependency>

    <!-- log4j 日志 -->
    <dependency>
      <groupId>log4j</groupId>
      <artifactId>log4j</artifactId>
      <version>${log4j.version}</version>
    </dependency>

    <!-- servlet API -->
    <dependency>
      <groupId>javax.servlet</groupId>
      <artifactId>servlet-api</artifactId>
      <version>${servlet-api.version}</version>
      <scope>provided</scope>
    </dependency>

    <!-- MySQL驱动 -->
    <dependency>
      <groupId>mysql</groupId>
      <artifactId>mysql-connector-java</artifactId>
      <version>${mysql.version}</version>
    </dependency>

    <!-- struts2 -->
    <dependency>
      <groupId>org.apache.struts</groupId>
      <artifactId>struts2-core</artifactId>
      <version>${struts.version}</version>
      <exclusions>
        <exclusion>
          <groupId>javassist</groupId>
          <artifactId>javassist</artifactId>
        </exclusion>
      </exclusions>
    </dependency>
    <dependency>
      <groupId>org.apache.struts</groupId>
      <artifactId>struts2-spring-plugin</artifactId>
      <version>${struts.version}</version>
    </dependency>
    <dependency>
      <groupId>org.apache.struts</groupId>
      <artifactId>struts2-convention-plugin</artifactId>
      <version>${struts.version}</version>
    </dependency>

    <!-- spring -->
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-core</artifactId>
      <version>${spring.version}</version>
    </dependency>
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-context</artifactId>
      <version>${spring.version}</version>
    </dependency>
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-context-support</artifactId>
      <version>${spring.version}</version>
    </dependency>
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-jdbc</artifactId>
      <version>${spring.version}</version>
    </dependency>
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-beans</artifactId>
      <version>${spring.version}</version>
    </dependency>
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-web</artifactId>
      <version>${spring.version}</version>
    </dependency>
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-expression</artifactId>
      <version>${spring.version}</version>
    </dependency>
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-orm</artifactId>
      <version>${spring.version}</version>
    </dependency>
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-test</artifactId>
      <version>${spring.version}</version>
      <scope>test</scope>
    </dependency>
    <!-- Spring AOP 切入点所需-->
    <dependency>
      <groupId>org.aspectj</groupId>
      <artifactId>aspectjweaver</artifactId>
      <version>1.8.10</version>
    </dependency>

    <!-- hibernate -->
    <dependency>
      <groupId>org.hibernate</groupId>
      <artifactId>hibernate-core</artifactId>
      <version>${hibernate.version}</version>
    </dependency>

    <!-- c3p0 -->
    <dependency>
      <groupId>c3p0</groupId>
      <artifactId>c3p0</artifactId>
      <version>0.9.1.2</version>
    </dependency>

    <!-- gson -->
    <dependency>
      <groupId>com.google.code.gson</groupId>
      <artifactId>gson</artifactId>
      <version>2.8.0</version>
    </dependency>

  </dependencies>


  <build>
    <finalName>newsManager</finalName>
    <plugins>
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-compiler-plugin</artifactId>
        <configuration>
          <source>1.6</source>
          <target>1.6</target>
        </configuration>
      </plugin>
    </plugins>
  </build>
</project>

struts配置文件struts.xml

<?xml version="1.0" encoding="UTF-8" ?>  
<!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.3//EN" "http://struts.apache.org/dtds/struts-2.3.dtd">  

<struts>  
    <!-- 设置默认编码 -->
    <constant name="struts.i18n.encoding" value="UTF-8"/>
    <!-- 上传的全部图片的最大限制-->
    <constant name="struts.multipart.maxSize" value="1024102400"/>


    <!--Action的配置-->
    <package name="user" extends="struts-default" namespace="/">
        <action name="register" class="userAction" method="register">
            <result name="success" type="stream">
                <param name="inputName">inputStream</param>
            </result>
            <result name="input">/login.jsp</result>
        </action>
    </package>


    <!--设置缓存文件的保存地址-->
    <constant name="struts.multipart.saveDir" value="d:/temp"></constant>
</struts>  

jdbc.properties

jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/ruanku?useUnicode=true&characterEncoding=UTF8
jdbc.username=root
jdbc.password=123456

至此就是一个完整的SSH整合项目中所用到的全部配置文件,而如果项目中有特别的需求时,可能还会需要一些额外的JAR包和一些相关的配置

上一篇:android 连按两次Back键退出 (单Activity多Fragment,保留根Fragment) 下一篇:Android基于高德地图实现搜索框的自动输入提示功能