搭建一個最基本的 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

在這裏插入圖片描述

四、項目分享地址:

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