搭建一个最基本的 SpringMVC 项目

一、环境准备

  • 开发工具: STS-3.9.9 下载地址
  • Java 版本:jdk1.8.0_202
  • Spring 版本:spring-framework-5.2.7.RELEASE 下载地址
  • Tomcat版本:apache-tomcat-9.0.8 下载地址
  • commons-logging 版本:commons-logging-1.2 下载地址
  • Gson 版本:gson-2.8.6(用于将 Java 对象转成 Json 字符串) 下载地址

二、搭建项目

1、创建一个动态 JavaWeb 项目

在这里插入图片描述

2、将必要的 Jar 包拷贝到项目的 /spring-mvc/WebContent/WEB-INF/lib/ 路径下,如图所示

在这里插入图片描述

3、在 src 目录下新建类型为 Spring Bean Configuration File 的 springMVC.xml 文件做为 SpringMVC 项目配置文件,内容如下

在这里插入图片描述

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xmlns:mvc="http://www.springframework.org/schema/mvc"
	xmlns:context="http://www.springframework.org/schema/context"
	xmlns:aop="http://www.springframework.org/schema/aop"
	xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.3.xsd
		http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
		http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd
		http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.3.xsd">

	<!-- 声明控制器包路径,程序将扫描有注解的包 -->
	<context:component-scan
		base-package="com.swmfizl.controller"></context:component-scan>

	<!-- 配置视图解析器,即 jsp 文件所在路径 -->
	<bean
		class="org.springframework.web.servlet.view.InternalResourceViewResolver">
		<property name="prefix" value="/views/" />
		<property name="suffix" value=".jsp" />
	</bean>

</beans>

4、配置 /spring-mvc/WebContent/WEB-INF/web.xml 文件,拦截指定的请求,并交给 SpringMVC 的 DispatcherServlet 处理

在这里插入图片描述

<?xml version="1.0" encoding="UTF-8"?>
<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_2_5.xsd"
	id="WebApp_ID" version="2.5">
	<display-name>SpringMVC</display-name>
	<welcome-file-list>
		<welcome-file>index.html</welcome-file>
		<welcome-file>index.htm</welcome-file>
		<welcome-file>index.jsp</welcome-file>
		<welcome-file>default.html</welcome-file>
		<welcome-file>default.htm</welcome-file>
		<welcome-file>default.jsp</welcome-file>
	</welcome-file-list>
	
	<servlet>
      <servlet-name>springDispatcherServlet</servlet-name>
      <servlet-class>
         org.springframework.web.servlet.DispatcherServlet
      </servlet-class>
      
      <!-- 告诉程序 springDispatcherServlet 配置文件的位置 -->
      <init-param>
      	<param-name>contextConfigLocation</param-name>
      	<param-value>classpath:springMVC.xml</param-value>
      </init-param>
      
      <!-- 程序开始立即加载该配置 -->
      <load-on-startup>1</load-on-startup>
   </servlet>

	<!-- 拦截一切请求,并交给 SpringMVC 的 DispatcherServlet 处理 -->
   <servlet-mapping>
      <servlet-name>springDispatcherServlet</servlet-name>
      <url-pattern>/</url-pattern>
   </servlet-mapping>
</web-app>

5、在目录 /spring-mvc/WebContent/ 下新建 index.jsp 文件,在目录 /spring-mvc/WebContent/views/ 下新建 welcome.jsp 文件,如图所示

在这里插入图片描述

6、新建一个 TestController 控制器,测试是否能正常显示 jsp 页面和接口是否能正常返回 Json 数据

在这里插入图片描述

/**
 * @author ZhongLi
 * @date 2020-06-23
 */
package com.swmfizl.controller;

import java.util.HashMap;
import java.util.Map;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.google.gson.Gson;

@Controller
public class TestController {

	/**
	 * 返回视图页面
	 * 
	 * @return
	 */
	@RequestMapping(value = "welcomePage")
	public String welcomePage() {
		return "welcome";
	}

	/**
	 * 返回 Json 数据
	 * 
	 * @return
	 */
	@RequestMapping(value = "welcomeData", method = RequestMethod.GET, produces = "application/json;charset=utf-8")
	@ResponseBody
	public String welcomeData() {
		Map<String, Object> response = new HashMap<String, Object>();
		Gson gson = new Gson();
		response.put("code", 0);
		response.put("msg", "欢迎");
		return gson.toJson(response);
	}
}

三、运行项目查看效果

1、查看首页,正常:http://localhost:8080/spring-mvc/

在这里插入图片描述

2、查看欢迎页面,正常:http://localhost:8080/spring-mvc/welcomePage

在这里插入图片描述

3、查看接口是否能正常返回 Json 数据:http://localhost:8080/spring-mvc/welcomeData

在这里插入图片描述

四、项目分享地址:

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章