Skip to content

4. 注解开发(处理器映射器、处理器适配器)

创建webapp骨架的项目

4.1. 搭建springmvc程序

4.1.1. Pom**

4.1.2. Springmvc.xml**

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
        https://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/context
        https://www.springframework.org/schema/context/spring-context.xsd  ">

    <!--配置扫描组件-->
    <context:component-scan base-package="com.neuedu.controller"/>
    

</beans>

4.1.3. Web.xml中配置前端控制器

xml
<!DOCTYPE web-app PUBLIC
        "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
        "http://java.sun.com/dtd/web-app_2_3.dtd" >
<web-app>
  <display-name>Archetype Created Web Application</display-name>

  <!--前端控制器-->
  <servlet>
    <servlet-name>DispatcherServlet</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <init-param>
      <!--默认的配置文件的名字applicationContext.xml-->
      <param-name>contextConfigLocation</param-name>
      <param-value>classpath:springmvc.xml</param-value>
    </init-param>

  </servlet>

  <servlet-mapping>
    <servlet-name>DispatcherServlet</servlet-name>
    <url-pattern>/</url-pattern>
  </servlet-mapping>
</web-app>

4.2. 配置注解形式的映射器、适配器

4.2.1. 配置IOC容器中的两个类

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
        https://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/context
        https://www.springframework.org/schema/context/spring-context.xsd  ">

    <!--配置扫描组件-->
    <context:component-scan base-package="com.neuedu.controller"/>

    <!--注解形式的处理器映射器(Mapping)-->
    <bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping"/>

    <!--注解形式的处理器适配器(Adapter)-->
    <bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter"/>
    

</beans>

4.2.2. 使用annotation驱动的形式声明映射器、适配器

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
        https://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">

    <!--配置扫描组件-->
    <context:component-scan base-package="com.neuedu.controller"/>

    <!--annotation-driven 代替上述映射器 和适配器 有些额外的功能-->
    <mvc:annotation-driven/>


</beans>

4.2.3. 具体的Controller

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

/**
 * 项目    : spring-mvc-java1
 * 创建时间 :2020/3/26  11:33 26
 * author  :jshand-root
 * site    :  http://314649444.iteye.com
 * 描述     : 注解形式
 */
@Controller
public class UserController {


    /**
     * http://ip:port/context/url
     *
     * http://127.0.0.1:8080/springmvc/test_annotation
     */
    @RequestMapping("/test_annotation")
    public void testAnnotation(){
        System.out.println("测试注解形式的方法");
    }

}

4.2.4. 测试:

http://127.0.0.1:8080/springmvc/test_annotation

Released under the MIT License.