首页 > 代码库 > 2017-5-23 SpringMVC入门(一)

2017-5-23 SpringMVC入门(一)

2017-5-23 SpringMVC入门(一)

SpringMVC
Maven
Java

1.依赖引入

  1. <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
  2. xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> 
  3. <modelVersion>4.0.0</modelVersion> 
  4. <groupId>SpringMvcStart</groupId> 
  5. <artifactId>SpringMvcStart</artifactId> 
  6. <version>0.0.1-SNAPSHOT</version> 
  7. <packaging>war</packaging> 
  8. <name>SpringMvcStart</name> 
  9. <description /> 
  10. <properties> 
  11. <webVersion>3.0</webVersion> 
  12. <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> 
  13. <junit.version>4.12</junit.version> 
  14. <spring.version>4.3.8.RELEASE</spring.version> 
  15. </properties> 
  16. <dependencies> 
  17. <dependency> 
  18. <groupId>org.glassfish</groupId> 
  19. <artifactId>bean-validator</artifactId> 
  20. <version>3.0-JBoss-4.0.2</version> 
  21. <scope>provided</scope> 
  22. </dependency> 
  23. <dependency> 
  24. <groupId>org.glassfish</groupId> 
  25. <artifactId>javax.enterprise.deploy</artifactId> 
  26. <version>3.0.1</version> 
  27. <scope>provided</scope> 
  28. </dependency> 
  29. <dependency> 
  30. <groupId>org.glassfish</groupId> 
  31. <artifactId>javax.jms</artifactId> 
  32. <version>3.0.1</version> 
  33. <scope>provided</scope> 
  34. </dependency> 
  35. <dependency> 
  36. <groupId>org.glassfish</groupId> 
  37. <artifactId>javax.management.j2ee</artifactId> 
  38. <version>3.0.1</version> 
  39. <scope>provided</scope> 
  40. </dependency> 
  41. <dependency> 
  42. <groupId>org.eclipse.persistence</groupId> 
  43. <artifactId>javax.persistence</artifactId> 
  44. <version>2.0.0</version> 
  45. <scope>provided</scope> 
  46. </dependency> 
  47. <dependency> 
  48. <groupId>org.glassfish</groupId> 
  49. <artifactId>javax.resource</artifactId> 
  50. <version>3.0.1</version> 
  51. <scope>provided</scope> 
  52. </dependency> 
  53. <dependency> 
  54. <groupId>org.glassfish</groupId> 
  55. <artifactId>javax.security.auth.message</artifactId> 
  56. <version>3.0.1</version> 
  57. <scope>provided</scope> 
  58. </dependency> 
  59. <dependency> 
  60. <groupId>org.glassfish</groupId> 
  61. <artifactId>javax.security.jacc</artifactId> 
  62. <version>3.0.1</version> 
  63. <scope>provided</scope> 
  64. </dependency> 
  65. <dependency> 
  66. <groupId>org.glassfish</groupId> 
  67. <artifactId>javax.servlet</artifactId> 
  68. <version>3.0.1</version> 
  69. <scope>provided</scope> 
  70. </dependency> 
  71. <dependency> 
  72. <groupId>org.glassfish</groupId> 
  73. <artifactId>javax.servlet.jsp</artifactId> 
  74. <version>3.0.1</version> 
  75. <scope>provided</scope> 
  76. </dependency> 
  77. <dependency> 
  78. <groupId>org.glassfish</groupId> 
  79. <artifactId>javax.servlet.jsp.jstl</artifactId> 
  80. <version>3.0.1</version> 
  81. <scope>provided</scope> 
  82. </dependency> 
  83. <dependency> 
  84. <groupId>javax.xml.bind</groupId> 
  85. <artifactId>jaxb-api-osgi</artifactId> 
  86. <version>2.2.1</version> 
  87. <scope>provided</scope> 
  88. </dependency> 
  89. <dependency> 
  90. <groupId>javax.ws.rs</groupId> 
  91. <artifactId>jsr311-api</artifactId> 
  92. <version>1.1.1</version> 
  93. <scope>provided</scope> 
  94. </dependency> 
  95. <dependency> 
  96. <groupId>org.glassfish.web</groupId> 
  97. <artifactId>jstl-impl</artifactId> 
  98. <version>1.2</version> 
  99. <scope>provided</scope> 
  100. </dependency> 
  101. <dependency> 
  102. <groupId>javax.mail</groupId> 
  103. <artifactId>mail</artifactId> 
  104. <version>1.4.3</version> 
  105. <scope>provided</scope> 
  106. </dependency> 
  107. <dependency> 
  108. <groupId>javax.xml</groupId> 
  109. <artifactId>webservices-api-osgi</artifactId> 
  110. <version>2.0.1</version> 
  111. <scope>provided</scope> 
  112. </dependency> 
  113. <dependency> 
  114. <groupId>org.jboss.weld</groupId> 
  115. <artifactId>weld-osgi-bundle</artifactId> 
  116. <version>1.0.1-SP3</version> 
  117. <scope>provided</scope> 
  118. </dependency> 
  119. <dependency> 
  120. <groupId>org.glassfish.web</groupId> 
  121. <artifactId>javax.servlet.jsp.jstl</artifactId> 
  122. <version>1.2.1</version> 
  123. </dependency> 
  124. <!-- https://mvnrepository.com/artifact/commons-logging/commons-logging --> 
  125. <dependency> 
  126. <groupId>commons-logging</groupId> 
  127. <artifactId>commons-logging</artifactId> 
  128. <version>1.2</version> 
  129. </dependency> 
  130.  
  131. <!-- https://mvnrepository.com/artifact/junit/junit --> 
  132. <dependency> 
  133. <groupId>junit</groupId> 
  134. <artifactId>junit</artifactId> 
  135. <version>${junit.version}</version> 
  136. </dependency> 
  137. <!-- https://mvnrepository.com/artifact/org.springframework/spring-context --> 
  138. <dependency> 
  139. <groupId>org.springframework</groupId> 
  140. <artifactId>spring-context</artifactId> 
  141. <version>${spring.version}</version> 
  142. </dependency> 
  143. <!-- https://mvnrepository.com/artifact/org.springframework/spring-web --> 
  144. <dependency> 
  145. <groupId>org.springframework</groupId> 
  146. <artifactId>spring-web</artifactId> 
  147. <version>${spring.version}</version> 
  148. </dependency> 
  149. <!-- https://mvnrepository.com/artifact/org.springframework/spring-core --> 
  150. <dependency> 
  151. <groupId>org.springframework</groupId> 
  152. <artifactId>spring-core</artifactId> 
  153. <version>${spring.version}</version> 
  154. </dependency> 
  155. <!-- https://mvnrepository.com/artifact/org.springframework/spring-webmvc --> 
  156. <dependency> 
  157. <groupId>org.springframework</groupId> 
  158. <artifactId>spring-webmvc</artifactId> 
  159. <version>${spring.version}</version> 
  160. </dependency> 
  161. <!-- https://mvnrepository.com/artifact/org.springframework/spring-webmvc-portlet --> 
  162. <dependency> 
  163. <groupId>org.springframework</groupId> 
  164. <artifactId>spring-webmvc-portlet</artifactId> 
  165. <version>${spring.version}</version> 
  166. </dependency> 
  167. <!-- https://mvnrepository.com/artifact/org.springframework/spring-websocket --> 
  168. <dependency> 
  169. <groupId>org.springframework</groupId> 
  170. <artifactId>spring-websocket</artifactId> 
  171. <version>${spring.version}</version> 
  172. </dependency> 
  173. <!-- https://mvnrepository.com/artifact/org.springframework/spring-tx --> 
  174. <dependency> 
  175. <groupId>org.springframework</groupId> 
  176. <artifactId>spring-tx</artifactId> 
  177. <version>${spring.version}</version> 
  178. </dependency> 
  179. <!-- https://mvnrepository.com/artifact/org.springframework/spring-aspects --> 
  180. <dependency> 
  181. <groupId>org.springframework</groupId> 
  182. <artifactId>spring-aspects</artifactId> 
  183. <version>${spring.version}</version> 
  184. </dependency> 
  185. <!-- https://mvnrepository.com/artifact/org.springframework/spring-context-support --> 
  186. <dependency> 
  187. <groupId>org.springframework</groupId> 
  188. <artifactId>spring-context-support</artifactId> 
  189. <version>${spring.version}</version> 
  190. </dependency> 
  191. <!-- https://mvnrepository.com/artifact/org.springframework/spring-instrument --> 
  192. <dependency> 
  193. <groupId>org.springframework</groupId> 
  194. <artifactId>spring-instrument</artifactId> 
  195. <version>${spring.version}</version> 
  196. </dependency> 
  197. <!-- https://mvnrepository.com/artifact/org.springframework/spring-instrument-tomcat --> 
  198. <dependency> 
  199. <groupId>org.springframework</groupId> 
  200. <artifactId>spring-instrument-tomcat</artifactId> 
  201. <version>${spring.version}</version> 
  202. </dependency> 
  203. <!-- https://mvnrepository.com/artifact/org.springframework/spring-jdbc --> 
  204. <dependency> 
  205. <groupId>org.springframework</groupId> 
  206. <artifactId>spring-jdbc</artifactId> 
  207. <version>${spring.version}</version> 
  208. </dependency> 
  209. <!-- https://mvnrepository.com/artifact/org.springframework/spring-jms --> 
  210. <dependency> 
  211. <groupId>org.springframework</groupId> 
  212. <artifactId>spring-jms</artifactId> 
  213. <version>${spring.version}</version> 
  214. </dependency> 
  215. <!-- https://mvnrepository.com/artifact/org.springframework/spring-messaging --> 
  216. <dependency> 
  217. <groupId>org.springframework</groupId> 
  218. <artifactId>spring-messaging</artifactId> 
  219. <version>${spring.version}</version> 
  220. </dependency> 
  221. <!-- https://mvnrepository.com/artifact/org.springframework/spring-orm --> 
  222. <dependency> 
  223. <groupId>org.springframework</groupId> 
  224. <artifactId>spring-orm</artifactId> 
  225. <version>${spring.version}</version> 
  226. </dependency> 
  227. <!-- https://mvnrepository.com/artifact/org.springframework/spring-oxm --> 
  228. <dependency> 
  229. <groupId>org.springframework</groupId> 
  230. <artifactId>spring-oxm</artifactId> 
  231. <version>${spring.version}</version> 
  232. </dependency> 
  233. <!-- https://mvnrepository.com/artifact/org.springframework/spring-test --> 
  234. <dependency> 
  235. <groupId>org.springframework</groupId> 
  236. <artifactId>spring-test</artifactId> 
  237. <version>${spring.version}</version> 
  238. </dependency> 
  239.  
  240. </dependencies> 
  241. <build> 
  242. <plugins> 
  243. <plugin> 
  244. <artifactId>maven-compiler-plugin</artifactId> 
  245. <version>2.3.2</version> 
  246. <configuration> 
  247. <source>1.8</source> 
  248. <target>1.8</target> 
  249. </configuration> 
  250. </plugin> 
  251. <plugin> 
  252. <artifactId>maven-war-plugin</artifactId> 
  253. <version>2.6</version> 
  254. <configuration> 
  255. <failOnMissingWebXml>false</failOnMissingWebXml> 
  256. </configuration> 
  257. </plugin> 
  258. </plugins> 
  259. </build> 
  260. </project> 

