spirngMVC(2)——HelloSpringMVC

1 HelloSpringMVC

1.1 For the configuration version program, see springmvc(1)

1.2 Annotated version

1 Create a new Moudle: spring-annotation to add web support.
2 Because Maven may have resource filtering problems, we will configure it perfectly

<!--静态资源过滤-->
    <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>

3 Introduce related dependencies in the pom.xml file
4 Add the Spring MVC configuration file springmvc-servlet.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
       https://www.springframework.org/schema/context/spring-context.xsd
       http://www.springframework.org/schema/mvc
       https://www.springframework.org/schema/mvc/spring-mvc.xsd">

    <!-- 自动扫描包,让指定包下的注解生效,由IOC容器统一管理 -->
    <context:component-scan base-package="com.zs.controller"/>

    <!-- 让Spring MVC不处理静态资源 .css .js .mp3 .mp4-->
    <mvc:default-servlet-handler />
    <!--
    支持mvc注解驱动
        在spring中一般采用@RequestMapping注解来完成映射关系
        要想使@RequestMapping注解生效
        必须向上下文中注册DefaultAnnotationHandlerMapping
        和一个AnnotationMethodHandlerAdapter实例
        这两个实例分别在类级别和方法级别处理。
        而annotation-driven配置帮助我们自动完成上述两个实例的注入。
     -->
    <mvc:annotation-driven />

    <!-- 视图解析器 -->
    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"
          id="internalResourceViewResolver">
        <!-- 前缀 -->
        <property name="prefix" value="/WEB-INF/jsp/" />
        <!-- 后缀 -->
        <property name="suffix" value=".jsp" />
    </bean>

</beans>

In the view resolver, we store all the views in the /WEB-INF/ directory, which can ensure the security of the view, because the files in this directory cannot be directly accessed by the client.
Let IOC annotations take effect
Static resource filtering: HTML. JS. CSS. Pictures, videos...
MVC annotation-driven
configuration view parser

5 Create Controller

package com.zs.controller;

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

@Controller //自动装配springIOC容器中
public class HelloController {
    
    
    @RequestMapping("/hello")
    public String hello(Model model) {
    
    
        // 封装数据
        model.addAttribute("msg","Hello,SpringMVCAnnotation");
        return "hello"; //会被视图解析器处理
    }
}

6 Create the view layer

<%--
  Created by IntelliJ IDEA.
  User: DELL
  Date: 2021/1/24
  Time: 14:34
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>Title</title>
</head>
<body>
${
    
    msg}
</body>
</html>

7 Configure Tomcat to run
8 Configure Tomcat and turn on the server
Insert picture description here
. Three things that must be configured to use springMVC:
processor mapper, processor adapter, and view resolver.
Usually, we only need to manually configure the view resolver, and the processor mapper and processor The adapter only needs to open the annotation driver, and save a lot of xml configuration

Guess you like

Origin blog.csdn.net/zs18753479279/article/details/113090585