從零開始基于struts2.3、hibernate4.3、spring4.2實現新聞發布系統。下面開始搭建開發環境,主要包括
1. 安裝eclipse插件
1. 下載jar包
1. 配置struts、spring、hibernate
### 一、安裝eclipse插件
在eclipse導航欄依次找到help->eclipse market。
1. 輸入hibernate搜索hibernate tools工具,點擊按照步驟安裝。
1. 輸入spring搜索spring tool suite,按照提示點擊安裝。
### 二、下載jar包
到下面的地址下載好struts 2.3.4、spring 4.2.3、hibernate4.3.11、commons-logging、mysql驅動、c3p0jar包。
1. struts 2下載地址 [struts 2](http://struts.apache.org/index.html)
1. spring下載地址[spring](http://repo.springsource.org/libs-release-local/org/springframework/spring/)
1. hibernate下載地址[hibernate](http://hibernate.org/orm/downloads/)
1. commons-logging下載地址 [commons-logging](http://commons.apache.org/proper/commons-logging/download_logging.cgi),下載commons-logging-1.2-bin.zip
1. mysql驅動下載地址[mysql驅動](http://dev.mysql.com/downloads/connector/j/)
1. c3p0地址地址[c3p0][[http://mvnrepository.com/artifact/c3p0/c3p0]](http://mvnrepository.com/artifact/c3p0/c3p0])
### 三、新建web工程
新建一個dynamic web project,取名為sshnews,dynamic web module version選擇2.5.

新建index.jsp,然后配置好tomcat跑一下,這一步相信大家都會。
### 四、導入jar包
### spring所需要的jar包
打開spring-framework-4.2.3.RELEASE-dist\spring-framework-4.2.3.RELEASE\libs文件夾,復制以下jar包:
spring-aop-4.2.3.RELEASE.jar
spring-aspects-4.2.3.RELEASE.jar
spring-beans-4.2.3.RELEASE.jar
spring-context-4.2.3.RELEASE.jar
spring-context-support-4.2.3.RELEASE.jar
spring-core-4.2.3.RELEASE.jar
spring-expression-4.2.3.RELEASE.jar
spring-instrument-4.2.3.RELEASE.jar
spring-instrument-tomcat-4.2.3.RELEASE.jar
spring-jdbc-4.2.3.RELEASE.jar
spring-jms-4.2.3.RELEASE.jar
spring-messaging-4.2.3.RELEASE.jar
spring-orm-4.2.3.RELEASE.jar
spring-oxm-4.2.3.RELEASE.jar
spring-test-4.2.3.RELEASE.jar
spring-tx-4.2.3.RELEASE.jar
spring-web-4.2.3.RELEASE.jar
spring-webmvc-4.2.3.RELEASE.jar
spring-webmvc-portlet-4.2.3.RELEASE.jar
spring-websocket-4.2.3.RELEASE.ja
### struts所需要的jar包
asm-3.3.jar
asm-commons-3.3.jar
asm-tree-3.3.jar
commons-fileupload-1.3.1.jar
commons-io-2.2.jar
commons-lang3-3.2.jar
freemarker-2.3.22.jar
javassist-3.11.0.GA.jar
log4j-api-2.2.jar
log4j-core-2.2.jar
ognl-3.0.6.jar
struts2-core-2.3.24.jar
xwork-core-2.3.24.jar
### hibernate所需要的jar包
將hibernate-release-4.3.10.Final\lib\required目錄下的jar包全部copy到工程lib目錄下.
antlr-2.7.7.jar
dom4j-1.6.1.jar
hibernate-commons-annotations-4.0.5.Final.jar
hibernate-core-4.3.10.Final.jar
hibernate-jpa-2.1-api-1.0.0.Final.jar
jandex-1.1.0.Final.jar
javassist-3.18.1-GA.jar
jboss-logging-3.1.3.GA.jar
jboss-logging-annotations-1.2.0.Beta1.jar
jboss-transaction-api_1.2_spec-1.0.0.Final.jar
### 其他jar包
c3p0-0.9.1.2.jar
commons-logging-1.2.jar
mysql-connector-java-5.0.8-bin.jar
然后build path。
### 五、配置spring
### 配置web.xml
打開web.xml,因為安裝了spring的插件,可以自動提示,按自動提示快捷鍵,找到ContextLoaderListener,配置代碼會自動補全。

###新建spring配置文件
點擊工程名,新建source folder(注意:不是folder),命名為conf,用來存放配置文件.在conf目錄下新建Spring Bean Definition file,名稱為applicationContext.xml.
~~~
<?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:context="http://www.springframework.org/schema/context" 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/aop http://www.springframework.org/schema/aop/spring-aop-4.1.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.1.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.1.xsd">
</beans>
~~~
如果生成的applicationContent.xml沒有xmlns:context這些,可以手動補充上去。
### 修改web.xml
將web.xml中spring配置中的location改為:classpath:applicationContext.xml.
~~~
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContent.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
~~~
目前的工作空間是這樣的:

以上步驟我們完成了導入jar包、新建資源文件夾、修改web.xml、新增applicationContent.xml。
### 六、設置c3p0數據庫連接池
在conf目錄下新建db.properties文件, 加入以下數據庫連接信息
~~~
jdbc.user=root
jdbc.password=123456
jdbc.driverClass=com.mysql.jdbc.Driver
jdbc.jdbcUrl=jdbc:mysql:///sshnews
jdbc.initPoolSize=5
jdbc.maxPoolSize=10
~~~

mysql數據庫中的sshnews是我們使用的數據庫名,可以提前建好。
然后在applicationContent.xml加入以下代碼:
~~~
<!-- 導入資源文件 -->
<context:property-placeholder location="classpath:db.properties" />
<!-- 數據庫連接配置 -->
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="user" value="${jdbc.user}"></property>
<property name="password" value="${jdbc.password}"></property>
<property name="driverClass" value="${jdbc.driverClass}"></property>
<property name="jdbcUrl" value="${jdbc.jdbcUrl}"></property>
<property name="initialPoolSize" value="${jdbc.initPoolSize}"></property>
<property name="maxPoolSize" value="${jdbc.maxPoolSize}"></property>
</bean>
~~~

### 七、spring整合hibernate
在conf目錄下新建hibernate配置文件,單擊conf文件夾名,右鍵,new->others,鍵入hibernate,新增hibernate配置文件

加入以下配置:
~~~
<!-- 配置hibernate基本屬性 -->
<session-factory>
<!-- 方言 ctrl+shift+t 輸入mysql5,找到org.hibernate.dialect.MySQL5InnoDBDialect -->
<property name="hibernate.dialect"> org.hibernate.dialect.MySQL5InnoDBDialect</property>
<!-- 是否顯示格式化sql -->
<property name="show_sql">true</property>
<property name="format_sql">true</property>
<!-- 生成數據表的策略 -->
<property name="hbm2ddl.auto">update</property>
</session-factory>
~~~

在src目錄下新建model包,我的包名是cn.ac.ucas.form, 然后修改applicationContent.xml,加入以下配置:
~~~
<!-- 整合hibernate -->
<bean id="sessionFactorys" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
<property name="dataSource" ref="dataSource"></property>
<property name="configLocation" value="classpath:hibernate.cfg.xml"></property>
<property name="mappingLocations" value="classpath:cn/ac/ucas/form/*.hbm.xml"> </property>
</bean>
~~~
### 八、整合struts
#### 配置web.XML
打開web.xml,加入下面代碼:
~~~
<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>
~~~
#### 加入struts.xml
拷貝struts-2.3.24-all/struts-2.3.24/apps/struts2-blank/WEB-INF/src/java目錄下的struts.xml到src目錄下。
### 九、測試
在cn.ac.ucas.form包下新建News類:
~~~
package cn.ac.ucas.form;
public class News {
private Integer id;
private String title;//新聞標題
private String author;//新聞作者
private String source;//新聞來源
private String posttime;//發布時間
private String content;// 新聞內容
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getAuthor() {
return author;
}
public void setAuthor(String author) {
this.author = author;
}
public String getSource() {
return source;
}
public void setSource(String source) {
this.source = source;
}
public String getPosttime() {
return posttime;
}
public void setPosttime(String posttime) {
this.posttime = posttime;
}
public String getContent() {
return content;
}
public void setContent(String content) {
this.content = content;
}
}
~~~
點擊包名->右鍵->new->other,鍵入hibernate,新增hibernate xml mapping文件,然后默認下一步,finish。會在包下生成News類對應的hibernate映射文件。至此工程目錄如下:

運行工程,在數據庫中查看是否有新的數據庫表生成。

Success!
總結:1.把需要的jar包全部下載好備用,會很方便
2.安裝好eclipse插件會很方便
3.db.properties配置容易出錯,查看參數是否寫正確
4.不要忘記在數據庫中新建數據庫
ssh新聞發布系統第一天就到這里,如有錯誤歡迎指出。轉載請留言。
- 前言
- [J2EE]java web項目中調用word轉html命令行工具
- [J2EE]jsp項目中使用UEditor富文本編輯器
- [J2EE]The superclass "javax.servlet.http.HttpServlet" was not found on the Java Build Path
- [j2ee]Eclipse搭建SSH開發框架
- Could not open Hibernate Session for transaction
- class org.springframework.web.context.ContextLoaderListener
- [java01]Java基本數據類型
- [java02]運算符
- jsp、javabean學生信息管理系統
- [java03]java字符串
- [ssh新聞發布系統一]搭建開發環境
- [ssh新聞發布系統二] 讀取新聞
- [ssh新聞發布系統三]存儲新聞
- [ssh新聞發布系統四]使用富文本編輯器發布新聞
- [ssh新聞發布系統五]刪除新聞
- struts2 helloworld
- struts請求走向流程
- [java04]java大數類