zoukankan      html  css  js  c++  java
  • SSH框架整合项目(一)——搭建平台和引入依赖

    前言:这个项目是我的第一个实验性项目,最初的立意是制作一个个性化的BBS。由于BBS能够综合大部分功能,因此作为练手的项目来说再好不过。从写第一行代码到完成测试版大概历时2周。中间遇到了不少以前在学习中没有想到的问题,当然通过解决这些难题也更加深了对MVC模型的理解。本来打算至少完成1.0版本以后再发布出来,可惜由于新工作的原因估计短时间内很难继续完成。所以就凑合把两周中间我经历的种种记录于此。

    项目后台通过Struts2+Spring+Hibernate搭建,前台使用了比较简单的Div+CSS+jQuery作为展示。

    一、通过Maven管理依赖

    这个过程比较复杂,虽然在网上能够查到不少现成的Maven依赖配置。但出于学习考虑,我还是手动添加并通过不断测试尽量将引入的依赖最小化。除了对基本框架的依赖以外,还有不少依赖关系是跟随项目开发进程不断引入的。

    <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.learnhow</groupId>
        <artifactId>bbs2016</artifactId>
        <packaging>war</packaging>
        <version>1.0</version>
        <name>bbs2016 Maven Webapp</name>
        <url>http://maven.apache.org</url>
        <dependencies>
            <!-- 添加上传依赖,自动添加io依赖 -->
            <dependency>
                <groupId>commons-fileupload</groupId>
                <artifactId>commons-fileupload</artifactId>
                <version>1.3.1</version>
            </dependency>
            <!-- 添加json-lib依赖 -->
            <dependency>
                <groupId>net.sf.json-lib</groupId>
                <artifactId>json-lib</artifactId>
                <version>2.4</version>
                <classifier>jdk15</classifier>
            </dependency>
            <!-- 添加tomcat -->
            <dependency>
                <groupId>tomcat</groupId>
                <artifactId>jasper-runtime</artifactId>
                <version>5.5.23</version>
            </dependency>
            <!-- 特别添加jdom,由于此依赖包并非从maven库中下载,因此事先需要在本地的maven库中添加此版本的jar包 -->
            <dependency>
                <groupId>org.jdom</groupId>
                <artifactId>jdom</artifactId>
                <version>2.0.6</version>
            </dependency>
            <!-- 添加jstl依赖 -->
            <dependency>
                <groupId>jstl</groupId>
                <artifactId>jstl</artifactId>
                <version>1.2</version>
                <scope>compile</scope>
            </dependency>
            <dependency>
                <groupId>taglibs</groupId>
                <artifactId>standard</artifactId>
                <version>1.1.2</version>
                <scope>compile</scope>
            </dependency>
            <!-- 添加spring整合hibernate依赖包 -->
            <dependency>
                <groupId>org.apache.commons</groupId>
                <artifactId>commons-dbcp2</artifactId>
                <version>2.1.1</version>
            </dependency>
            <dependency>
                <groupId>org.springframework</groupId>
                <artifactId>spring-orm</artifactId>
                <version>4.2.2.RELEASE</version>
            </dependency>
            <!-- 添加hibernate基础依赖 -->
            <dependency>
                <groupId>org.hibernate</groupId>
                <artifactId>hibernate-core</artifactId>
                <version>4.3.11.Final</version>
            </dependency>
            <dependency>
                <groupId>mysql</groupId>
                <artifactId>mysql-connector-java</artifactId>
                <version>5.1.36</version>
            </dependency>
            <!-- 添加struts整合spring依赖包 -->
            <dependency>
                <groupId>org.apache.struts</groupId>
                <artifactId>struts2-spring-plugin</artifactId>
                <version>2.3.24</version>
            </dependency>
            <!-- 添加spring-web包的依赖,主要是为了以后开发方便 -->
            <dependency>
                <groupId>org.springframework</groupId>
                <artifactId>spring-web</artifactId>
                <version>4.2.2.RELEASE</version>
            </dependency>
            <!-- 添加spring基础依赖包 -->
            <dependency>
                <groupId>org.springframework</groupId>
                <artifactId>spring-context</artifactId>
                <version>4.2.2.RELEASE</version>
            </dependency>
            <dependency>
                <groupId>org.springframework</groupId>
                <artifactId>spring-aspects</artifactId>
                <version>4.2.2.RELEASE</version>
            </dependency>
            <!-- 添加struts依赖包 -->
            <dependency>
                <groupId>org.apache.struts</groupId>
                <artifactId>struts2-core</artifactId>
                <version>2.3.24</version>
                <!-- 这里排除掉队javassist的依赖,通过hibernate包引入 -->
                <exclusions>
                    <exclusion>
                        <groupId>javassist</groupId>
                        <artifactId>javassist</artifactId>
                    </exclusion>
                </exclusions>
            </dependency>
            <dependency>
                <groupId>log4j</groupId>
                <artifactId>log4j</artifactId>
                <version>1.2.17</version>
            </dependency>
            <!-- 添加junit依赖包 -->
            <dependency>
                <groupId>junit</groupId>
                <artifactId>junit</artifactId>
                <version>4.12</version>
                <scope>test</scope>
            </dependency>
        </dependencies>
        <build>
            <finalName>bbs2016</finalName>
        </build>
    </project>
    Maven

    二、配置文件

    SSH搭建框架的关键主要有三点:

    (1)Struts2通过Spring提供的ObjectFactory获取各种Action实例

    (2)Hibernate使用Spring的控制翻转来注入SessionFactory,并集成事务管理

    (3)使用Tomcat监听器对Spring容器初始化

    首先应该配置的是Tomcat容器,这里千万不要使用由IDE自动为你产生的web.xml文件。注意文件头的部分,我的建议是从Tomcat里找到相关的配置信息复制过来。

    <?xml version="1.0" encoding="UTF-8"?>
    <web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee
                          http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
        version="3.1" metadata-complete="true">
    
        <!-- 尽量使用web.xml的标准写法 -->
        <display-name>bbs2016 by learnhow</display-name>
        <!-- 设置欢迎页面 -->
        <welcome-file-list>
            <welcome-file>home_browseHome</welcome-file>
        </welcome-file-list>
        <!-- 配置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>
        <!-- 配置spring前端监听器,当服务器启动的时候初始化xml配置 -->
        <listener>
            <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
        </listener>
        <listener>
            <listener-class>framework.ApplicationScopeLoaderListener</listener-class>
        </listener>
        <context-param>
            <!-- 所有需要初始化的xml都可以在这里配置 -->
            <param-name>contextConfigLocation</param-name>
            <param-value>classpath:spring.xml,classpath:springhibernate.xml</param-value>
        </context-param>
    </web-app>
    web.xml

    接下来就可以配置struts.xml,同样建议从Struts2提供的配置文件中复制相关文件头。注意我在Maven里引入的Struts版本为2.3。(注:最初引入配置文件的时候只需要清空package标签内部的action)

    <?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>
        <!-- 这个配置是用来指定struts2框架对于上传文件的临时路径,如果不指定会采用servlet容器的默认路径。 <constant name="struts.multipart.saveDir" 
            value=""/> -->
        <!-- 指定由spring容器产生action实例,这样才能注入需要服务 -->
        <constant name="struts.objectFactory" value="spring" />
        <!-- struts配置文件改动后,是否重新加载(生产环境中建议设置为false) -->
        <constant name="struts.configuration.xml.reload" value="true" />
        <!-- 关闭动态方法调用 -->
        <constant name="struts.enable.DynamicMethodInvocation" value="false" />
        <!-- 请求参数的编码方式 -->
        <constant name="struts.i18n.encoding" value="utf-8" />
        <package name="default" namespace="/" extends="struts-default">
            <!-- 配置默认请求转发地址 -->
            <default-action-ref name="home" />
            <global-results>
                <result name="home" type="redirect">home_browseHome</result>
                <result name="error">/WEB-INF/jsp/error.jsp</result>
            </global-results>
            <action name="home">
                <result type="redirect">home_browseHome</result>
            </action>
            <action name="register">
                <result>/WEB-INF/jsp/register.jsp</result>
            </action>
            <action name="custom">
                <result>/WEB-INF/jsp/custom.jsp</result>
            </action>
            <!-- 使用通配符的方法调用 -->
            <action name="home_*" class="action.HomeAction" method="{1}">
                <result name="home">/WEB-INF/jsp/home.jsp?page=${pageIndex}</result>
                <result name="success">/WEB-INF/jsp/success.jsp</result>
                <result name="error">/WEB-INF/jsp/error.jsp</result>
            </action>
            <action name="login_*" class="action.LoginAction" method="{1}">
                <result name="success" type="stream">
                    <param name="contentType">text/html</param>
                    <param name="inputName">inputStream</param>
                </result>
            </action>
            <action name="ajax_*" class="action.AjaxAction" method="{1}">
                <result name="success" type="stream">
                    <param name="contentType">text/html</param>
                    <param name="inputName">inputStream</param>
                </result>
            </action>
            <action name="register_*" class="action.RegisterAction" method="{1}">
                <result name="repeat" type="stream">
                    <param name="contentType">text/html</param>
                    <param name="inputName">inputStream</param>
                </result>
            </action>
            <action name="post_*" class="action.PostAction" method="{1}">
                <result name="browse" type="redirect">browse_browse?msgid=${msgid}
                </result>
            </action>
            <action name="browse_*" class="action.BrowseAction" method="{1}">
                <result name="browse">/WEB-INF/jsp/browse.jsp</result>
            </action>
            <action name="upload_*" class="action.UploadAction" method="{1}">
                <result name="success" type="redirect">custom</result>
            </action>
        </package>
    </struts>
    struts.xml

    顺带插一句有关jsp中文乱码的解决方案。上面引入的是struts2文件中的配置方法,另一种更简单的方法是采用Tomcat容器提供的filter。

    <filter>
        <filter-name>CharacterEncodingFilter</filter-name>
        <filter-class>org.apache.catalina.filters.SetCharacterEncodingFilter</filter-class>
        <init-param>
            <param-name>encoding</param-name>
            <param-value>utf-8</param-value>
        </init-param>
    </filter>
    web.xml

    然后开始配置Hibernate,由于是通过Spring整合的Hibernate,这个部分的配置实际上已经属于Spring的范畴。

    <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.xsd
            http://www.springframework.org/schema/context
            http://www.springframework.org/schema/context/spring-context.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">
    
        <!-- 配置数据源 -->
        <bean id="dataSource" destroy-method="close"
            class="org.apache.commons.dbcp2.BasicDataSource">
            <!-- 连接信息通过spring.xml文件引入 -->
            <property name="driverClassName" value="${mysql.driver}" />
            <property name="url" value="${mysql.url}" />
            <property name="username" value="${mysql.username}" />
            <property name="password" value="${mysql.password}" />
        </bean>
    
        <!-- 通过数据源初始化sessionFactory -->
        <bean id="sessionFactory"
            class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
            <property name="dataSource" ref="dataSource" />
            <!-- 包扫描,不用单独配置.hbm.xml文件 -->
            <property name="packagesToScan">
                <list>
                    <value>model</value>
                </list>
            </property>
            <property name="hibernateProperties">
                <value>
                    hibernate.dialect=${hibernate.dialect}
                    hibernate.show_sql=${hibernate.show_sql}
                    hibernate.hbm2ddl.auto=${hibernate.hbm2ddl.auto}
                    hibernate.format_sql=${hibernate.format}
                </value>
            </property>
        </bean>
    
        <!-- 通过注入sessionFactory获取事务控制器 -->
        <bean id="transactionManager"
            class="org.springframework.orm.hibernate4.HibernateTransactionManager">
            <property name="sessionFactory" ref="sessionFactory" />
        </bean>
    
        <!-- 事务建言:事务控制器的详细配置 -->
        <tx:advice id="txAdvice" transaction-manager="transactionManager">
            <tx:attributes>
                <!-- 启用事物的方法前缀 -->
                <tx:method name="add*" />
                <tx:method name="save*" />
                <tx:method name="update*" />
                <tx:method name="delete*" />
                <tx:method name="remove*" />
                <!-- 仅适用只读事物的方法前缀 -->
                <tx:method name="get*" read-only="true" />
                <tx:method name="find*" read-only="true" />
                <tx:method name="load*" read-only="true" />
                <!-- 默认其他方法不开启事物 -->
                <tx:method name="*" propagation="SUPPORTS" />
            </tx:attributes>
        </tx:advice>
    
        <!-- 注解方式配置事物 -->
        <tx:annotation-driven transaction-manager="transactionManager" />
    
        <!-- 配置需要添加事务控制的切点 -->
        <aop:config>
            <aop:pointcut id="transactionPointcut" expression="execution(* service.impl.*.*(..))" />
            <aop:advisor pointcut-ref="transactionPointcut"
                advice-ref="txAdvice" />
        </aop:config>
        
        <!-- 配置dao上的事务管理,主要是为了调试底层,方便数据库实现的操作,最后应该删除切记!!! -->
        <aop:config>
            <aop:pointcut id="transactionPointcut" expression="execution(* dao.nimpl.*.*(..))" />
            <aop:advisor pointcut-ref="transactionPointcut"
                advice-ref="txAdvice" />
        </aop:config>
        
    </beans>
    springhibernate.xml

    有关database.properties的配置选项在下面(密码请填写自己的MySQL登录密码)

    #config mysql
    mysql.driver=com.mysql.jdbc.Driver
    mysql.url=jdbc:mysql://localhost:3306/bbs2016
    mysql.username=root
    mysql.password=
    #config hibernate 
    hibernate.dialect=org.hibernate.dialect.MySQLDialect
    hibernate.show_sql=false
    hibernate.hbm2ddl.auto=update
    hibernate.format=true
    database.properties

    最后是引入包扫描和占位符文件的配置信息,这个部分在Spring中又被称为Root ApplicationContext。

    <?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/aop http://www.springframework.org/schema/aop/spring-aop.xsd
            http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd
            http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
    
        <!-- 扫描数据库配置文件 -->
        <context:property-placeholder location="classpath:database.properties" />
        
        <context:component-scan base-package="dao,service,model" />
    
    </beans>
    spring.xml

    篇幅所限,先写到这里。有关配合方面的说明基本到这里结束,下一章开始写一些设计和代码方面的东西,更多干货可以期待哦。

  • 相关阅读:
    Docker 第一篇 认识Docker 的作用好处
    AspNetCoreApi 跨域处理
    VS 2017 发布:由于构建错误,发布失败
    iTerm2 cheatsheet (from github)
    find命令:忽略一个目录或者多个目录
    git push throws error: RPC failed; result=22, HTTP code = 411的解决办法
    sourceTree 一款图形化GIT工具
    生产力工具之vimwiki 和 calendar
    source : not found 原因及解决办法
    亚马逊开放机器学习系统源代码:挑战谷歌TensorFlow
  • 原文地址:https://www.cnblogs.com/learnhow/p/5323559.html
Copyright © 2011-2022 走看看