1. 程式人生 > >利用IDEA 搭建 ssh框架

利用IDEA 搭建 ssh框架

IDEA搭建SSH環境

1.環境

軟體版本:IntelliJ IDEA 2016.3.2
系統:windows 7 32位 / ubuntu
框架:Hibernate3,Spring3.2, Struts2.3(跟框架版本關係不大)

2.問題

學了java之後又學了SSH三大框架,想做一個整體的專案,卻在怎麼搭建SSH環境上耗時不少,照著網上的也一直在報錯,後來才知道配置沒有問題,是XML配置上。現在把整個流程總結一下。

3.解決方法

1.建立Project:.
開啟軟體之後:File-->New-->Project。出現下圖,按照下圖設定。
注意:第3步可以選擇下載,我是下載過了,就選了第一個直接匯入
完成後點選Next

2.選擇專案要存放的路徑和專案名稱
然後點選Finsh

3.建立Tomcat服務
Run-->Edit Configrautions開啟如下介面,點左上角的加號,選擇Tomcat Server-->Local

根據下面創建出一個Tomcat Server

首先配置Server介面中的資訊:

然後配置Deployment中的資訊

4.建立Project Structure
點選:File-->Project Structure
先看左邊第一個Project

然後是Modules,Modules的中間要選中要操作的專案,右邊先看paths一般是預設,重要的是依賴:Dependencies.在這裡點右邊的加號,新增Spring和Hibernate的jar包

再之後是Artifacts,這裡是個重點,這一步的作用是把Modules中新增的依賴包放到專案的web/WEB-INF/lib目錄下

5.程式碼部分
jar包都已經引入,Tomcat Server部署也都設定好,還需要在程式碼都讓他們起作用,這部分是必須要寫的。需要操作兩個檔案web.xml中配置Spring,以及建立bean.xml檔案
程式碼是:
web.xml中新增spring配置部分

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"         xmlns:xsi
=
"http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" version="3.1"> <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> <!-- Spring配置 --> <context-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:bean.xml</param-value> </context-param> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> <welcome-file-list> <welcome-file>index.jsp</welcome-file> </welcome-file-list> </web-app>

然後在src中建立bean.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"       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">


</beans>

6.以上就用IDEA完成了SSH專案的配置,現在讓這個專案啟動起來: