歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
您现在的位置: Linux教程網 >> UnixLinux >  >> Linux編程 >> Linux編程

Spring MVC 注解-讓Spring跑起來

 (1) 導入包,包結構如下圖所示:


        (2) 配置web.xml,如下所示:

  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
  3.     xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"  
  4.     xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"  
  5.     id="WebApp_ID" version="2.5">  
  6.     <display-name>spring_test</display-name>  
  7.   
  8.     <!-- 配置文件位置,默認為/WEB-INF/applicationContext.xml -->  
  9.     <context-param>  
  10.         <param-name>contextConfigLocation</param-name>  
  11.         <param-value>classpath:applicationContext.xml</param-value>  
  12.     </context-param>  
  13.   
  14.     <!-- 字符集過濾器 -->  
  15.     <filter>  
  16.         <filter-name>characterEncodingFilter</filter-name>  
  17.         <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>  
  18.         <init-param>  
  19.             <param-name>encoding</param-name>  
  20.             <param-value>UTF-8</param-value>  
  21.         </init-param>  
  22.     </filter>  
  23.     <filter-mapping>  
  24.         <filter-name>characterEncodingFilter</filter-name>  
  25.         <url-pattern>/*</url-pattern>  
  26.     </filter-mapping>  
  27.       
  28.     <!-- 上下文Spring監聽器 -->  
  29.     <listener>  
  30.         <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>  
  31.     </listener>  
  32.       
  33.     <!-- servlet控制跳轉 -->  
  34.     <servlet>  
  35.         <servlet-name>spring</servlet-name>  
  36.         <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>  
  37.         <!-- 配置文件 -->  
  38.         <init-param>  
  39.             <param-name>contextConfigLocation</param-name>  
  40.             <param-value>classpath:context-dispatcher.xml</param-value>  
  41.         </init-param>  
  42.     </servlet>  
  43.     <servlet-mapping>  
  44.         <servlet-name>spring</servlet-name>  
  45.         <url-pattern>*.html</url-pattern>  
  46.     </servlet-mapping>  
  47.   
  48.     <welcome-file-list>  
  49.         <welcome-file>index.html</welcome-file>  
  50.         <welcome-file>index.htm</welcome-file>  
  51.         <welcome-file>index.jsp</welcome-file>  
  52.         <welcome-file>default.html</welcome-file>  
  53.         <welcome-file>default.htm</welcome-file>  
  54.         <welcome-file>default.jsp</welcome-file>  
  55.     </welcome-file-list>  
  56. </web-app>  
        (3) 配置dispatcher文件src/context-dispatcher.xml
  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <beans default-lazy-init="true"  
  3.     xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
  4.     xmlns:context="http://www.springframework.org/schema/context"  
  5.     xmlns:mvc="http://www.springframework.org/schema/mvc"  
  6.     xsi:schemaLocation="  
  7.        http://www.springframework.org/schema/beans    
  8.        http://www.springframework.org/schema/beans/spring-beans-3.0.xsd   
  9.        http://www.springframework.org/schema/mvc    
  10.        http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd    
  11.        http://www.springframework.org/schema/context   
  12.        http://www.springframework.org/schema/context/spring-context-3.0.xsd">   
  13.   
  14.     <!-- 使用注解的包,包括子集 -->  
  15.     <context:component-scan base-package="com.geloin.spring" />  
  16.     <!-- 通過注解,把URL映射到Controller上,該標簽默認注冊DefaultAnnotationHandlerMapping和AnnotationMethodHandlerAdapter -->  
  17.     <mvc:annotation-driven />  
  18.     <!-- 視圖解析器 -->  
  19.     <bean id="viewResolver"  
  20.         class="org.springframework.web.servlet.view.InternalResourceViewResolver">  
  21.         <property name="viewClass"  
  22.             value="org.springframework.web.servlet.view.JstlView" />  
  23.         <property name="prefix" value="/WEB-INF/pages/" />  
  24.         <property name="suffix" value=".jsp"></property>  
  25.     </bean>  
  26. </beans>  
Copyright © Linux教程網 All Rights Reserved