米子SSH之路(二)SSH的配置(1)Spring2.5

在这里我选择的IDE是原版的eclipse, 插件我也只装了 Tomcat的插件,而没有选择用MyEclipse.

为什么我没选择用MyEclipse?

1. 之前做项目的时候,用过MyEclipse,但是在我的机器老死,而且速度慢,也许可能是我的机子差 的原因,不知道大家有没有类似的问题。后来换了Eclipse3.4原版 问题都没了,用起来感觉还蛮不错的 。

2. 不得不说Eclipse太强大了,它的插件无所不能。但是我在这里想对和我一样的初学者来说,不论 你学习什么语言,不要一上手就用这些高度集成的IDE开发工具,特别是那些可视化IDE(比如MS的VS系列 )。高度集成的IDE只适合开发或高手用。在业内有句话是这么说的,只能让菜鸟更菜鸟,高手更高手。

3. 为什么没用NetBean?在我的眼里那是高手用的,这个时段的我略过,而我用Eclipse + Tomcat 也 只是图编译比较方便而已,其他功能基本上无视。

一,在eclipse创建一个项目叫 miziStudy

1.1 New->Project

1.2 在弹出窗口中选择 Web->Dynamic Web Project, 然后Next

1.3  在此窗口设置你的项目名字,项目存放路径以及tomcat版本

1.4 直接finish掉,后面的设置按照默认设置,当然你如果有兴趣也可以去看看。

ok, finish后 你点eclipse右边面板看到你新建的项目。

1.5, 项目创建完毕,现在就开始我们的SSH之路吧。

今天在这里讲讲SSH配置,我网络上搜了好久,大家都讲的很简单,或者只是稍微提下,又或者把配置 文件贴上来就了事了,但是殊不知这对像我这样的菜鸟来说,特别是没有一点java基础的来说,是一件让 人很头痛的事。也许是我比较傻也不一定,整整用了半个月的时间才弄懂怎么配置。(当然不包括上班时 间)。

二,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 ] =>用来设定web站台的环境参数* [ <param-name></param-name> ] (子元素)=> 用来指定参数的名称* [ <param-value></param-value> ] (子元素)=> 用来设定参数值* ************* 从类路径下加载spring的配置文件, 多个配置文件可以用逗号和空格区分* classpath: 关键字特指类路径下加载********************--><context-param><param-name>contextConfigLocation</param-name><param-value>classpath:applicationContext*.xml</param-value></context-param><!--* [<listener></listener>]=>用来设定监听接口* [<listener-class></listener-class>](子元素)=>定义Listener的类名称* ******** 负责启动spring的监听器* 它将引用处的上下文参数获得spring配置文件地址* 指定Spring提供的ContextLoaderListener Web 容器监听器,* 该监听器在web容器启动时自动运行并且根据ContextLoaderListener参数* 获取Spring配置文件,并启动Spring容器。注意要将log4j.propertis放在* 类目录下,以备目录引擎自动声效************** --><listener><listener-class>org.springframework.web.context.ContextLoaderListener</listener-class></listener> </web-app>

2.3,在src下面新建 applicationContext.xml

2.4 给applicationContext.xml 加上spring的标头 (注意结束符号)

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

注意标头是2.5的 不要引入2.0, 错了可能Spring就不能正确加载。

2.5 引入spring2.5的包。

spring功能太强大,我们初学用不了那么多,所以只要引入我们需要的包就可以了。需要的时候再添 加。

把包复制到 /WEB-INFO/lib 文件夹下

2.6 测试Spring是否配置成功

2.6.1 在src下面我们新建一个包spring.test.hello

(右单击src文件夹,new -> package )

2.6.2 在spring.test.hello下面新建两个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; //岁数    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"?><img alt="" width="11" height="16" src="http://img.68idc.cn/uploadfile/allimg/150625/120G13539-0.gif" /><beans xmlns="http://www.springframework.org/schema/beans"<img alt="" width="11" height="16" src="http://img.68idc.cn/uploadfile/allimg/150625/120G13539-0.gif" />       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"<img alt="" width="11" height="16" src="http://img.68idc.cn/uploadfile/allimg/150625/120G13539-0.gif" />       xmlns:p="http://www.springframework.org/schema/p"<img alt="" width="11" height="16" src="http://img.68idc.cn/uploadfile/allimg/150625/120G13539-0.gif" />       xmlns:aop="http://www.springframework.org/schema/aop"<img alt="" width="11" height="16" src="http://img.68idc.cn/uploadfile/allimg/150625/120G13539-0.gif" />       xmlns:tx="http://www.springframework.org/schema/tx"<img alt="" width="11" height="16" src="http://img.68idc.cn/uploadfile/allimg/150625/120G13539-0.gif" />       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd<img alt="" width="11" height="16" src="http://img.68idc.cn/uploadfile/allimg/150625/120G13539-0.gif" />       http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd<img alt="" width="11" height="16" src="http://img.68idc.cn/uploadfile/allimg/150625/120G13539-0.gif" />       http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd"><img alt="" width="11" height="16" src="http://img.68idc.cn/uploadfile/allimg/150625/120G13539-0.gif" /><img alt="" width="11" height="16" src="http://img.68idc.cn/uploadfile/allimg/150625/120G13539-0.gif" />        <bean id="UserPo" class="spring.test.hello.UserPo"><img alt="" width="11" height="16" src="http://img.68idc.cn/uploadfile/allimg/150625/120G13539-0.gif" />            <property name="username" value="Mizi"></property><img alt="" width="11" height="16" src="http://img.68idc.cn/uploadfile/allimg/150625/120G13539-0.gif" />            <property name="age" value="25"></property><img alt="" width="11" height="16" src="http://img.68idc.cn/uploadfile/allimg/150625/120G13539-0.gif" />            <property name="sex" value="man"></property><img alt="" width="11" height="16" src="http://img.68idc.cn/uploadfile/allimg/150625/120G13539-0.gif" />        </bean><img alt="" width="11" height="16" src="http://img.68idc.cn/uploadfile/allimg/150625/120G13539-0.gif" /><img alt="" width="11" height="16" src="http://img.68idc.cn/uploadfile/allimg/150625/120G13539-0.gif" /></beans>

2.6.8 运行测试代码是否成功

可以右单击HelloTest.java,在弹出的菜单选择

又或者点击Eclipse 工具栏的按钮

2.6.9  运行结果在console中显示

如果你显示的结果和下图一致,那么Spring就配置成功了

ok, 今天就到此为止,明天继续Stuts

本文配套源码:http://www.bianceng.net/java/201212/825.htm

要永不言弃坚持到底百折不挠宁死不屈,但我们好多人没想过,

米子SSH之路(二)SSH的配置(1)Spring2.5

相关文章:

你感兴趣的文章:

标签云: