maven3.3搭建spring+hibernate+struts2的web项目

我对maven的理解:

网上是这样解释的:Maven  是一个项目管理和构建自动化工具。

举个例子:

在不使用maven的时候,我要创建一个web项目,那我这个项目需要某些jar包(比如struts2的框架包),首先我要到struts2的官网去下载struts2的jar包然后把它们丢进我的项目的lib里面。还有可能我的项目需要合作,我把项目给其他人时,也不能保证每个人使用的IDE的版本和它们的项目结构都保持一致(我觉得IDE本身也很复杂,里面也有各种配置,稍稍不同可能也会有问题)

但是在我使用了maven后,maven为我构建一个很规范的项目结构,我也不需要到开源软件的官网去下载项目的jar包,我只需要知道我的项目需要使用什么jar包(jar包也是一个个的项目,maven会帮我们管理这些项目的依赖),然后维护一个名字是pom.xml的文件(maven的知识网上很多,pom.xml的作用就不多讲了),

我对maven也并非特别熟悉,只会使用他来构建项目,我他的具体细节尚未了解,毕竟这是apache一个很伟大的项目,希望日后我能慢慢了解更多maven的奥秘。。。


首先我要在myeclipse中创建一个WEB项目,就像创建一般的WEB项目一样,知识在创建的时候选择add maven support:

maven3.3搭建spring+hibernate+struts2的web项目_第1张图片


一路next到这里选择standard maven jee project struture,就是选择项目是一个标准的maven项目:

maven3.3搭建spring+hibernate+struts2的web项目_第2张图片


完成后maven的项目结构就是这样:这里说明一下,src/main/java是放java代码的,src/main/resource是放资源文件的(比如spring的配置文件等等),下面的test是模块化测试的代码和资源文件

maven3.3搭建spring+hibernate+struts2的web项目_第3张图片


然后我要开始搭建spring+struts2+hibernate了,把jar包添加进来,在pom.xml文件里面写就ok

