JavaWeb框架搭建之ssh整合關鍵點
關於jar包的引入
struts2的jar包:官網下載 struts-2.3.35-all.zip的壓縮包。在struts-2.3.35\apps下面有一些專案war包,選擇struts2-blank.war,這是一個空的專案的。複製專案裡的jar包,這是struts2開發最基礎、最原生的所需的jar包。如圖:
另外,在struts-2.3.35\lib下面還有兩個jar包。一個是struts2-convention-plugin-2.3.35.jar,這個是struts2基於註解開發所必需的。還有一個是struts2-spring-plugin-2.3.35.jar,這個就是用於struts2整合spring的外掛包,整合spring的時候必須要引入的。不過,在spring當中也提供了整合struts2的jar包 spring-struts-3.2.9.RELEASE.jar,用於整合struts2的整合。兩個jar包引入其中一個就可以了。
hibernate的jar包:對於hibernate5.0以後的版本,在官網下載對應版本的zip,以hibernate-release-5.0.12.Final.jar為例,只要引入\hibernate-release-5.0.12.Final\lib\required、\hibernate-release-5.0.12.Final\lib\jpa、\hibernate-release-5.0.12.Final\lib\jpa-metamodel-generator下的jar即可,這裡面已經包含了hibernate的核心包以及相關jpa的支援。
spring的jar包:在spring4.0以後,spring官方文件說明中也提到建議使用hibernate5.0+的版本。spring依賴注入的核心jar包
關於配置檔案
首先是web.xml的配置
<?xml version="1.0" encoding="UTF-8"?> <web-app id="WebApp_9" version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"> <!-- springWeb監聽器 --> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> <!-- 全域性引數配置,WEB-INF/applicationContext.xml --> <context-param> <param-name>config</param-name> <param-value>classpath:applicationContext.xml</param-value> </context-param> <!-- 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>
<!-- 初始化首頁 --> <welcome-file-list> <welcome-file>index.html</welcome-file> </welcome-file-list>
</web-app>
再就是applicationContext.xml檔案,這個跟spring單獨使用沒有什麼區別。
最後是struts.xml。struts2整合spring之後,Action的建立交給spring管理,