SpringMVC项目配置
一、创建一个maven项目
1、new一个maven项目,选择next,如图:
2、勾选Create a simple project(skip archetype selection)
3、填写Group Id(项目的唯一标识)和Artidact Id(项目名称),选择Packaging为war,然后直接Finish。
4、当前该项目的结构如下:
5、在webapp下新建文件夹WEB-INF
二、添加并编写相关配置文件
1、在WEB-INF下新建web.xml文件
2、编写web.xml内容
1 <?xml version="1.0" encoding="UTF-8"?> 2 <web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee" 3 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 4 xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"> 5 6 <!-- 配置监听器 --> 7 <listener> 8 <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> 9 </listener> 10 <listener> 11 <listener-class>org.springframework.web.util.IntrospectorCleanupListener</listener-class> 12 </listener> 13 14 <!-- 配置过滤器,解决POST乱码问题 --> 15 <filter> 16 <filter-name>encoding</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>encoding</filter-name> 25 <url-pattern>/*</url-pattern> 26 </filter-mapping> 27 28 <!-- 配置SpringMVC分发器,拦截所有请求 --> 29 <servlet> 30 <servlet-name>springmvc</servlet-name> 31 <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> 32 <init-param> 33 <param-name>namespace</param-name> 34 <param-value>spring-servlet</param-value> 35 </init-param> 36 <load-on-startup>1</load-on-startup> 37 </servlet> 38 <servlet-mapping> 39 <servlet-name>springmvc</servlet-name> 40 <url-pattern>/</url-pattern> 41 </servlet-mapping> 42 </web-app>
在这个配置中,我们规定了 DispatcherServlet 的关联 XML 文件名称叫做 spring-servlet。注意,这里的路径是相对于web.xml来说的,也就是说,这个文件也在WEB-INF的根目录下。所以,我们需要在WEB-INF的根目录下新建一个spring-servlet.xml文件。
3、spring-servlet.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" xmlns:p="http://www.springframework.org/schema/p" 4 xmlns:context="http://www.springframework.org/schema/context" 5 xmlns:util="http://www.springframework.org/schema/util" xmlns:mvc="http://www.springframework.org/schema/mvc" 6 xsi:schemaLocation="http://www.springframework.org/schema/beans 7 http://www.springframework.org/schema/beans/spring-beans-3.0.xsd 8 http://www.springframework.org/schema/context 9 http://www.springframework.org/schema/context/spring-context-3.0.xsd 10 http://www.springframework.org/schema/util 11 http://www.springframework.org/schema/util/spring-util-3.0.xsd 12 http://www.springframework.org/schema/mvc 13 http://www.springframework.org/schema/mvc/spring-mvc.xsd"> 14 15 <!-- 开启注解模式驱动 --> 16 <mvc:annotation-driven></mvc:annotation-driven> 17 <!-- 扫描包 --> 18 <context:component-scan base-package="com.springmvc.*"></context:component-scan> 19 <!-- 静态资源过滤 --> 20 <mvc:resources location="/resources/" mapping="/resources/**" /> 21 <!-- 静态资源重新映射 不加则以.html和.ftl结尾的页面都报404错误,只有.jsp才能正常显示 --> 22 <mvc:resources mapping="/WEB-INF/view/**" location="/WEB-INF/view/" /> 23 <!-- 视图渲染 --> 24 <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> 25 <!-- 制定页面存放的路径 --> 26 <property name="prefix" value="/WEB-INF/view/"></property> 27 <!-- 文件的后缀 --> 28 <property name="suffix" value=".html"></property> 29 </bean> 30 31 </beans>
根据配置,有三个需要注意的地方。
(1)它会扫描 com.springmvc 包下所有的Java类,但凡是遇到有注解的,比如@Controller , @Service , @Autowired ,就会将它们加入到Spring的bean工厂里面去。
(2)所有的静态资源文件,比如说 js , css , images 都需要放在/resources目录下。
(3)所有的展示页面,比如jsp文件,都需要放置在/WEB-INF/view目录下。
4、创建相关目录后项目结构如下:
5、添加applicationContext.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" xmlns:aop="http://www.springframework.org/schema/aop" 4 xmlns:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx" 5 xmlns:util="http://www.springframework.org/schema/util" xmlns:p="http://www.springframework.org/schema/p" 6 xsi:schemaLocation="http://www.springframework.org/schema/beans 7 http://www.springframework.org/schema/beans/spring-beans.xsd 8 http://www.springframework.org/schema/aop 9 http://www.springframework.org/schema/aop/spring-aop-4.0.xsd 10 http://www.springframework.org/schema/tx 11 http://www.springframework.org/schema/tx/spring-tx-4.0.xsd 12 http://www.springframework.org/schema/context 13 http://www.springframework.org/schema/context/spring-context-4.0.xsd 14 http://www.springframework.org/schema/util 15 http://www.springframework.org/schema/util/spring-util-4.0.xsd 16 "> 17 18 </beans>
6、导入jar包
1 <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> 2 <modelVersion>4.0.0</modelVersion> 3 <groupId>com.cn</groupId> 4 <artifactId>SpringMVC</artifactId> 5 <version>0.0.1-SNAPSHOT</version> 6 <packaging>war</packaging> 7 <dependencies> 8 <!-- springmvc --> 9 <dependency> 10 <groupId>org.springframework</groupId> 11 <artifactId>spring-beans</artifactId> 12 <version>4.3.2.RELEASE</version> 13 </dependency> 14 <dependency> 15 <groupId>org.springframework</groupId> 16 <artifactId>spring-core</artifactId> 17 <version>4.3.2.RELEASE</version> 18 </dependency> 19 <dependency> 20 <groupId>org.springframework</groupId> 21 <artifactId>spring-jdbc</artifactId> 22 <version>4.3.2.RELEASE</version> 23 </dependency> 24 <dependency> 25 <groupId>org.springframework</groupId> 26 <artifactId>spring-context-support</artifactId> 27 <version>4.3.2.RELEASE</version> 28 </dependency> 29 <dependency> 30 <groupId>org.springframework</groupId> 31 <artifactId>spring-context</artifactId> 32 <version>4.3.2.RELEASE</version> 33 <exclusions> 34 <!-- Exclude Commons Logging in favor of SLF4j --> 35 <exclusion> 36 <groupId>commons-logging</groupId> 37 <artifactId>commons-logging</artifactId> 38 </exclusion> 39 </exclusions> 40 </dependency> 41 <dependency> 42 <groupId>org.springframework</groupId> 43 <artifactId>spring-expression</artifactId> 44 <version>4.3.2.RELEASE</version> 45 </dependency> 46 <dependency> 47 <groupId>org.springframework</groupId> 48 <artifactId>spring-webmvc</artifactId> 49 <version>4.3.2.RELEASE</version> 50 </dependency> 51 <dependency> 52 <groupId>org.springframework</groupId> 53 <artifactId>spring-web</artifactId> 54 <version>4.3.2.RELEASE</version> 55 </dependency> 56 <dependency> 57 <groupId>org.springframework</groupId> 58 <artifactId>spring-tx</artifactId> 59 <version>4.3.2.RELEASE</version> 60 </dependency> 61 </dependencies> 62 </project>
三、测试
a)新增一个Controller
1 package com.springmvc.controller; 2 3 import org.springframework.stereotype.Controller; 4 import org.springframework.web.bind.annotation.RequestMapping; 5 6 @Controller 7 public class HelloController { 8 9 @RequestMapping("hello") 10 public String hello(){ 11 System.out.println("hello"); 12 return "helloMVC"; 13 } 14 }
b)在view里新增一个简单helloMVC.html
1 <!DOCTYPE html> 2 <html> 3 <head> 4 <meta charset="UTF-8"> 5 <title>Insert title here</title> 6 </head> 7 <body> 8 <h1>hello MVC</h1> 9 </body> 10 </html
c)把该项目加载到tomcat
d)启动项目,不报错。在地址栏输入:http://localhost:8080/SpringMVC/hello,结果如下: