---
title: "使用Spring 2.5 和 Hibernate 3.2 开发MVC Web程序(基于annotation特性)"
author: "eicesoft"
date: "2008-01-23T17:17:00Z"
tags: []
slug: "spring-2-5-hibernate-3-2-mvc-web-annotation"
draft: false
lastmod: "2008-01-23T17:17:00Z"
id: 674952
url: "https://eicesoft.iteye.com/blog/674952"
---

使用Spring 2.5 和 Hibernate 3.2 开发MVC Web程序(基于annotation特性)

&lt;?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" /&gt;

 

Spring 和 Hibernate 都支持annotation开发应用程序,无疑减少呢很多XML配置,使程序看起来更”干净”,下面我们以一个简单的例子来说明:

程序的开发环境:eclipse &lt;?xml:namespace prefix = st1 ns = "urn:schemas-microsoft-com:office:smarttags" /&gt;&lt;chsdate w:st="on" isrocdate="False" islunardate="False" day="30" month="12" year="1899"&gt;3.2.2&lt;/chsdate&gt; + WTP + tomcat(推荐)

首先我们建一个动态的Web项目

修改WEB-INF/web.xml

 

&lt;?xml version="1.0" encoding="UTF-8"?&gt;

&lt;web-app id="ascweb" 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"&gt;

     

      &lt;display-name&gt;ascweb&lt;/display-name&gt;

 

      &lt;!--  载入Spring配置文件 --&gt;

      &lt;context-param&gt;

           &lt;param-name&gt;contextConfigLocation&lt;/param-name&gt;

           &lt;param-value&gt;/WEB-INF/applicationContext.xml&lt;/param-value&gt;&lt;!-- 自动载入的spring配置 --&gt;

      &lt;/context-param&gt;

      &lt;listener&gt;

      &lt;listener-class&gt;org.springframework.web.context.ContextLoaderListener&lt;/listener-class&gt;

      &lt;/listener&gt;

 

      &lt;!--  字符编码转换 --&gt;

      &lt;filter&gt;

           &lt;filter-name&gt;encodingFilter&lt;/filter-name&gt;

          &lt;filter-class&gt;

org.springframework.web.filter.CharacterEncodingFilter

&lt;/filter-class&gt;

           &lt;init-param&gt;

                 &lt;param-name&gt;encoding&lt;/param-name&gt;

                 &lt;param-value&gt;UTF-8&lt;/param-value&gt;

           &lt;/init-param&gt;

      &lt;/filter&gt;

      &lt;filter-mapping&gt;

           &lt;filter-name&gt;encodingFilter&lt;/filter-name&gt;

           &lt;url-pattern&gt;\*.do&lt;/url-pattern&gt;

      &lt;/filter-mapping&gt;

      &lt;!-- Spring MVC 的Servlet --&gt;

      &lt;servlet&gt;

           &lt;servlet-name&gt;ascweb&lt;/servlet-name&gt;

           &lt;servlet-class&gt;

                 org.springframework.web.servlet.DispatcherServlet

           &lt;/servlet-class&gt;

           &lt;load-on-startup&gt;1&lt;/load-on-startup&gt;

      &lt;/servlet&gt;

 

      &lt;servlet-mapping&gt;

           &lt;servlet-name&gt;ascweb&lt;/servlet-name&gt;

           &lt;url-pattern&gt;\*.do&lt;/url-pattern&gt;

      &lt;/servlet-mapping&gt;

 

      &lt;welcome-file-list&gt;

           &lt;welcome-file&gt;index.htm&lt;/welcome-file&gt;

           &lt;welcome-file&gt;index.jsp&lt;/welcome-file&gt;

           &lt;welcome-file&gt;index.do&lt;/welcome-file&gt;

      &lt;/welcome-file-list&gt;

&lt;/web-app&gt;

 

建立好这个文件之后,我们在WEB-INF目录下面建一个ascweb-servlet.xml文件,其实这个文件的命名就是Web.xml中servlet-name的名字加-servlet.xml.其文件内容如下:

&lt;?xml version="1.0" encoding="UTF-8"?&gt;

&lt;!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd"&gt;

 

&lt;beans&gt;

&lt;!-- 定义Spring MVC 的模板文件 --&gt;

      &lt;bean id="viewResolver"

      class="org.springframework.web.servlet.view.InternalResourceViewResolver"&gt;

           &lt;property name="viewClass" value="org.springframework.web.servlet.view.JstlView"/&gt;     &lt;!-- 支持JSTL --&gt;

           &lt;property name="prefix" value="WEB-INF/views/" /&gt;     &lt;!-- 模板的路径 --&gt;

           &lt;property name="suffix" value=".jsp" /&gt;                 &lt;!-- 模板文件的后缀--&gt;

      &lt;/bean&gt;

 

      &lt;!-- 依赖注入到HelloController这个类 --&gt;

      &lt;bean id="helloController" class="com.asc.web.ctl.HelloController"&gt;

           &lt;property name="viewName"&gt;  &lt;!-- 设定viewName属性的值为hello --&gt;

                 &lt;value&gt;hello&lt;/value&gt;

           &lt;/property&gt;

           &lt;property name="userDao"&gt;     &lt;!-- 设置属性userDao为Hibernate的Dao对象 --&gt;

                 &lt;ref bean="userDAOProxy" /&gt; &lt;!-- 注入一个bean(定义在Hibernate的配置文件里) --&gt;

           &lt;/property&gt;

      &lt;/bean&gt;

&lt;/beans&gt;

 

然后我们建立一个/WEB-INF/applicationContext.xml文件,其内容为:

&lt;?xml version="1.0" encoding="UTF-8"?&gt;

&lt;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-2.0.xsd"&gt;

      &lt;-- 数据源配置 --&gt;

      &lt;bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close"&gt;

           &lt;property name="driverClassName"&gt;

                 &lt;value&gt;com.microsoft.sqlserver.jdbc.SQLServerDriver&lt;/value&gt;

            &lt;/property&gt;

           &lt;property name="url"&gt;

                 &lt;value&gt;jdbc:sqlserver://192.168.1.21:2433;DatabaseName=expert&lt;/value&gt;

           &lt;/property&gt;

           &lt;property name="username"&gt;