处理器映射器与处理器适配器最佳实践(●ˇ∀ˇ●)

目录结构
处理器映射器与处理器适配器最佳实践(●ˇ∀ˇ●)
pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>org.javaboy</groupId>
    <artifactId>springmvc02</artifactId>
    <version>1.0-SNAPSHOT</version>

    <packaging>war</packaging>

    <dependencies>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-webmvc</artifactId>
            <version>RELEASE</version>
        </dependency>
        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>javax.servlet-api</artifactId>
            <version>4.0.1</version>
        </dependency>
        <dependency>
            <groupId>javax.servlet.jsp</groupId>
            <artifactId>javax.servlet.jsp-api</artifactId>
            <version>2.3.3</version>
        </dependency>
    </dependencies>

</project>

applicationContext.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"
       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">
    <!--先在Spring中进行包扫描 true扫描所有的但是出去controller-->
    <context:component-scan base-package="org.javaboy.springmvc02" use-default-filters="true">
        <context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
    </context:component-scan>

</beans>

spring-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">

    <!--springmvc上来也是包扫描 false一个也不扫但是又要把controller加进来-->
    <context:component-scan base-package="org.javaboy.springmvc02" use-default-filters="false">
        <context:include-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
    </context:component-scan>
    <!--让在MyController中的注解@RequestMapping(/hello)访问到方法hello-->
    <bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping"
          id="requestMappingHandlerMapping"/>
    <!--找到hello方法后还需要有人执行它-->
    <bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter"
          id="requestMappingHandlerAdapter"/>
    <!--上面的2句可以用下面这一句代替-->
    <!--<mvc:annotation-driven-->
    <!--有视图modelandView就要有视图解析器-->
   <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver" id="viewResolver">
       <property name="prefix" value="/"/>
       <property name="suffix" value=".jsp"/>
   </bean>
</beans>

hello.jsp

<%--
  Created by IntelliJ IDEA.
  User: 管 理 员
  Date: 2021年11月02日 0002
  Time: 18:31:07
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>hello</title>
</head>
<body>
<%--${name}就是MyController中配置的javaboy--%>
<h1>hello ${name}</h1>
</body>
</html>

MyController.java

package org.javaboy.springmvc02.controller;

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

/**
 * @author 邓雪松 (づ ̄ 3 ̄)づ)
 * @create 2021-11-02-18-13
 * @RequestMapping去标记一个接口,这个接口的访问路径就是/hello,
 * 当你访问这个接口的时候就返回hello(return "hello")(ModelAndView视图)
 */
@Controller
public class MyController {
    @RequestMapping("/hello")
    public ModelAndView hello(){
        ModelAndView mv = new ModelAndView("hello");
        mv.addObject("name","javaboy");
        return mv;
    }
}

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_3_1.xsd"
         version="3.1">
    <!--在web.xml中加载spring和springmvc的配置文件-->
    <!--先加载spring的-->
    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>classpath:applicationContext.xml</param-value>
    </context-param>
    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>
    <!--然后是springmvc的-->
    <servlet>
        <servlet-name>springmvc</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <init-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>classpath:spring-servlet.xml</param-value>
        </init-param>
    </servlet>
    <servlet-mapping>
        <servlet-name>springmvc</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping>
</web-app>

运行结果图
处理器映射器与处理器适配器最佳实践(●ˇ∀ˇ●)

结束(●ˇ∀ˇ●)

上一篇:SpringMVC-全局异常处理


下一篇:SpringMVC之使用与原理