一直希望能夠搭建一個完整的,基礎Web框架,方便日後接一些外快的時候,能夠省時省力,終於花了一周的時間,把這個東西搞定了。特此寫下此博客,一來是紀念,二來是希望能夠為別人提供方便。 ...
一直希望能夠搭建一個完整的,基礎Web框架,方便日後接一些外快的時候,能夠省時省力,終於花了一周的時間,把這個東西搞定了。特此寫下此博客,一來是紀念,二來是希望能夠為別人提供方便。順帶說一下,恩,組合框架的各個部分用的版本有的是最新的,有的則不是,不敢保證最新版本下,按照這個整合方式,不會報錯...
簡單介紹一下,本框架的基本功能點:
- Spring:整個框架的主體部分,這個自不用說。
- SpringMVC:MVC部分我還是比較喜歡Spring的。
- MyBatis:選型的時候選擇這個ORM主要也是考慮其靈活性的問題,畢竟我也不知道,今後會遇到怎樣的需求,用Hibernate一來是不太會用,二來,我還是比較喜歡直接寫SQL來的簡單一點。
- SpringSecurity:這個主要是安全框架,負責用戶登錄驗證及整站許可權分配的相關事項(許可權分配真的很有用,這個我就不多說了)。
- EhCache:一個非常流行又非常簡單好用的緩存框架,並且目前已經支持分散式,如果覺得不好用,自己換成Redis也都OK。
- JCaptcha:一個簡單的驗證碼生成框架(或者說工具)。
- Log4J:現在沒人不知道這個日誌框架吧...
按照這樣的選型,基本可以完成大部分網站的基礎框架,如:
- 用戶登錄管理,
- 整站許可權分配,
- 數據緩存,
- 登錄驗證碼,
- 資料庫操作,
- 統一異常處理,
- 日誌輸出。
網上找了很多文章,大部分都是只有部分整合的,比如SSH整合,SSM整合,SpringMVC+SpringSecurity,等等,東一塊,西一塊,非常分散,而且每個人的配置方式還不一樣,不統一,有的人喜歡註解配置,有的人喜歡XML配置,有的文章甚至直接就是有東沒西,神龍見首不見尾。看的我是很鬱悶,很蛋疼,因為當我好不容易搭好一個SSM框架,我發現,我要管理用戶登錄及許可權分配的時候,咦,我還得去配置Security,然後又要找很多文章來補充知識,配置Security的時候,又要找驗證碼的框架,都搞好了以後,發現誒,緩存這個東西不都是任何一個網站必備的嗎?所以就有了現在這篇文章,花了好大得勁,終於把所有都整合起來了。
但是,本文章,不提供項目源碼!
只有自己親手走過一遍,才真正學得會知識。作為一個程式員,我相信這樣才是對我們是最好的一個方式。所以不要索要源碼,不會給的。 先給出整個項目的
Pom.xml
1 <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 2 xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd"> 3 <modelVersion>4.0.0</modelVersion> 4 <groupId>com.magic.rent</groupId> 5 <artifactId>ssm</artifactId> 6 <packaging>war</packaging> 7 <version>1.0-SNAPSHOT</version> 8 <name>ssm Maven Webapp</name> 9 <url>http://maven.apache.org</url> 10 <repositories> 11 <repository> 12 <id>atlassian</id> 13 <name>atlassian</name> 14 <url>http://maven.jahia.org/maven2/</url> 15 </repository> 16 </repositories> 17 <build> 18 <finalName>ssm</finalName> 19 <plugins> 20 <!--Mybatis 逆向工程插件--> 21 <plugin> 22 <groupId>org.mybatis.generator</groupId> 23 <artifactId>mybatis-generator-maven-plugin</artifactId> 24 <version>1.3.2</version> 25 <configuration> 26 <verbose>true</verbose> 27 <overwrite>true</overwrite> 28 </configuration> 29 </plugin> 30 </plugins> 31 </build> 32 <properties> 33 <security.version>4.1.3.RELEASE</security.version> 34 <spring.version>4.3.3.RELEASE</spring.version> 35 </properties> 36 <dependencies> 37 <!-- SpringFramework Start --> 38 <dependency> 39 <groupId>org.springframework</groupId> 40 <artifactId>spring-core</artifactId> 41 <version>${spring.version}</version> 42 </dependency> 43 44 <dependency> 45 <groupId>org.springframework</groupId> 46 <artifactId>spring-web</artifactId> 47 <version>${spring.version}</version> 48 </dependency> 49 50 <dependency> 51 <groupId>org.springframework</groupId> 52 <artifactId>spring-oxm</artifactId> 53 <version>${spring.version}</version> 54 </dependency> 55 56 <dependency> 57 <groupId>org.springframework</groupId> 58 <artifactId>spring-tx</artifactId> 59 <version>${spring.version}</version> 60 </dependency> 61 62 <dependency> 63 <groupId>org.springframework</groupId> 64 <artifactId>spring-jdbc</artifactId> 65 <version>${spring.version}</version> 66 </dependency> 67 68 <dependency> 69 <groupId>org.springframework</groupId> 70 <artifactId>spring-webmvc</artifactId> 71 <version>${spring.version}</version> 72 </dependency> 73 74 <dependency> 75 <groupId>org.springframework</groupId> 76 <artifactId>spring-aop</artifactId> 77 <version>${spring.version}</version> 78 </dependency> 79 80 <dependency> 81 <groupId>org.springframework</groupId> 82 <artifactId>spring-context-support</artifactId> 83 <version>${spring.version}</version> 84 </dependency> 85 86 <dependency> 87 <groupId>org.springframework</groupId> 88 <artifactId>spring-test</artifactId> 89 <version>${spring.version}</version> 90 </dependency> 91 92 <dependency> 93 <groupId>org.springframework</groupId> 94 <artifactId>spring-expression</artifactId> 95 <version>${spring.version}</version> 96 </dependency> 97 <!-- SpringFramework End --> 98 99 <!--SpringSecurity Start--> 100 <dependency> 101 <groupId>org.springframework.security</groupId> 102 <artifactId>spring-security-core</artifactId> 103 <version>${security.version}</version> 104 </dependency> 105 <dependency> 106 <groupId>org.springframework.security</groupId> 107 <artifactId>spring-security-web</artifactId> 108 <version>${security.version}</version> 109 </dependency> 110 <dependency> 111 <groupId>org.springframework.security</groupId> 112 <artifactId>spring-security-config</artifactId> 113 <version>${security.version}</version> 114 </dependency> 115 <dependency> 116 <groupId>org.springframework.security</groupId> 117 <artifactId>spring-security-taglibs</artifactId> 118 <version>${security.version}</version> 119 </dependency> 120 <dependency> 121 <groupId>org.springframework.security</groupId> 122 <artifactId>spring-security-crypto</artifactId> 123 <version>${security.version}</version> 124 </dependency> 125 <!--SpringSecurity End--> 126 127 <!--aspectj start--> 128 <dependency> 129 <groupId>org.aspectj</groupId> 130 <artifactId>aspectjweaver</artifactId> 131 <version>1.8.6</version> 132 </dependency> 133 134 <dependency> 135 <groupId>org.aspectj</groupId> 136 <artifactId>aspectjrt</artifactId> 137 <version>1.8.6</version> 138 </dependency> 139 <!--aspectj end--> 140 141 <!--c3p0--> 142 <dependency> 143 <groupId>com.mchange</groupId> 144 <artifactId>c3p0</artifactId> 145 <version>0.9.5.1</version> 146 </dependency> 147 148 <!--servlet/jsp api start--> 149 <dependency> 150 <groupId>javax.servlet</groupId> 151 <artifactId>servlet-api</artifactId> 152 <version>2.5</version> 153 </dependency> 154 155 <dependency> 156 <groupId>javax.servlet.jsp</groupId> 157 <artifactId>jsp-api</artifactId> 158 <version>2.1</version> 159 <scope>provided</scope> 160 </dependency> 161 <!--servlet/jsp api end--> 162 163 <!--junit4--> 164 <dependency> 165 <groupId>junit</groupId> 166 <artifactId>junit</artifactId> 167 <version>4.11</version> 168 <scope>test</scope> 169 </dependency> 170 171 <!--Mybatis--> 172 <dependency> 173 <groupId>org.mybatis</groupId> 174 <artifactId>mybatis</artifactId> 175 <version>3.3.0</version> 176 </dependency> 177 <!--Mybatis Spring整合--> 178 <dependency> 179 <groupId>org.mybatis</groupId> 180 <artifactId>mybatis-spring</artifactId> 181 <version>1.2.3</version> 182 </dependency> 183 184 <!--MySQL Driver--> 185 <dependency> 186 <groupId>mysql</groupId> 187 <artifactId>mysql-connector-java</artifactId> 188 <version>5.1.6</version> 189 </dependency> 190 191 <dependency> 192 <groupId>jstl</groupId> 193 <artifactId>jstl</artifactId> 194 <version>1.2</version> 195 </dependency> 196 197 <!--JCaptcha驗證碼--> 198 <dependency> 199 <groupId>com.octo.captcha</groupId> 200 <artifactId>jcaptcha</artifactId> 201 <version>1.0</version> 202 </dependency> 203 204 <!--公共工具包--> 205 <dependency> 206 <groupId>org.apache.commons</groupId> 207 <artifactId>commons-lang3</artifactId> 208 <version>3.4</version> 209 </dependency> 210 211 <!--Ehcache緩存框架 start--> 212 <dependency> 213 <groupId>net.sf.ehcache</groupId> 214 <artifactId>ehcache-core</artifactId> 215 <version>2.6.11</version> 216 </dependency> 217 <!--Mybatis-Ehcache整合包--> 218 <dependency> 219 <groupId>org.mybatis</groupId> 220 <artifactId>mybatis-ehcache</artifactId> 221 <version>1.0.0</version> 222 </dependency> 223 <!--Ehcache-Web頁面及對象緩存--> 224 <dependency> 225 <groupId>net.sf.ehcache</groupId> 226 <artifactId>ehcache-web</artifactId> 227 <version>2.0.4</version> 228 </dependency> 229 <dependency> 230 <groupId>org.slf4j</groupId> 231 <artifactId>slf4j-api</artifactId> 232 <version>1.6.1</version> 233 </dependency> 234 <dependency> 235 <groupId>org.slf4j</groupId> 236 <artifactId>slf4j-log4j12</artifactId> 237 <version>1.6.2</version> 238 </dependency> 239 <!--Ehcache緩存框架 end--> 240 </dependencies> 241 </project>
項目結構截圖
[圖是高清的,我從1080P的屏幕下截取的]
[字太小可以下載到本地後放大,可以看得很清楚,親測]
[回頭有空,我再補一個目錄結構文檔]