2.web.xml配置

  1. <?xml version="1.0" encoding="UTF-8"?> 
  2. <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0"> 
  3. <display-name>SpringMvcStart</display-name> 
  4. <welcome-file-list> 
  5. <welcome-file>index.html</welcome-file> 
  6. <welcome-file>index.htm</welcome-file> 
  7. <welcome-file>index.jsp</welcome-file> 
  8. <welcome-file>default.html</welcome-file> 
  9. <welcome-file>default.htm</welcome-file> 
  10. <welcome-file>default.jsp</welcome-file> 
  11. </welcome-file-list> 
  12. <servlet> 
  13. <servlet-name>springmvc</servlet-name> 
  14. <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> 
  15. <init-param> 
  16. <param-name>contextConfigLocation</param-name> 
  17. <param-value>/WEB-INF/springmvc-config.xml</param-value> 
  18. </init-param> 
  19. <load-on-startup>1</load-on-startup> 
  20. </servlet> 
  21. <servlet-mapping> 
  22. <servlet-name>springmvc</servlet-name> 
  23. <url-pattern>/</url-pattern> 
  24. </servlet-mapping> 
  25. <listener> 
  26. <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> 
  27. </listener> 
  28. <context-param> 
  29. <param-name>contextConfigLocation</param-name> 
  30. <param-value>classpath:applicationContext.xml</param-value> 
  31. </context-param> 
  32. </web-app> 

3.springmvc-config.xml配置

  1. <?xml version="1.0" encoding="UTF-8"?> 
  2. <beans xmlns="http://www.springframework.org/schema/beans" 
  3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
  4. xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.1.xsd"> 
  5.  
  6. <!-- 配置Handle,映射“/hello请求” --> 
  7. <bean name="/hello" class="com.huawei.l00379880.controller.HelloController"></bean> 
  8. <!-- 处理器映射器将bean的name作为url进行查找,需要在配置Handle时指定name,(即url) --> 
  9. <bean class="org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping"></bean> 
  10. <!-- SimpleControllerHandlerAdapter是一个处理器适配器,所有处理器适配器都要实现HandlerAdapter接口 --> 
  11. <bean class="org.springframework.web.servlet.mvc.SimpleControllerHandlerAdapter"></bean> 
  12. <!-- 视图解析器 --> 
  13. <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"></bean> 
  14. </beans> 

2017-5-23 SpringMVC入门(一)