SpringMVC的学习_____2.注解版的实现

1.SpringMVC的web.xml文件的编写:

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xmlns="http://java.sun.com/xml/ns/javaee"
         xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
         http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
         id="WebApp_ID" version="3.0">

  <!--1.注册dispatcherservlet 实际上就是一个servlet-->

  <servlet>
    <servlet-name>springmvc</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>

    <!--SpringMVC配置-->
    <init-param>
      <param-name>contextConfigLocation</param-name>
      <param-value>classpath:springmvc-servlet.xml</param-value>
    </init-param>

    <load-on-startup>1</load-on-startup>
  </servlet>

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

</web-app>

2.SpringMVC配置文件的编写:

<?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.xsd
        http://www.springframework.org/schema/mvc
        http://www.springframework.org/schema/mvc/spring-mvc.xsd">
    <!--
    原理级别的编写流程:
      处理器映射器
      处理器适配器
      视图解析器
      Controller bean
    -->

    <!--注解级别的编写-->
    <!--1.自动扫描包,让该包下的注解生效 由spring进行 bean 管理-->
    <context:component-scan base-package="com.xbf.controller"/>

    <!--2.让SpringMVC不处理静态资源-->
    <mvc:default-servlet-handler/>

    <!--3.
    支持注解驱动
    在spring中使用@RequestMapping来完成映射关系,
    要想使@RequestMapping注解生效
     必须向上下文中注册DefaultAnnotationHandlerMapping
     和一个AnnotationMethodHandlerAdapter实例
      这两个实例分别在类级别和方法级别处理。
     而annotation-driven配置帮助我们自动完成上述两个实例的注入。
    -->
    <mvc:annotation-driven/>

    <!--4.视图解析器-->
    <!--我们将视图放在 web-inf目录下,这个目录客户不能进行直接访问,比较安全-->
    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix" value="/WEB-INF/jsp/"/>
        <property name="suffix" value=".jsp"/>
    </bean>
</beans>

3.controller层的编写:

package com.xbf.controller;


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

@Controller
@RequestMapping("/controller")
public class HelloController {
    @RequestMapping("/hello")
    public String hello(Model model){
        model.addAttribute("msg","hellospring");
        return "hello";
    }

}

4,前端页面的编写:

注解为@Controller是为了让Spring IOC容器初始化时自动扫描到;

@RequestMapping是为了映射请求路径,这里因为类与方法上都有映射所以访问时应该是/HelloController/hello;

方法中声明Model类型的参数是为了把Action中的数据带到视图中;

方法返回的结果是视图的名称hello,加上配置文件中的前后缀变成WEB-INF/jsp/hello.jsp。

<%--
  Created by IntelliJ IDEA.
  User: xbf
  Date: 2019/8/8
  Time: 1:30
  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>

总结:

流程:
1.web.xml文件的编写:
dispatcherservlet的注册--》SpringMVC配置文件,起动级别,过滤请求的范围
2.springmvc-servlet.xml文件的编写:
自动扫描包
注解驱动
静态资源过滤
视图解析
3.controller层的编写
@Controller
@RequestMappping("hello")
4.前端页面的编写;

 

猜你喜欢

转载自www.cnblogs.com/xbfchder/p/11318811.html