SpringMVC笔记:Hello world

SpringMVC是Spring Framework的一部分,是基于Java实现的轻量级MVC Web框架,SpringMVC框架主要围绕DispatcherServlet(Servlet调度)进行设计的,会将收到的请求使用DispatcherServlet分发到不同的Controller进行处理。SpringMVC特点:

  • 轻量级,简介灵活,也简单易学。
  • 是一个基于请求和响应的MVC框架,非常高效。
  • 与Spring兼容性非常好。
  • 约定大于配置。
  • 功能强大,支持RESTful风格、数据校验、数据格式化、拦截器等。

Hello world示例

此示例中使用的是原始的方式,即实现Controller接口的方式,可以更加直观的体现SpringMVC的执行流程和原理,但是实际上,平常工作中更多的是使用注解来进行开发。

pom配置

以下只是列了常用的pom配置和可能需要的一些依赖,实际运行时也可以自己调整依赖的版本和添加其他的配置和依赖。

<dependencies>
	<dependency>
		<groupId>junit</groupId>
		<artifactId>junit</artifactId>
		<version>4.12</version>
		<scope>test</scope>
	</dependency>
	<dependency>
		<groupId>org.springframework</groupId>
		<artifactId>spring-webmvc</artifactId>
		<version>5.1.9.RELEASE</version>
	</dependency>
	<dependency>
		<groupId>javax.servlet</groupId>
		<artifactId>servlet-api</artifactId>
		<version>2.5</version>
	</dependency>
	<dependency>
		<groupId>javax.servlet.jsp</groupId>
		<artifactId>jsp-api</artifactId>
		<version>2.2</version>
	</dependency>
	<dependency>
		<groupId>javax.servlet</groupId>
		<artifactId>jstl</artifactId>
		<version>1.2</version>
	</dependency>
	<dependency>
		<groupId>org.projectlombok</groupId>
		<artifactId>lombok</artifactId>
		<version>1.16.10</version>
	</dependency>
	<dependency>
		<groupId>com.fasterxml.jackson.core</groupId>
		<artifactId>jackson-databind</artifactId>
		<version>2.13.1</version>
	</dependency>
	<dependency>
		<groupId>com.alibaba</groupId>
		<artifactId>fastjson</artifactId>
		<version>1.2.79</version>
	</dependency>
</dependencies>

<build>
	<resources>
		<resource>
			<directory>src/main/java</directory>
			<includes>
				<include>**/*.properties</include>
				<include>**/*.xml</include>
			</includes>
			<filtering>false</filtering>
		</resource>
		<resource>
			<directory>src/main/resources</directory>
			<includes>
				<include>**/*.properties</include>
				<include>**/*.xml</include>
			</includes>
			<filtering>false</filtering>
		</resource>
	</resources>
</build>

web.xml配置

给module添加了web支持之后,就会在 web/WEB-INF 目录下生成web.xml文件。

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
         version="4.0">

    <!-- 1. 注册DispatcherServlet:请求分发器 -->
    <servlet>
        <!-- 这里的servlet-name可以自定义,但是需要与下面的servlet-mapping中的servlet-name相同,表示需要处理哪些路径的请求 -->
        <servlet-name>springmvc</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <init-param>
            <!-- 指定spring配置文件,文件名称可以自定义 -->
            <param-name>contextConfigLocation</param-name>
            <param-value>classpath:springmvc-servlet.xml</param-value>
        </init-param>
        <!-- 启动级别1:项目启动的时候就加载本文件的配置 -->
        <load-on-startup>1</load-on-startup>
    </servlet>

    <!-- /匹配所有请求,注意这里不是/* -->
    <servlet-mapping>
        <servlet-name>springmvc</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping>

</web-app>

Spring配置文件

在web.xml中指定了Spring配置文件之后,接下来自然就需要继续配置Spring了。

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

    <!-- 映射器器:会在Spring配置中查找和请求路径一致的bean -->
    <bean class="org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping"/>
    <!-- 适配器 -->
    <bean class="org.springframework.web.servlet.mvc.SimpleControllerHandlerAdapter"/>

    <!-- 视图解析器:给DispatcherServlet指定对应的模板引擎ModelAndView -->
    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver" id="InternalResourceViewResolver">
        <!-- 配置前缀和后缀:会在controller返回的字符串上自动添加对应的前缀和后缀,然后查找并映射到对应的jsp文件 -->
        <!-- 前缀 -->
        <property name="prefix" value="/WEB-INF/jsp/"/>
        <!-- 后缀 -->
        <property name="suffix" value=".jsp"/>
    </bean>

    <!-- 指定处理器:id对应的是请求路径,class对应的是这个请求的controller处理器 -->
    <bean id="/hello" class="com.yun.controller.HelloController"/>

</beans>

实现Controller接口

由于Spring配置文件中配置了对应请求的Controller处理器,所以实现了Controller接口之后,来自浏览器的请求就会先经过接口的handleRequest进行处理,处理之后再进入到jsp页面,最后再返回给浏览器。

package com.yun.controller;

import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.Controller;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class HelloController implements Controller {
    public ModelAndView handleRequest(HttpServletRequest request, HttpServletResponse response) throws Exception {
        ModelAndView mv = new ModelAndView();
        // 设置在ModelAndView对象里的数据,可以直接在jsp文件中以${msg}的方式拿到
        mv.addObject("msg", "HelloSpringMVC!");
        // 由于在Spring配置文件中配置了前缀和后缀,这里返回hello之后就会自动映射到/WEB-INF/jsp/hello.jsp
        mv.setViewName("hello");
        return mv;
    }
}
posted @ 2022-03-24 01:06  山上下了雪-bky  阅读(28)  评论(0编辑  收藏  举报