【SpringMVC】1. SpringMVC的第一个程序——HelloWorld

##一、什么是SpringMVC

Spring Web MVC is the original web framework built on the Servlet API and included in the Spring Framework from the very beginning. The formal name “Spring Web MVC” comes from the name of its source module spring-webmvc but it is more commonly known as “Spring MVC”.

Parallel to Spring Web MVC, Spring Framework 5.0 introduced a reactive stack, web framework whose name Spring WebFlux is also based on its source module spring-webflux.
—SpringMVC官方文档

Spring Web MVC是构建在Servlet API上的原始Web框架,从一开始就包含在Spring Framework中。正式名称“Spring Web MVC”来自其源模块spring-webmvc的名称, 但它通常被称为“Spring MVC”。

与Spring Web MVC并行,Spring Framework 5.0引入了一个反应式堆栈,Web框架,其名称Spring WebFlux也基于其源模块 spring-webflux。
###SpringMVC的优点

  • Spring 为展现层提供的基于 MVC 设计理念的优秀的Web 框架,是目前最主流的 MVC 框架之一。
  • Spring3.0 后全面超越 Struts2,成为最优秀的 MVC 框架。
  • Spring MVC 通过一套 MVC 注解,让 POJO 成为处理请 求的控制器,而无须实现任何接口。
  • 支持 REST 风格的 URL 请求。
  • 采用了松散耦合可插拔组件结构,比其他 MVC 框架更具
    扩展性和灵活性



    ##二、第一个程序HelloWorld实现

注意!!
SpringMVC系列的日志基于Spring4,请注意版本号的匹配,不然可能出现一些明明代码一样却报错的问题。

步骤

  1. 加入 jar 包
  2. 在 web.xml 中配置 DispatcherServlet
  3. 加入 Spring MVC 的配置文件
  4. 编写处理请求的处理器,并标识为处理器
  5. 编写视图

###1.创建一个名为SpringMVC-1的Web项目
###2.导入相关的JAR 包
如果是使用MyEclipse进行开发的同学可以右键项目–>Configure Facets–>Install Spring Facets来自动导入相关的包(注意MyEclipse的版本,早期的版本可能没有Spring 4.X的选项,此教程的Spring版本是Spring 4.1.0)
这里写图片描述

![这里写图片描述](https://img-blog.csdn.net/20180820104824997?watermark/2/text/aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3FxXzMzNTk2OTc4/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70)

如果使用eclipse进行开发的同学也可以导入以下的JAR包

  • commons-logging-1.1.3.jar
  • spring-aop-4.1.0.RELEASE.jar
  • spring-beans-4.1.0.RELEASE.jar
  • spring-context-4.1.0.RELEASE.jar
  • spring-core-4.1.0.RELEASE.jar
  • spring-expression-4.1.0.RELEASE.jar
  • spring-web-4.1.0.RELEASE.jar
  • spring-webmvc-4.1.0.RELEASE.jar

###3.在web.xml配置DispatcherServlet
配置DispatcherServlet的作用可以看这篇文章:《第三章 DispatcherServlet详解 ——跟开涛学SpringMVC》

<servlet>
		<servlet-name>springDispatcherServlet</servlet-name>
		<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
		<!-- 配置DispatcherServlet的一个初始化参数:配置SpringMVC配置文件位置和名称 -->
		<!-- 实际上可以不通过contextConfiguration来配置SpringMVC配置文件的位置和名称 默认的配置文件为:/WEB-INF/<servlet-name>-servlet.xml -->
		<init-param>
			<param-name>contextConfigLocation</param-name>
			<param-value>classpath:springmvc.xml</param-value>
		</init-param>
		<load-on-startup>1</load-on-startup>
	</servlet>

	<servlet-mapping>
		<servlet-name>springDispatcherServlet</servlet-name>
		<url-pattern>/</url-pattern>
	</servlet-mapping>

###4.在src根目录下创建Spring配置文件springmvc.xml

<?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:context="http://www.springframework.org/schema/context"
	xmlns:mvc="http://www.springframework.org/schema/mvc"
	xsi:schemaLocation="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.1.xsd
		http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.1.xsd">

	<!-- 配置自动扫描 -->
	<context:component-scan base-package="com.springmvc"></context:component-scan>
	<!-- 配置视图解析器:如何把Handler方法返回值解析为实际的物理视图 -->
	<bean
		class="org.springframework.web.servlet.view.InternalResourceViewResolver">
		<property name="prefix" value="/WEB-INF/views/"></property>
		<property name="suffix" value=".jsp"></property>
	</bean>
<beans>

###5.创建一个名为com.springmvc.handlers的包
####(1)在这个包内创建一个叫做HelloWorld的Handle类

package com.springmvc.handlers;

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

@Controller
public class HelloWorld {

	/*
	 * 1.使用RequestMapping注解起来映射请求的URL
	 * 2.返回值通过视图解析器解析为实际的物理视图,对于InternalResourceViewResolver 视图解析器,会做如下解析:
	 * /WEB-INF/views/success.jsp
	 */
	@RequestMapping("/helloworld")
	public String hello(){
		System.out.println("HelloWorld");
		return "success";
	}
}

###6.在WEB-INF文件夹下创建views,并在views下创建success.jsp文件

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %>
    
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
	
	<h4> success page</h4>
	
</body>
</html>

###7.运行TomCat服务器。
TomCat运行成功后在浏览器输入以下地址:http://localhost:8080/springmvc-1/helloworld,出现了Success界面即为成功。
这里写图片描述

**由于我的Success页面写了其他东西,所以多了其他东西,只要上到Success页即为成功**

猜你喜欢

转载自blog.csdn.net/qq_33596978/article/details/81867481