<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/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>MavenWebApp</groupId>
    <artifactId>MavenWebApp</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>war</packaging>
    <name>MavenWebApp</name>
    <description />
    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    </properties>
    <dependencies>

        <!-- junut4 模块化测试 -->
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.3.1</version>
            <scope>test</scope>
        </dependency>

        <!-- struts2核心 -->
        <dependency>
            <groupId>org.apache.struts</groupId>
            <artifactId>struts2-core</artifactId>
            <version>2.3.20</version>
        </dependency>

        <!-- convention-plugin插件,使用了这个插件之后,就可以采用注解的方式配置Action -->
        <dependency>
            <groupId>org.apache.struts</groupId>
            <artifactId>struts2-convention-plugin</artifactId>
            <version>2.3.20</version>
        </dependency>

        <!--config-browser-plugin插件,使用了这个插件之后,就可以很方便的浏览项目中的所有action及其与 jsp view的映射 -->
        <dependency>
            <groupId>org.apache.struts</groupId>
            <artifactId>struts2-config-browser-plugin</artifactId>
            <version>2.3.20</version>
        </dependency>

        <!-- Struts2和Spring整合插件 -->
        <dependency>
            <groupId>org.apache.struts</groupId>
            <artifactId>struts2-spring-plugin</artifactId>
            <version>2.3.4.1</version>
        </dependency>

        <!-- struts2 Ajax 获取json数据 -->
        <dependency>
            <groupId>org.apache.struts</groupId>
            <artifactId>struts2-json-plugin</artifactId>
            <version>2.3.20</version>
        </dependency>

        <!-- Hibernate核心包 -->
        <dependency>
            <groupId>org.hibernate</groupId>
            <artifactId>hibernate-core</artifactId>
            <version>4.3.0.Final</version>
        </dependency>

        <!-- 扩展Java类与实现Java接口 -->
        <dependency>
            <groupId>cglib</groupId>
            <artifactId>cglib</artifactId>
            <version>2.2</version>
        </dependency>

        <!-- 运用Log4j必须用到这个包 -->
        <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>slf4j-api</artifactId>
            <version>1.7.5</version>
            <scope>compile</scope>
        </dependency>

        <!-- spring4 -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-core</artifactId>
            <version>4.1.6.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version>4.1.6.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-jdbc</artifactId>
            <version>4.1.6.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-beans</artifactId>
            <version>4.1.6.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-web</artifactId>
            <version>4.1.6.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-expression</artifactId>
            <version>4.1.6.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-orm</artifactId>
            <version>4.1.6.RELEASE</version>
        </dependency>

        <!-- spring3可选的依赖注入,不可缺少 -->
        <dependency>
            <groupId>org.aspectj</groupId>
            <artifactId>aspectjweaver</artifactId>
            <version>1.8.0</version>
        </dependency>

        <!--aspectjweaver包,spring AOP 需要 -->
        <dependency>
            <groupId>org.aspectj</groupId>
            <artifactId>aspectjweaver</artifactId>
            <version>1.8.5</version>
        </dependency>

        <!-- log4j日志包 -->
        <dependency>
            <groupId>log4j</groupId>
            <artifactId>log4j</artifactId>
            <version>1.2.16</version>
            <scope>compile</scope>
        </dependency>

        <!-- jsp接口 -->
        <dependency>
            <groupId>javax.servlet.jsp</groupId>
            <artifactId>jsp-api</artifactId>
            <version>2.1</version>
            <scope>provided</scope>
        </dependency>



        <!-- servlet接口 -->
        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>servlet-api</artifactId>
            <version>2.5</version>
            <scope>provided</scope>
        </dependency>

        <!-- Mysql数据库JDBC连接包 -->
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>5.1.26</version>
        </dependency>

    </dependencies>

    <build>
        <finalName>MavenWebApp</finalName>

        <!-- 使用 Maven 部署 Java Web 项目时,Hibernate 的映射文件 *.hbm.xml 没有被打包部署到目标目录下 -->
        <resources>
            <resource>
                <directory>src/main/java</directory>
                <includes>
                    <include>**/*.hbm.xml</include>
                </includes>
            </resource>
            <!--  
            <resource>
                <directory>src/main/resources</directory>
                <includes>
                    <include>**/*.xml</include>
                    <include>**/*.properties</include>
                </includes>
            </resource>
            -->
        </resources>
    </build>
</project>

在上面的xml中,pom.xml是描述我的项目需要依赖哪些其他项目,也就是需要哪些jar包(pom.xml的作用不止这点),当然我们不需要记住这些jar包的具体坐标,直接到maven的中央仓库中去寻找:http://mvnrepository.com/


接下来就像不使用maven前一样搭建项目了:

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"
    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
    id="WebApp_ID" version="3.0">
    <display-name>MavenWebApp</display-name>
    <welcome-file-list>
        <welcome-file>index.html</welcome-file>
        <welcome-file>index.htm</welcome-file>
        <welcome-file>index.jsp</welcome-file>
        <welcome-file>default.html</welcome-file>
        <welcome-file>default.htm</welcome-file>
        <welcome-file>default.jsp</welcome-file>
    </welcome-file-list>

    <!-- Spring监听器 -->
    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>
    <!-- 框架配置文件位置 -->
    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>classpath*:applicationContext.xml</param-value>
    </context-param>
    
    <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>
    
    <!-- Struts2过滤器 -->
    <filter>
        <filter-name>struts2</filter-name>
        <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
    </filter>
    <filter-mapping>
        <filter-name>struts2</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>
</web-app>


spring的配置文件:

<?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:aop="http://www.springframework.org/schema/aop"
    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.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-3.0.xsd">

    <!--JDBC DataSource -->
    <bean id="dataSource"
        class="org.springframework.jdbc.datasource.DriverManagerDataSource">
        <property name="driverClassName" value="com.mysql.jdbc.Driver" />
        <property name="url" value="jdbc:mysql://localhost:3306/hibernate" />
        <property name="username" value="root" />
        <property name="password" value="root" />
    </bean>

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

    <!-- hibernate 在整合了 spring 后,hibernate 的事务将交由 spring 管理 -->
    <!-- 事务管理器 -->
    <bean id="transactionManager"
        class="org.springframework.orm.hibernate4.HibernateTransactionManager">
        <property name="sessionFactory" ref="sessionFactory"></property>
    </bean>

    <!-- 事务传播行为 -->
    <tx:advice id="txAdvice" transaction-manager="transactionManager">
        <tx:attributes>
            <tx:method name="add*" />
            <tx:method name="delete*" />
            <tx:method name="update*" />
            <tx:method name="get*" />
            <tx:method name="*" rollback-for="Exception" propagation="REQUIRED"></tx:method>
        </tx:attributes>
    </tx:advice>

    <!-- aop 配置 -->
    <aop:config>
        <aop:pointcut id="pointcut"
            expression="execution(* com.genericdao.service.*.*(..))" />
        <aop:advisor advice-ref="txAdvice" pointcut-ref="pointcut" />
    </aop:config>

    <!-- dao -->
    <!-- base dao -->
    
    <bean name="baseDao" class="com.genericdao.dao.BaseDaoImpl">
        <property name="sessionFactory" ref="sessionFactory"></property>
    </bean>
    

    <!-- user dao -->
    
    <bean name="userDaoImpl" class="com.genericdao.dao.UserDaoImpl" />
    

    <!-- service -->
    <!-- base service -->
    
    <bean name="baseServiceImpl" class="com.genericdao.service.BaseServiceImpl"
        abstract="true">
        <property name="baseDao" ref="baseDao"></property>
    </bean>
    
    

    <!-- user service -->
    
    <bean name="userServiceImpl" class="com.genericdao.service.UserServiceImpl"
        parent="baseServiceImpl">
        <property name="userDao" ref="userDaoImpl"></property>
    </bean>
    

    <!-- action -->
    
    <bean name="testAction" class="com.genericdao.action.TestAction">
        <property name="userService" ref="userServiceImpl"></property>
    </bean>
    

    <!-- pojo -->
    <!--  
    <bean name="user" class="com.genericdao.pojo.User" />
    -->

</beans>


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">
     
<hibernate-configuration>
    <session-factory>
     
    <!-- Database connection settings -->
    <!-- 此处 dataSource 交由 spring 容器管理
    <property name="connection.driver_class">com.mysql.jdbc.Driver</property>
    <property name="connection.url">jdbc:mysql://localhost/hibernate</property>
    <property name="connection.username">root</property>
    <property name="connection.password">root</property>
    -->
         
    <!-- JDBC connection pool (use the built-in) -->
    <!-- <property name="connection.pool_size">1</property> -->
         
    <!-- SQL dialect :数据库方言-->
    <property name="dialect">org.hibernate.dialect.MySQLDialect</property>
         
    <!-- Echo all executed SQL to stdout -->
    <property name="show_sql">true</property>
    <!-- 格式化SQL语句 -->
    <property name="format_sql">false</property>
         
    <!-- Enable Hibernate's automatic session context management -->
    <!--<property name="current_session_context_class">thread</property>-->
         
    <!-- Drop and re-create the database schema on startup -->
    <!-- <property name="hbm2ddl.auto">create</property> -->
         
    <!-- Disable the second-level cache -->
    <property name="cache.provider_class">org.hibernate.cache.NoCacheProvider</property>
     
     <property name="hibernate.hbm2ddl.auto">update</property>
     
     <!-- 在 web 开发中,使用线程安全的 session,这个在 hibernate 整合 spring 是要使用 -->
    
 <property 
name="hibernate.current_session_context_class">org.springframework.orm.hibernate4.SpringSessionContext</property>

     <!-- 配置实体类映射文件,该文件一定要和对应类的路径一致 -->
    <mapping resource="com/genericdao/pojo/User.hbm.xml"/>
    </session-factory>
</hibernate-configuration>


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.enable.DynamicMethodInvocation" value="false" />
    <constant name="struts.devMode" value="true" />
    
    <package name="default" namespace="/" extends="struts-default">
    
        <action name="test" class="testAction">
            <result>/content/test.jsp</result>
        </action>
            
    </package>


</struts>


这里要注意的是,maven的src/main/java和src/main/rsources文件夹,如果习惯把资源文件放在代码同目录下(src/main/java),使用 Maven 部署 Java Web 项目时,Hibernate 的映射文件 *.hbm.xml 没有被打包部署到目标目录下,一种解决办法是在pom.xml的<biuld></biuld>中加入如下内容:

<!-- 使用 Maven 部署 Java Web 项目时,Hibernate 的映射文件 *.hbm.xml 没有被打包部署到目标目录下 -->
        <resources>
            <resource>
                <directory>src/main/java</directory>
                <includes>
                    <include>**/*.hbm.xml</include>
                </includes>
            </resource>
            <!--  
            <resource>
                <directory>src/main/resources</directory>
                <includes>
                    <include>**/*.xml</include>
                    <include>**/*.properties</include>
                </includes>
            </resource>
            -->
        </resources>


maven搭建SSH项目大致就这样了,具体的各层java代码和我的上一篇博文里面的一致。


你可能感兴趣的:(maven,ssh,设计)