前言:这个项目是我的第一个实验性项目,最初的立意是制作一个个性化的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.0modelVersion> <groupId>com.learnhowgroupId> <artifactId>bbs2016artifactId> <packaging>warpackaging> <version>1.0version> <name>bbs2016 Maven Webappname> <url>http://maven.apache.orgurl> <dependencies> <dependency> <groupId>commons-fileuploadgroupId> <artifactId>commons-fileuploadartifactId> <version>1.3.1version> dependency> <dependency> <groupId>net.sf.json-libgroupId> <artifactId>json-libartifactId> <version>2.4version> <classifier>jdk15classifier> dependency> <dependency> <groupId>tomcatgroupId> <artifactId>jasper-runtimeartifactId> <version>5.5.23version> dependency> <dependency> <groupId>org.jdomgroupId> <artifactId>jdomartifactId> <version>2.0.6version> dependency> <dependency> <groupId>jstlgroupId> <artifactId>jstlartifactId> <version>1.2version> <scope>compilescope> dependency> <dependency> <groupId>taglibsgroupId> <artifactId>standardartifactId> <version>1.1.2version> <scope>compilescope> dependency> <dependency> <groupId>org.apache.commonsgroupId> <artifactId>commons-dbcp2artifactId> <version>2.1.1version> dependency> <dependency> <groupId>org.springframeworkgroupId> <artifactId>spring-ormartifactId> <version>4.2.2.RELEASEversion> dependency> <dependency> <groupId>org.hibernategroupId> <artifactId>hibernate-coreartifactId> <version>4.3.11.Finalversion> dependency> <dependency> <groupId>mysqlgroupId> <artifactId>mysql-connector-javaartifactId> <version>5.1.36version> dependency> <dependency> <groupId>org.apache.strutsgroupId> <artifactId>struts2-spring-pluginartifactId> <version>2.3.24version> dependency> <dependency> <groupId>org.springframeworkgroupId> <artifactId>spring-webartifactId> <version>4.2.2.RELEASEversion> dependency> <dependency> <groupId>org.springframeworkgroupId> <artifactId>spring-contextartifactId> <version>4.2.2.RELEASEversion> dependency> <dependency> <groupId>org.springframeworkgroupId> <artifactId>spring-aspectsartifactId> <version>4.2.2.RELEASEversion> dependency> <dependency> <groupId>org.apache.strutsgroupId> <artifactId>struts2-coreartifactId> <version>2.3.24version> <exclusions> <exclusion> <groupId>javassistgroupId> <artifactId>javassistartifactId> exclusion> exclusions> dependency> <dependency> <groupId>log4jgroupId> <artifactId>log4jartifactId> <version>1.2.17version> dependency> <dependency> <groupId>junitgroupId> <artifactId>junitartifactId> <version>4.12version> <scope>testscope> dependency> dependencies> <build> <finalName>bbs2016finalName> build> project>
二、配置文件
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"> <display-name>bbs2016 by learnhowdisplay-name> <welcome-file-list> <welcome-file>home_browseHomewelcome-file> welcome-file-list> <filter> <filter-name>struts2filter-name> <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilterfilter-class> filter> <filter-mapping> <filter-name>struts2filter-name> <url-pattern>/*url-pattern> filter-mapping> <listener> <listener-class>org.springframework.web.context.ContextLoaderListenerlistener-class> listener> <listener> <listener-class>framework.ApplicationScopeLoaderListenerlistener-class> listener> <context-param> <param-name>contextConfigLocationparam-name> <param-value>classpath:spring.xml,classpath:springhibernate.xmlparam-value> context-param> web-app>
接下来就可以配置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> <constant name="struts.objectFactory" value="spring" /> <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_browseHomeresult> <result name="error">/WEB-INF/jsp/error.jspresult> global-results> <action name="home"> <result type="redirect">home_browseHomeresult> action> <action name="register"> <result>/WEB-INF/jsp/register.jspresult> action> <action name="custom"> <result>/WEB-INF/jsp/custom.jspresult> 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.jspresult> <result name="error">/WEB-INF/jsp/error.jspresult> action> <action name="login_*" class="action.LoginAction" method="{1}"> <result name="success" type="stream"> <param name="contentType">text/htmlparam> <param name="inputName">inputStreamparam> result> action> <action name="ajax_*" class="action.AjaxAction" method="{1}"> <result name="success" type="stream"> <param name="contentType">text/htmlparam> <param name="inputName">inputStreamparam> result> action> <action name="register_*" class="action.RegisterAction" method="{1}"> <result name="repeat" type="stream"> <param name="contentType">text/htmlparam> <param name="inputName">inputStreamparam> 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.jspresult> action> <action name="upload_*" class="action.UploadAction" method="{1}"> <result name="success" type="redirect">customresult> action> package> struts>
顺带插一句有关jsp中文乱码的解决方案。上面引入的是struts2文件中的配置方法,另一种更简单的方法是采用Tomcat容器提供的filter。
<filter> <filter-name>CharacterEncodingFilterfilter-name> <filter-class>org.apache.catalina.filters.SetCharacterEncodingFilterfilter-class> <init-param> <param-name>encodingparam-name> <param-value>utf-8param-value> init-param> filter>
然后开始配置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"> <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> <bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean"> <property name="dataSource" ref="dataSource" /> <property name="packagesToScan"> <list> <value>modelvalue> 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> <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> <aop:config> <aop:pointcut id="transactionPointcut" expression="execution(* dao.nimpl.*.*(..))" /> <aop:advisor pointcut-ref="transactionPointcut" advice-ref="txAdvice" /> aop:config> beans>
有关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
最后是引入包扫描和占位符文件的配置信息,这个部分在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>
篇幅所限,先写到这里。有关配合方面的说明基本到这里结束,下一章开始写一些设计和代码方面的东西,更多干货可以期待哦。