zoukankan      html  css  js  c++  java
  • SpringMVC的学习_____2.注解版的实现-----以及restful风格前端参数的传递

    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";
        }
    
    }

    Restful风格:(Controller层的编写)

    @Controller     //自动扫描包的注解
    @RequestMapping("/restful")   //注解驱动
    public class RestfulController {
    
        @RequestMapping("/test1/{p1}/{p2}")  
         //restful风格的前端传入参数的形式 
    
        //接受前端参数的注解: @PathVariable
        public String restful(@PathVariable int p1, @PathVariable int p2, Model model){
            int sum=p1+p2; //获取前端传地进来的参数
            model.addAttribute("msg",sum);
            //将使用参数的计算结果放入目标数
            //并返回给前端页面
            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.前端页面的编写;

    总之总的思想就是:用户前端传递来的请求---》DispatcherServlet-----》具体的Controller进行处理并返回!

     

  • 相关阅读:
    回答自己之前的提问!
    阅读《构建之法》第13-17章
    阅读《构建之发》10-12章
    阅读《构建之法 》8,9,10章
    Practise 5.2测试与封装(黑白盒
    Practice5.1 测试与封装5.1
    Practice4 阅读《构建之法》6-7章
    Practice3 阅读《构建之法》1-5章
    “做汉堡”之评价我的队友
    Practice2 结对子之“小学四则运算”
  • 原文地址:https://www.cnblogs.com/xbfchder/p/11318811.html
Copyright © 2011-2022 走看看