在作JavaWeb的SSH框架開發的時候,遇到過不少的細節問題,這裏大概記錄下html
我使用的IDE是Eclipse(老版本)三大框架:Spring四、Struts二、Hibernate5前端
1.web.xml的配置java
<?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" id="WebApp_ID" version="2.5"> <display-name>Blog</display-name> <session-config> <session-timeout>60</session-timeout> </session-config> <!-- 讓spring隨web啓動而建立的監聽器 --> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> <!-- 配置spring配置文件位置參數 --> <context-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:applicationContext.xml</param-value> </context-param> <!-- 擴大session做用範圍 --> <filter> <filter-name>openSessionInView</filter-name> <filter-class>org.springframework.orm.hibernate5.support.OpenSessionInViewFilter</filter-class> </filter> <!-- struts2核心過濾器 --> <filter> <filter-name>struts2</filter-name> <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class> </filter> <filter-mapping> <filter-name>openSessionInView</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> <filter-mapping> <filter-name>struts2</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> <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> </web-app>
1.ContextLoaderListener的做用:mysql
ContextLoaderListener監聽器的做用就是啓動Web容器時,自動裝配ApplicationContext的配置信息。web
由於它實現了ServletContextListener這個接口,在web.xml配置這個監聽器,啓動容器時,就會默認執行它實現的方法。spring
在ContextLoaderListener中關聯了ContextLoader這個類,因此整個加載配置過程由ContextLoader來完成。sql
通俗簡單理解:必需要配置這個,用來讀取Spring配置文件數據庫
2.contextConfigLocation的做用:express
配置spring配置文件位置參數,這裏我Spring的XML配置文件在src下,直接寫便可apache
3.OpenSessionInViewFilter的做用:
Spring爲咱們解決Hibernate的Session的關閉與開啓問題。
Hibernate 容許對關聯對象、屬性進行延遲加載,可是必須保證延遲加載的操做限於同一個 Hibernate Session 範圍以內進行。
若是 Service 層返回一個啓用了延遲加載功能的領域對象給 Web 層,當 Web 層訪問到那些須要延遲加載的數據時,因爲加載領域對象的 Hibernate Session 已經關閉,這些致使延遲加載數據的訪問異常
好比拋出這種異常:
org.hibernate.LazyInitializationException:(LazyInitializationException.java:42)
- failed to lazily initialize a collection of role: cn.easyjava.bean.product.ProductType.childtypes, no session or session was closed
org.hibernate.LazyInitializationException: failed to lazily initialize a collection of role: cn.easyjava.bean.product.ProductType.childtypes,
no session or session was closed
用來把一個Hibernate Session和一次完整的請求過程對應的線程相綁定。目的是爲了實現"Open Session in View"的模式。例如: 它容許在事務提交以後延遲加載顯示所須要的對象。
而Spring爲咱們提供的OpenSessionInViewFilter過濾器爲咱們很好的解決了這個問題。
OpenSessionInViewFilter的主要功能是用來把一個Hibernate Session和一次完整的請求過程對應的線程相綁定。
目的是爲了實現"Open Session in View"的模式。例如: 它容許在事務提交以後延遲加載顯示所須要的對象
OpenSessionInViewFilter 過濾器將 Hibernate Session 綁定到請求線程中,它將自動被 Spring 的事務管理器探測到。
因此 OpenSessionInViewFilter 適用於 Service 層使用HibernateTransactionManager進行事務管理的環境,也能夠用於非事務只讀的數據操做中
通俗簡單解釋:有時候數據庫查到的結果返回到jsp頁面會拋異常,配置好這個就能夠解決。在前端頁面顯示完相應的結果再關閉session
其餘注意事項:
1.strust2核心過濾器必定要寫在最後邊
2.開頭我這裏設置的session有效時間60(單位:分鐘)
接下來Spring配置文件:
命名什麼均可以,我取名applicationContext.xml
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://www.springframework.org/schema/beans" 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-4.2.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.2.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.2.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.2.xsd "> <!-- 讀取db.properties文件 --> <context:property-placeholder location="classpath:db.properties" /> <!-- 配置c3p0鏈接池 --> <bean name="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"> <property name="jdbcUrl" value="${jdbc.jdbcUrl}"></property> <property name="driverClass" value="${jdbc.driverClass}"></property> <property name="user" value="${jdbc.user}"></property> <property name="password" value="${jdbc.password}"></property> </bean> <!-- 核心事務管理器 --> <bean name="transactionManager" class="org.springframework.orm.hibernate5.HibernateTransactionManager"> <property name="sessionFactory" ref="sessionFactory"></property> </bean> <!-- 配置通知 --> <tx:advice id="txAdvice" transaction-manager="transactionManager"> <tx:attributes> <tx:method name="save*" isolation="REPEATABLE_READ" propagation="REQUIRED" read-only="false" /> <tx:method name="persist*" isolation="REPEATABLE_READ" propagation="REQUIRED" read-only="false" /> <tx:method name="update*" isolation="REPEATABLE_READ" propagation="REQUIRED" read-only="false" /> <tx:method name="modify*" isolation="REPEATABLE_READ" propagation="REQUIRED" read-only="false" /> <tx:method name="delete*" isolation="REPEATABLE_READ" propagation="REQUIRED" read-only="false" /> <tx:method name="remove*" isolation="REPEATABLE_READ" propagation="REQUIRED" read-only="false" /> <tx:method name="get*" isolation="REPEATABLE_READ" propagation="REQUIRED" read-only="true" /> <tx:method name="find*" isolation="REPEATABLE_READ" propagation="REQUIRED" read-only="true" /> <tx:method name="*" isolation="REPEATABLE_READ" propagation="REQUIRED" read-only="false" /> </tx:attributes> </tx:advice> <!-- 配置將通知織入目標對象 --> <aop:config> <aop:pointcut expression="execution(* service.impl.*ServiceImpl.*(..))" id="txPc" /> <aop:advisor advice-ref="txAdvice" pointcut-ref="txPc" /> </aop:config> <!-- 在spring配置中放置hibernate配置信息 --> <bean name="sessionFactory" class="org.springframework.orm.hibernate5.LocalSessionFactoryBean"> <!-- 將鏈接池注入到sessionFactory, hibernate會經過鏈接池得到鏈接 --> <property name="dataSource" ref="dataSource"></property> <!-- 配置hibernate基本信息 --> <property name="hibernateProperties"> <props> <prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect </prop> <!-- 可選配置 --> <prop key="hibernate.hbm2ddl.auto">update</prop> </props> </property> <!-- 引入orm元數據,指定orm元數據所在的包路徑,spring會自動讀取包中的全部配置 --> <property name="mappingDirectoryLocations" value="classpath:domain"></property> </bean> <!-- action --> <!-- 注意:Action對象做用範圍必定是多例的(prototype).這樣才符合struts2架構 --> <bean name="userAction" class="web.action.UserAction" scope="prototype"> <property name="userService" ref="userService"></property> </bean> <!-- service --> <bean name="userService" class="service.impl.UserServiceImpl"> <property name="userdao" ref="userDao"></property> </bean> <!-- dao --> <bean name="userDao" class="dao.impl.UserDaoImpl"> <property name="sessionFactory" ref="sessionFactory"></property> </bean> </beans>
這裏解釋下Spring配置文件以及易錯處:
1.數據庫參數我寫在一個db.properties文件種,方便開發中修改
2.接下來配置c3p0鏈接池,雖然不使用也能夠,可是hibernate推薦使用
3.transactionManager這個bean,管理事務嵌套,開啓,關閉,資源線程同步,提交,回滾
初步瞭解, HibernateTransactionManager這個類提供 sessionFactory的管理。
爲了實現數據同步,在HibernateTransactionManager內部會進行Hibernate session的open和close,並將打開的Hibernaate sesion關聯到當前的Application session。
在Application中則經過getCurrentSession方式獲取爭取的打開的Hibernate session, 從而解決某些方面的線程安全及同步問題。
通俗解釋,這裏配置的意義在於:只有配置好這一項,才能夠在DAO層使用HibernateTemplate,方便數據庫操做
4.配置通知和AOP方面,見我之前文章:
http://www.cnblogs.com/xuyiqing/p/8463598.html
http://www.cnblogs.com/xuyiqing/p/8464465.html
這裏大概解釋下:
例如這裏:
<tx:method name="get*" isolation="REPEATABLE_READ" propagation="REQUIRED" read-only="true" />
這一句的意思是:全部DAO層方法,在生效前,先經過這裏的「通知」,若是是get開頭的方法,只能查詢數據庫,沒法修改
這裏的兩個參數:isolation="REPEATABLE_READ"事務隔離級別,暫不作解釋,開發中只選擇這一項
propagation="REQUIRED"事務傳播行爲,暫不作解釋,開發中只選擇這一項
最後一個屬性,是否只讀:若是是查詢類方法,應該設置爲只讀,若是是增刪更新操做,應該設置爲false
下面:expression="execution(* service.impl.*ServiceImpl.*(..))這裏是通配方法,爲全部service包的實現類中的全部方法配置切點
後邊是一個加強AOP配置,配好便可,沒什麼解釋的
5.在spring配置中放置hibernate配置信息:
先注入上面配置好的c3p0鏈接池,再配置Hibernate
<prop key="hibernate.hbm2ddl.auto">update</prop>的意思是,每一次生成表的時候若是表存在則覆蓋,表不存在的話新建一張表,這種方式是最經常使用的
<prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>使用MySQL方言,我使用MySQL開發,全部使用這個配置
<!-- 引入orm元數據,指定orm元數據所在的包路徑,spring會自動讀取包中的全部配置 -->
<property name="mappingDirectoryLocations" value="classpath:domain"></property>
這裏的意思:hibernate須要一些元數據配置,我把這些配置寫在了一個domain包中
最後的三層結構的bean配置就很少解釋了
附:db.properties
jdbc.jdbcUrl=jdbc:mysql:///blog?useUnicode=true&characterEncoding=utf-8&autoReconnect=true jdbc.driverClass=com.mysql.jdbc.Driver jdbc.user=root jdbc.password=123456
url設置的時候須要注意下:這裏有個大坑,當時解決了幾天。
後邊加入characterEncoding=utf-8才能夠往數據庫中存入中文,不然都是亂碼
後一個參數autoReconnect=true是意思的:自動重連,MySQL若是長時間不操做就會關閉(默認8小時)
上邊提到了hibernateORM元數據配置:
這裏繼續
我創建了一個實體類:User(get、set方法)
package domain; public class User { private Long u_id; private String username; private String u_password; private String qq; private String avatar; private Integer article_count; private String checkCode; public String getCheckCode() { return checkCode; } public void setCheckCode(String checkCode) { this.checkCode = checkCode; } public Long getU_id() { return u_id; } public void setU_id(Long u_id) { this.u_id = u_id; } public String getUsername() { return username; } public void setUsername(String username) { this.username = username; } public String getU_password() { return u_password; } public void setU_password(String u_password) { this.u_password = u_password; } public String getQq() { return qq; } public void setQq(String qq) { this.qq = qq; } public String getAvatar() { return avatar; } public void setAvatar(String avatar) { this.avatar = avatar; } public Integer getArticle_count() { return article_count; } public void setArticle_count(Integer article_count) { this.article_count = article_count; } }
對應ORM元數據配置:
User.hbm.xml:
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd"> <hibernate-mapping package="domain"> <class name="User" table="blog_user"> <id name="u_id"> <generator class="native"></generator> </id> <property name="username" column="username"></property> <property name="u_password" column="u_password"></property> <property name="qq" column="qq"></property> <property name="avatar" column="avatar"></property> <property name="article_count" column="article_count"></property> </class> </hibernate-mapping>
package是包名:在這個包下找到User類,映射
table是新建的表名字,Id是主鍵生成策略:
具體見我之前文章:http://www.cnblogs.com/xuyiqing/p/8449059.html
實體開發使用native便可
接下來就是配置表的字段名便可
複雜的配置,如:一對多,多對多等(外鍵)
好比我這裏還有一個article文章類:一個用戶能夠有多個文章,一個分類下也能夠有多個文章:
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd"> <hibernate-mapping package="domain"> <class name="Article" table="blog_article"> <id name="article_id"> <generator class="native"></generator> </id> <property name="title"></property> <property name="summary"></property> <property name="read_count"></property> <property name="comment_count"></property> <property name="up_count"></property> <property name="create_time"></property> <property name="article_detail"></property> <many-to-one name="article_type" column="article_type_id" class="Articletype"></many-to-one> <many-to-one name="author" column="article_author_id" class="User"></many-to-one> </class> </hibernate-mapping>
更多的細節見我之前的文章:
http://www.cnblogs.com/xuyiqing/category/1163473.html
接下來是struts2的配置:
<?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.multipart.maxSize" value="31457280"></constant> <constant name="struts.objectFactory" value="spring"></constant> <package name="Blog" namespace="/" extends="struts-default"> <interceptors> <interceptor name="privilegeInterceptor" class="web.interceptor.PrivilegeInterceptor"></interceptor> <interceptor-stack name="myStack"> <interceptor-ref name="privilegeInterceptor"> <param name="excludeMethods">login,regist,createRandom,execute</param> </interceptor-ref> <interceptor-ref name="defaultStack"></interceptor-ref> </interceptor-stack> </interceptors> <default-interceptor-ref name="myStack"></default-interceptor-ref> <global-results> <result name="toHome" type="redirect">/IndexAction_articleTypeList </result> <result name="toLogin" type="redirect">/login.jsp</result> </global-results> <!-- 整合:class屬性上填寫spring中action對象的BeanName 徹底由spring管理action生命週期,包括Action的建立 --> <action name="UserAction_*" class="userAction" method="{1}"> <exception-mapping result="loginError" exception="java.lang.RuntimeException"></exception-mapping> <result name="loginError">/login.jsp</result> <result name="regist">/register.jsp</result> </action> <action name="CheckImgAction" class="web.action.CheckImgAction"> <result name="success" type="stream"> <param name="contentType">image/jpeg</param> <param name="inputName">inputStream</param> </result> </action> <action name="UploadAction_*" class="uploadAction" method="{1}"> <result name="success">/index.jsp</result> <interceptor-ref name="defaultStack"> <param name="fileUpload.allowedExtensions">jepg,jpg,gif,png</param> </interceptor-ref> <result name="input">/index.jsp</result> </action> </package> <package name="article" extends="json-default"> <action name="OthersAction_*" class="othersAction" method="{1}"> <result name="good" type="json"></result> <result name="reply" type="json"></result> <result name="delete" type="json"></result> </action> </package> </struts>
開頭配置了兩個常量:上次文件最大字節、交給Spring管理Action
後邊的:
1.這裏我設置了一個攔截器:暫且稱它爲登陸狀態檢驗器:若是不登錄,沒法訪問個人BBS,下邊我寫出攔截器代碼,這裏先看配置
要把自定義攔截器放在默認攔截器前面
<param name="excludeMethods">login,regist,createRandom,execute</param>
這裏的意思是不攔截登陸、註冊、隨機生成驗證碼方法
2.接下來配置的是全局結果集
3.後邊的Action配置就沒必要詳細說了:注意這一句
<exception-mapping result="loginError" exception="java.lang.RuntimeException"></exception-mapping>
這裏我爲何要配置一個異常呢?很是有用
用於登陸錯誤回顯,Exception裏面能夠放message,即錯誤信息,好比用戶名不存在等等
若是有異常,這裏就返回當前頁面,帶着錯誤信息,前端頁面用EL或struts2標籤顯示便可:例如
<s:property value="exception.message" />
4.後邊的Action參數配置還能夠是一個流對象:用於作驗證碼圖片臨時保存
5.後邊我留下了一個文件上傳Action的配置:
<interceptor-ref name="defaultStack">
<param name="fileUpload.allowedExtensions">jepg,jpg,gif,png</param>
</interceptor-ref>
這裏的意思是:我作了一個過濾器,只能夠上傳圖片文件
6.<package name="article" extends="json-default">
這個包的做用很重要:是專門處理AJAX請求的,struts-default沒法處理AJAX請求
這裏是登陸攔截器的代碼:
package web.interceptor; import java.util.Map; import com.opensymphony.xwork2.ActionContext; import com.opensymphony.xwork2.ActionInvocation; import com.opensymphony.xwork2.interceptor.MethodFilterInterceptor; import domain.User; //驗證登陸狀態攔截器 @SuppressWarnings("all") public class PrivilegeInterceptor extends MethodFilterInterceptor { @Override // 不攔截登錄和註冊方法 protected String doIntercept(ActionInvocation invocation) throws Exception { // 1 得到Session Map<String, Object> session = ActionContext.getContext().getSession(); // 2 得到登錄標識 User user = (User) session.get("user"); // 3 判斷標識是否存在 if (user != null) { // 存在=> 放行 return invocation.invoke(); } else { // 不存在=> 重定向到登錄頁面 return "toLogin"; } } }
寫的很詳細,沒必要多說,都能看懂吧
配置文件部份內容就完了,後邊陸續更新代碼方面的坑和注意事項