在上文配置DispatcherServlet中,已经了解到了,Spring MVC项目的基本原理,现在,基于此,搭建一个简单的Spring MVC项目。


首先,配置web.xml:(这些配置的意思以及为什么要这么配置,在配置DispatcherServlet中,已经说明清楚了)

<web-app version="3.0" xmlns="http://java.sun/xml/ns/javaee"
	xmlns:xsi="http://www.w3/2001/XMLSchema-instance"
	xsi:schemaLocation="http://java.sun/xml/ns/javaee 
    http://java.sun/xml/ns/javaee/web-app_3_0.xsd">
	<display-name>spring-mvc-stuty</display-name>
	
	<servlet>
		<servlet-name>springmvc</servlet-name>
		<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
		<init-param>
			<param-name>contextConfigLocation</param-name>
			<param-value>/WEB-INF/springmvc-servlet.xml</param-value>
		</init-param>
		<load-on-startup>1</load-on-startup>
	</servlet>
	<servlet-mapping>
		<servlet-name>springmvc</servlet-name>
		<url-pattern>/app/*</url-pattern>
	</servlet-mapping>
	
</web-app>

然后是在WEB-INF目录下,新建一个springmvc-servlet.xml配置文件:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework/schema/beans"
    xmlns:mvc="http://www.springframework/schema/mvc"
    xmlns:xsi="http://www.w3/2001/XMLSchema-instance"
    xmlns:context="http://www.springframework/schema/context"
    xsi:schemaLocation="
        http://www.springframework/schema/beans
        https://www.springframework/schema/beans/spring-beans.xsd
        http://www.springframework/schema/context
        https://www.springframework/schema/context/spring-context.xsd
        http://www.springframework/schema/mvc
        https://www.springframework/schema/mvc/spring-mvc.xsd">
        
	<!-- 多个包名用逗号隔开, 但不能有空格 -->
	<context:component-scan base-package="com.fyk.web.**" />
	
	<!-- <mvc:annotation-driven />是开启spring mvc的分发注解请求操作配置
	(也就是说,该配置会自动注册RequestMappingHandlerMapping与RequestMappingHandlerAdapter两个Bean。
	他们是Spring MVC为@Controller分发请求所必需的,并且提供了数据绑定支持),
	这里还将返回参数设置为UTF-8,避免中文乱码 -->
    <mvc:annotation-driven>
		<mvc:message-converters register-defaults="true">
			<!-- 将StringHttpMessageConverter的默认编码设为UTF-8 -->
			<bean class="org.springframework.http.converter.StringHttpMessageConverter">
				<constructor-arg value="UTF-8" />
			</bean>
		</mvc:message-converters>
	</mvc:annotation-driven>
</beans>

这个配置中,component-scan是扫描bean,这里,我扫描了com.fyk.web包下的类,这样,该包下的controller可以被spring发现。
而<mvc:annotation-driven />配置,是开启spring mvc的分发注解请求操作配置(也就是说,该配置会自动注册RequestMappingHandlerMapping与RequestMappingHandlerAdapter两个Bean。他们是Spring MVC为@Controller分发请求所必需的,并且提供了数据绑定支持)。
如果单单只是配置<mvc:annotation-driven />的话,会发现spring mvc返回中文乱码,所以在该配置中,将编码设置成为了UTF-8。


至此,spring mvc框架已经搭建完成了,写一个测试类:

package com.fyk.web.test;

import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("test")
public class TestController {

	@PostMapping("/hello")
	public String sayHello(String name) {
		return String.format("%s,你好!", name);
	}
}

然后访问该地址:
发现传入中文和返回中文,都没有出现乱码,基础的MVC工程算是搭建完成了。以后,会在此基础工程上,继续实现复杂的功能。
源码地址:https://gitee/fyk_wq/spring-stuty

更多推荐

Spring MVC(二)——起步:搭建一个简单的Spring MVC工程项目