现在的位置: 首页 > 综合 > 正文

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

2012年10月05日 ⁄ 综合 ⁄ 共 2212字 ⁄ 字号 评论关闭

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

 

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

程序的开发环境:eclipse 3.2.2 + WTP + tomcat(推荐)

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

修改WEB-INF/web.xml

 

<?xml version="1.0" encoding="UTF-8"?>

<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">

     

      <display-name>ascweb</display-name>

 

      <!--  载入Spring配置文件 -->

      <context-param>

           <param-name>contextConfigLocation</param-name>

           <param-value>/WEB-INF/applicationContext.xml</param-value><!-- 自动载入的spring配置 -->

      </context-param>

      <listener>

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

      </listener>

 

      <!--  字符编码转换 -->

      <filter>

           <filter-name>encodingFilter</filter-name>

          <filter-class>

org.springframework.web.filter.CharacterEncodingFilter

</filter-class>

           <init-param>

                 <param-name>encoding</param-name>

                 <param-value>UTF-8</param-value>

           </init-param>

      </filter>

      <filter-mapping>

           <filter-name>encodingFilter</filter-name>

           <url-pattern>*.do</url-pattern>

      </filter-mapping>

      <!-- Spring MVC Servlet -->

      <servlet>

           <servlet-name>ascweb</servlet-name>

           <servlet-class>

                 org.springframework.web.servlet.DispatcherServlet

           </servlet-class>

           <load-on-startup>1</load-on-startup>

      </servlet>

 

      <servlet-mapping>

           <servlet-name>ascweb</servlet-name>

           <url-pattern>*.do</url-pattern>

      </servlet-mapping>

 

      <welcome-file-list>

           <welcome-file>index.htm</welcome-file>

           <welcome-file>index.jsp</welcome-file>

           <welcome-file>index.do</welcome-file>

      </welcome-file-list>

</web-app>

 

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

<?xml version="1.0" encoding="UTF-8"?>

<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd">

 

<beans>

<!-- 定义Spring MVC 的模板文件 -->

      <bean id="viewResolver"

抱歉!评论已关闭.