歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux編程 >> Linux編程 >> Spring MVC 注解-讓Spring跑起來

Spring MVC 注解-讓Spring跑起來

日期:2017/3/1 10:21:16   编辑:Linux編程
(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. <!-- 配置文件位置,默認為/WEB-INF/applicationContext.xml -->
  8. <context-param>
  9. <param-name>contextConfigLocation</param-name>
  10. <param-value>classpath:applicationContext.xml</param-value>
  11. </context-param>
  12. <!-- 字符集過濾器 -->
  13. <filter>
  14. <filter-name>characterEncodingFilter</filter-name>
  15. <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
  16. <init-param>
  17. <param-name>encoding</param-name>
  18. <param-value>UTF-8</param-value>
  19. </init-param>
  20. </filter>
  21. <filter-mapping>
  22. <filter-name>characterEncodingFilter</filter-name>
  23. <url-pattern>/*</url-pattern>
  24. </filter-mapping>
  25. <!-- 上下文Spring監聽器 -->
  26. <listener>
  27. <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
  28. </listener>
  29. <!-- servlet控制跳轉 -->
  30. <servlet>
  31. <servlet-name>spring</servlet-name>
  32. <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
  33. <!-- 配置文件 -->
  34. <init-param>
  35. <param-name>contextConfigLocation</param-name>
  36. <param-value>classpath:context-dispatcher.xml</param-value>
  37. </init-param>
  38. </servlet>
  39. <servlet-mapping>
  40. <servlet-name>spring</servlet-name>
  41. <url-pattern>*.html</url-pattern>
  42. </servlet-mapping>
  43. <welcome-file-list>
  44. <welcome-file>index.html</welcome-file>
  45. <welcome-file>index.htm</welcome-file>
  46. <welcome-file>index.jsp</welcome-file>
  47. <welcome-file>default.html</welcome-file>
  48. <welcome-file>default.htm</welcome-file>
  49. <welcome-file>default.jsp</welcome-file>
  50. </welcome-file-list>
  51. </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. <context:component-scan base-package="com.geloin.spring" />
  15. <!-- 通過注解,把URL映射到Controller上,該標簽默認注冊DefaultAnnotationHandlerMapping和AnnotationMethodHandlerAdapter -->
  16. <mvc:annotation-driven />
  17. <!-- 視圖解析器 -->
  18. <bean id="viewResolver"
  19. class="org.springframework.web.servlet.view.InternalResourceViewResolver">
  20. <property name="viewClass"
  21. value="org.springframework.web.servlet.view.JstlView" />
  22. <property name="prefix" value="/WEB-INF/pages/" />
  23. <property name="suffix" value=".jsp"></property>
  24. </bean>
  25. </beans>
Copyright © Linux教程網 All Rights Reserved