首先,萬分抱歉,據上一篇更新後,時隔已近一個月。雖然博主不日念起此事,但實在是最近繁事纏身,說這些也是想告訴對此文有所期待的朋友,博主一定會更完到最後一章,不盡人意之處,還請各位多擔待。如果不出意外,博主還是努力一周更兩篇。 前面,我們看過項目實際運行的功能介紹,表結構也有所瞭解。今天,我們就正式進 ...
首先,萬分抱歉,據上一篇更新後,時隔已近一個月。雖然博主不日念起此事,但實在是最近繁事纏身,說這些也是想告訴對此文有所期待的朋友,博主一定會更完到最後一章,不盡人意之處,還請各位多擔待。如果不出意外,博主還是努力一周更兩篇。
前面,我們看過項目實際運行的功能介紹,表結構也有所瞭解。今天,我們就正式進入編碼工作。代碼寫到哪,博主會將代碼實時提交到git,有需要的朋友,可以自行前往下載。地址:https://git.oschina.net/input4hua/input4hua_reader.git
首先我們在的開發工具,也就是eclipse上新建一個maven項目,如果這一步不清楚的同學,可以網上找下這方面的資料。我們的項目也是用maven構建的。
我這裡給出了項目的包結構,相應的也給出了每個包的說明。
domain是和實體類相關,其下有
dao.base,dao,dao.impl,這三個包是數據持久層,即這一層與資料庫打交道。
infra提供了各層公用模塊,包括其下有
utils工具包,VO展示對象
web即網路訪問層,其下controller包即是控制層,接收通過網路發送過來的請求
整體流程就是controller控制層接收請求,將請求數據交由service服務層處理。服務層里處理邏輯,併在有必要的時候調用dao層從資料庫請求數據,dao層從資料庫得到數據後,通過entity層相應的實體類返回給service層。service層得到dao層返回來的實體類數據,做邏輯處理並轉換封裝成VO類,返回給controller層。controller層調用視圖解析器(我們這裡是freemaker模板引擎),將數據展現到頁面,如下圖所示
接下來,我們來看一下pom文件,maven項目通過pom來管理整個項目所需jar包依賴
<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>com.input4hua</groupId> <artifactId>myreader</artifactId> <version>1.0.0</version> <packaging>war</packaging> <name>myreader</name> <url>http://maven.apache.org</url> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> </properties> <dependencies> <dependency> <groupId>org.apache.openejb</groupId> <artifactId>javaee-api</artifactId> <version>5.0-1</version> <scope>provided</scope> </dependency> <dependency> <groupId>javax.servlet</groupId> <artifactId>jstl</artifactId> <version>1.2</version> <scope>provided</scope> </dependency> <dependency> <groupId>javax.servlet.jsp</groupId> <artifactId>jsp-api</artifactId> <version>2.1</version> <scope>provided</scope> </dependency> <!-- mysql資料庫驅動 --> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>5.1.37</version> </dependency> <!-- aspectjweaver.jar這是Spring AOP所要用到的包 --> <dependency> <groupId>org.aspectj</groupId> <artifactId>aspectjweaver</artifactId> <version>1.7.1</version> </dependency> <!-- hibernate4 --> <dependency> <groupId>org.hibernate</groupId> <artifactId>hibernate-core</artifactId> <version>4.2.5.Final</version> </dependency> <dependency> <groupId>org.hibernate</groupId> <artifactId>hibernate-entitymanager</artifactId> <version>4.2.5.Final</version> </dependency> <dependency> <groupId>org.hibernate</groupId> <artifactId>hibernate-ehcache</artifactId> <version>4.2.5.Final</version> </dependency> <dependency> <groupId>org.hibernate.javax.persistence</groupId> <artifactId>hibernate-jpa-2.0-api</artifactId> <version>1.0.1.Final</version> </dependency> <!-- spring mvc --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-webmvc</artifactId> <version>3.2.3.RELEASE</version> </dependency> <!-- freemarker --> <dependency> <groupId>org.freemarker</groupId> <artifactId>freemarker</artifactId> <version>2.3.20</version> </dependency> <!-- spring3 --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context-support</artifactId> <version>3.2.3.RELEASE</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-core</artifactId> <version>3.2.0.RELEASE</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> <version>3.2.0.RELEASE</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-jdbc</artifactId> <version>3.2.0.RELEASE</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-beans</artifactId> <version>3.2.0.RELEASE</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-web</artifactId> <version>3.2.0.RELEASE</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-expression</artifactId> <version>3.2.0.RELEASE</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-orm</artifactId> <version>3.2.0.RELEASE</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-test</artifactId> <version>3.2.0.RELEASE</version> </dependency> <!--jsp頁面使用的jstl --> <dependency> <groupId>javax.servlet</groupId> <artifactId>jstl</artifactId> <version>1.2</version> </dependency> <dependency> <groupId>commons-beanutils</groupId> <artifactId>commons-beanutils</artifactId> <version>1.9.3</version> </dependency> <!-- 數據源 c3p0 --> <dependency> <groupId>com.mchange</groupId> <artifactId>c3p0</artifactId> <version>0.9.5-pre8</version> </dependency> <dependency> <groupId>com.mchange</groupId> <artifactId>mchange-commons-java</artifactId> <version>0.2.12</version> </dependency> <!-- Jackson JSON Mapper --> <dependency> <groupId>org.codehaus.jackson</groupId> <artifactId>jackson-mapper-asl</artifactId> <version>1.9.10</version> </dependency> <!-- Redis --> <dependency> <groupId>redis.clients</groupId> <artifactId>jedis</artifactId> <version>2.1.0</version> </dependency> <dependency> <groupId>org.apache.commons</groupId> <artifactId>commons-lang3</artifactId> <version>3.3.2</version> </dependency> <!-- log4j --> <dependency> <groupId>org.slf4j</groupId> <artifactId>slf4j-api</artifactId> <version>1.7.2</version> </dependency> <dependency> <groupId>org.slf4j</groupId> <artifactId>slf4j-log4j12</artifactId> <version>1.7.2</version> </dependency> <dependency> <groupId>log4j</groupId> <artifactId>log4j</artifactId> <version>1.2.17</version> </dependency> </dependencies> <build> <plugins> <plugin> <artifactId>maven-war-plugin</artifactId> </plugin> <plugin> <artifactId>maven-compiler-plugin</artifactId> <configuration> <source>1.7</source> <target>1.7</target> </configuration> </plugin> <plugin> <groupId>org.apache.tomcat.maven</groupId> <artifactId>tomcat7-maven-plugin</artifactId> <version>2.2</version> <configuration> <port>8081</port> <path>/</path> <uriEncoding>UTF-8</uriEncoding> <finalName>myreader</finalName> <server>tomcat7</server> </configuration> </plugin> </plugins> </build> </project>
從這個pom文件,大家就能看到我們這個項目所用到的一些組件了。需要說的是
<plugin> <groupId>org.apache.tomcat.maven</groupId> <artifactId>tomcat7-maven-plugin</artifactId> <version>2.2</version> <configuration> <port>8081</port> <path>/</path> <uriEncoding>UTF-8</uriEncoding> <finalName>myreader</finalName> <server>tomcat7</server> </configuration> </plugin>
這個插件配置後,我們便可以不再在eclipse里配置tomcat了
具體操作如下所示:
如上圖所示,點擊debug後的小倒三角形,打開Debug Configuations面板,在左側Maven Build上點擊右鍵New,如下圖順序
在上方Name:取名,這裡隨意。
在Glals:後填入clean install註意這裡有空格
而後,點擊Workspace…按鈕選擇要關聯的項目,這裡是myreader
然後,切換tab頁到JRE ,如下圖選擇jdk為1.7
接下來,切換tab頁到Source關聯一個源碼項目,如下圖,這裡選擇myreader
最後Apply應用
這一步完成之後,我們先暫且放在這裡,因為這個時候點擊Debug是會報錯的。因為我們的項目可能缺少文件。
如下圖所示,我們需要添加一個web.xml文件在webapp下的WEB-INF
接下來我們就結合web.xml里的內容來看看resource下的各個配置文件
首先是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_2_5.xsd" version="2.5"> <display-name></display-name> <welcome-file-list> <welcome-file>index.html</welcome-file> </welcome-file-list> <context-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:spring-hibernate.xml</param-value> </context-param> <filter> <filter-name>openSessionInViewFilter</filter-name> <filter-class>org.springframework.orm.hibernate4.support.OpenSessionInViewFilter</filter-class> <init-param> <param-name>singleSession</param-name> <param-value>true</param-value> </init-param> </filter> <filter-mapping> <filter-name>openSessionInViewFilter</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> <servlet> <servlet-name>springMVC</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <init-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:springMVC.xml</param-value> </init-param> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>springMVC</servlet-name> <url-pattern>/</url-pattern> </servlet-mapping> <filter> <filter-name>encodingFilter</filter-name> <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class> <init-param> <param-name>encoding</param-name> <param-value>UTF-8</param-value> </init-param> <init-param> <param-name>forceEncoding</param-name> <param-value>true</param-value> </init-param> </filter> <filter-mapping> <filter-name>encodingFilter</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> </web-app>
我們配置上下文參數contextConfigLocation來指定spring配置文件路徑
配置一個openSessionInViewFilter過濾器
配置一個springMVC相關聯的servlet,並制定springMVC配置文件路徑
encodelingFiter是編碼過濾器
下麵是配置文件的介紹
common.properties
#flie save place 文件保存路徑,這裡指的是圖書每章內容的文本保存根目錄
contentBasePath=F:/mrFile/
#image save place 普通保存路徑,即圖書封面圖片保存的根目錄
imageServerPath=http://192.168.11.9:8085/imgPath/
#每頁章節數目
menu_pageSize=100
#每頁圖書數目
book_pageSize=20
#首頁圖書數目
book_homeSize=50
#模糊查詢匹配圖書數目
book_likeSize=5
#我的書架圖書cookie時間
book_storeCookie_time=31536000
#config for redis 以下是redis的配置
redis.pool.maxActive=512
redis.pool.maxIdle=100
redis.pool.maxWait=100000
redis.pool.testOnBorrow=true
redis.pool.testOnReturn=true
redis.ip=127.0.0.1
redis.port=6379
redis.expire=3600
config.properties
#MySQL \u7684\u914D\u7F6E hibernate.dialect=org.hibernate.dialect.MySQLDialect driverClassName=com.mysql.jdbc.Driver validationQuery=SELECT 1 jdbc_url=jdbc\:mysql://localhost:3306/myreader?useUnicode\=true&characterEncoding\=utf-8 jdbc_username=root jdbc_password=root hibernate.hbm2ddl.auto=update hibernate.show_sql=false hibernate.format_sql=false sessionInfoName=sessionInfo uploadFieldName=filedata uploadFileMaxSize=20971520 uploadFileExts=txt,rar,zip,doc,docx,xls,xlsx,jpg,jpeg,gif,png,swf,wmv,avi,wma,mp3,mid uploadDirectory=attached
這裡是heibernate的一些配置,包括jdbc資料庫連接的參數
freemarker.properties
#\u8BBE\u7F6E\u6807\u7B7E\u7C7B\u578B\uFF1Asquare_bracket:[] auto_detect:[]<> tag_syntax=auto_detect #\u6A21\u7248\u7F13\u5B58\u65F6\u95F4\uFF0C\u5355\u4F4D\uFF1A\u79D2 template_update_delay=0 default_encoding=UTF-8 output_encoding=UTF-8 locale=zh_CN #\u8BBE\u7F6E\u6570\u5B57\u683C\u5F0F \uFF0C\u9632\u6B62\u51FA\u73B0 000.00 number_format=\# #\u53D8\u91CF\u4E3A\u7A7A\u65F6\uFF0C\u4E0D\u4F1A\u62A5\u9519 classic_compatible=true #\u8FD9\u4E2A\u8868\u793A\u6BCF\u4E2Afreemarker\u7684\u89C6\u56FE\u9875\u9762\u90FD\u4F1A\u81EA\u52A8\u5F15\u5165\u8FD9\u4E2Aftl\u6587\u4EF6\u3002\u91CC\u9762\u5B9A\u8BAE\u7684\u5C31\u662F\u4E00\u4E9B\u5B8F\uFF0C\u5982text\u6587\u672C\u6846\uFF0C\u5404\u79CDform\u5143\u7D20 #auto_import="/WEB-INF/templates/index.ftl" as do
log4j.properties
log4j.rootLogger = WARN , stdout log4j.appender.stdout = org.apache.log4j.ConsoleAppender log4j.appender.stdout.Target = System.out log4j.appender.stdout.layout = org.apache.log4j.PatternLayout log4j.appender.stdout.layout.ConversionPattern = %-d{yyyy-MM-dd HH:mm:ss} [ %t:%r ] - [ %p ] %m%n log4j.appender.D = org.apache.log4j.DailyRollingFileAppender log4j.appender.D.File = /usr/local/tomcat/tomcat8080/logs/log.log log4j.appender.D.Append = true log4j.appender.D.Threshold = DEBUG log4j.appender.D.layout = org.apache.log4j.PatternLayout log4j.appender.D.layout.ConversionPattern = %-d{yyyy-MM-dd HH:mm:ss} [ %t:%r ] - [ %p