在這里我選擇的IDE是原版的eclipse, 插件我也只裝了 Tomcat的插件,而沒有選擇用MyEclipse.
為什么我沒選擇用MyEclipse?
1. 之前做項(xiàng)目的時(shí)候,用過MyEclipse,但是在我的機(jī)器老死,而且速度慢,也許可能是我的機(jī)子差的原因,不知道大家有沒有類似的問題。后來換了Eclipse3.4原版 問題都沒了,用起來感覺還蠻不錯(cuò)的。
2. 不得不說Eclipse太強(qiáng)大了,它的插件無所不能。但是我在這里想對(duì)和我一樣的初學(xué)者來說,不論你學(xué)習(xí)什么語言,不要一上手就用這些高度集成的IDE開發(fā)工具,特別是那些可視化IDE(比如MS的VS系列)。高度集成的IDE只適合開發(fā)或高手用。在業(yè)內(nèi)有句話是這么說的,只能讓菜鳥更菜鳥,高手更高手。
3. 為什么沒用NetBean?在我的眼里那是高手用的,這個(gè)時(shí)段的我略過,而我用Eclipse + Tomcat 也只是圖編譯比較方便而已,其他功能基本上無視。

一,在eclipse創(chuàng)建一個(gè)項(xiàng)目叫 miziStudy

1.1 New->Project


1.2 在彈出窗口中選擇 Web->Dynamic Web Project, 然后Next


1.3  在此窗口設(shè)置你的項(xiàng)目名字,項(xiàng)目存放路徑以及tomcat版本

1.4 直接finish掉,后面的設(shè)置按照默認(rèn)設(shè)置,當(dāng)然你如果有興趣也可以去看看。
ok, finish后 你點(diǎn)eclipse右邊面板看到你新建的項(xiàng)目。


1.5, 項(xiàng)目創(chuàng)建完畢,現(xiàn)在就開始我們的SSH之路吧。
今天在這里講講SSH配置,我網(wǎng)絡(luò)上搜了好久,大家都講的很簡單,或者只是稍微提下,又或者把配置文件貼上來就了事了,但是殊不知這對(duì)像我這樣的菜鳥來說,特別是沒有一點(diǎn)java基礎(chǔ)的來說,是一件讓人很頭痛的事。也許是我比較傻也不一定,整整用了半個(gè)月的時(shí)間才弄懂怎么配置。(當(dāng)然不包括上班時(shí)間)。
二,Spring2.5 配置

2.1  在miziStudy/webContent/WEB-INF/ 下面找到web.xml


2.2 在web.xml 上配置spring2.5

<?xml version="1.0" encoding="UTF-8"?>
<web-app id="WebApp_ID" 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">
<!-- ###################################### -->
<!-- ########## Spring2 ################## -->
<!-- ###################################### -->
<!--
* [
<context-param></context-param ] =>用來設(shè)定web站臺(tái)的環(huán)境參數(shù)
* [
<param-name></param-name> ] (子元素)=> 用來指定參數(shù)的名稱
* [
<param-value></param-value> ] (子元素)=> 用來設(shè)定參數(shù)值
* ************
* 從類路徑下加載spring的配置文件, 多個(gè)配置文件可以用逗號(hào)和空格區(qū)分
* classpath: 關(guān)鍵字特指類路徑下加載
********************-->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext*.xml</param-value>
</context-param>

<!--
* [
<listener></listener>]=>用來設(shè)定監(jiān)聽接口
* [
<listener-class></listener-class>](子元素)=>定義Listener的類名稱
* *******
* 負(fù)責(zé)啟動(dòng)spring的監(jiān)聽器
* 它將引用處的上下文參數(shù)獲得spring配置文件地址
* 指定Spring提供的ContextLoaderListener Web 容器監(jiān)聽器,
* 該監(jiān)聽器在web容器啟動(dòng)時(shí)自動(dòng)運(yùn)行并且根據(jù)ContextLoaderListener參數(shù)
* 獲取Spring配置文件,并啟動(dòng)Spring容器。注意要將log4j.propertis放在
* 類目錄下,以備目錄引擎自動(dòng)聲效
************** -->

<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
</web-app>

2.3,在src下面新建 applicationContext.xml









2.4 給applicationContext.xml 加上spring的標(biāo)頭 (注意結(jié)束符號(hào)</beans>)


<?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:p
="http://www.springframework.org/schema/p"
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-2.5.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd"
>
</beans>

注意標(biāo)頭是2.5的 不要引入2.0, 錯(cuò)了可能Spring就不能正確加載。

2.5 引入spring2.5的包。
spring功能太強(qiáng)大,我們初學(xué)用不了那么多,所以只要引入我們需要的包就可以了。需要的時(shí)候再添加。
把包復(fù)制到 /WEB-INFO/lib 文件夾下



2.6 測(cè)試Spring是否配置成功
    2.6.1 在src下面我們新建一個(gè)包spring.test.hello
    (右單擊src文件夾,new -> package )


2.6.2 在spring.test.hello下面新建兩個(gè)java文件 UserPo.java, Hello.java
(右單擊 spring.test.hello 包,new->file)


2.6.3 修改UserPo.java, 代碼如下

package spring.test.hello;


public class UserPo {
    
    
private String username; //用戶呢稱
    private String sex; //性別
    private int age; //歲數(shù)
    
    
public String getUsername() {
        
return username;
    }

    
public void setUsername(String username) {
        
this.username = username;
    }

    
public String getSex() {
        
return sex;
    }

    
public void setSex(String sex) {
        
this.sex = sex;
    }

    
public int getAge() {
        
return age;
    }

    
public void setAge(int age) {
        
this.age = age;
    }

    
}

2.6.4 修改HelloTest.java 如下

package spring.test.hello;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class HelloTest {
    
    
public static void main( String[] args ) {
        
//加載spring配置文件,初始化IoC容器
        ApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext.xml");
        
        
//從容器 接管Bean
        UserPo user = (UserPo) ac.getBean("UserPo");
        
        
//輸出歡迎信息
        
        System.out.println( 
"Hello:" + user.getUsername() + ";u is " + user.getAge() + " age; and u is a " + user.getSex() );
        
    }

    
}

2.6.7 修改 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:p
="http://www.springframework.org/schema/p"
       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-2.5.xsd
       http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
       http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd"
>

        
<bean id="UserPo" class="spring.test.hello.UserPo">
            
<property name="username" value="Mizi"></property>
            
<property name="age" value="25"></property>
            
<property name="sex" value="man"></property>
        
</bean>

</beans>

2.6.8 運(yùn)行測(cè)試代碼是否成功

可以右單擊HelloTest.java,在彈出的菜單選擇


又或者點(diǎn)擊Eclipse 工具欄的按鈕


2.6.9  運(yùn)行結(jié)果在console中顯示

如果你顯示的結(jié)果和下圖一致,那么Spring就配置成功了



ok, 今天就到此為止,明天繼續(xù)Stuts


SSH 路還有很長,我剛剛上路。。。。


======================
代碼下載  Spring配置以及測(cè)試代碼