• SpringMVC入门


    SpringMVC

    快速入门

    1. jar包
      • commons-logging-1.1.3.jar
      • spring-aop-4.0.0.RELEASE.jar
      • spring-beans-4.0.0.RELEASE.jar
      • spring-context-4.0.0.RELEASE.jar
      • spring-core-4.0.0.RELEASE.jar
      • spring-expression-4.0.0.RELEASE.jar
      • spring-web-4.0.0.RELEASE.jar
      • spring-webmvc-4.0.0.RELEASE.jar
    2. 在web.xml中配置DispatcherServlet
          <servlet>
           <servlet-name>dispatcherServlet</servlet-name>
           <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
           <init-param>
               <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>
      
    3. mvc.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
               http://www.springframework.org/schema/context/spring-context.xsd">
           <!--注解扫描-->
           <context:component-scan base-package="com.*"></context:component-scan>
           <!--配置视图解析器-->
           <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
               <property name="prefix" value="/"></property>
               <property name="suffix" value=".jsp"></property>
           </bean>
       </beans>
      
    4. 编写Controller层标识
        package com.Controller;
    
        import org.springframework.stereotype.Controller;
        import org.springframework.web.bind.annotation.RequestMapping;
    
        /**
        * @author Jamin <br>
        * @date 2019/3/28 14:35 <br>
        *     controller层
        */
        @Controller
        public class UserController {
    
        /**
        * helloword
        *
        * @return index页面
        */
        @RequestMapping(value = "/hello")
        public String hello() {
            System.out.println("Hello World");
            return "index";
        }
        }
    
    获取数据
    1. @RequestMapping:支持Ant风格资源地址
      * – ?:匹配文件名中的一个字符
      * – *:匹配文件名中的任意字符
      * – 匹配多层路径

    2. @PathVarivable:将URL中的占位符参数绑定到控制台的处理方法的入参中

          @Controller
      public class UserController {
      /**
      * helloword
      *
      * @return index页面
      */
      @RequestMapping(value = "/hello}")
      public String hello(@RequestParam(value = "name", required = false) String name) {
          System.out.println("Hello World");
          return "index";
      }
      }
      
    3. @RequestParm

          @Controller
      public class UserController {
      /**
      * helloword
      *
      * @return index页面
      */
      @RequestMapping(value = "/hello}")
      public String hello(@RequestParam(value = "name", required = false) String name) {
          System.out.println("Hello World");
          return "index";
      }
      }
      
    4. @RequestHeader

       @Controller
       public class UserController {
       /**
       * helloword
       *
       * @return index页面
       */
       @RequestMapping(value = "/hello}")
       public String hello(@RequestHeader(value = "Host") String header) {
           System.out.println("Hello World" + header);
           return "index";
       }
       }
      
    5. @CookieValue

          @Controller
      public class UserController {
      /**
      * helloword
      *
      * @return index页面
      */
      @RequestMapping(value = "/hello}")
      public String hello(@CookieValue(value = "JSESSIONID") String cookie) {
          System.out.println("Hello World" + cookie);
          return "index";
      }
      }
      
    6. 使用 Servlet API 作为入参

      1. HttpServletRequest
      2. HttpServletResponse
      3. HttpSession
      4. java.security.Principa
      5. Locale
      6. InputStream
      7. OutputStream
      8. Reader
      9. Writer
       @Controller
       public class UserController {
       /**
       * helloword
       *
       * @return index页面
       */
       @RequestMapping(value = "/hello}")
       public String hello(HttpServletRequest request) {
           System.out.println("Hello World");
           String name = request.getParameter("name");
           System.out.println(name);
           return "index";
       }
       }
      

    输出数据

    1. ModelAndView
          @Controller
      public class UserController {
      /**
      * helloword
      *
      * @return index页面
      */
      @RequestMapping("/hello")
      public ModelAndView hello() {
          ModelAndView mav = new ModelAndView();
          mav.addObject("name","张三");
          mav.addObject("age","12");
          mav.setViewName("index");
          return mav;
      }
      }
      
    2. Model
       @Controller
       public class UserController {
       /**
       * helloword
       *
       * @return index页面
       */
       @RequestMapping("/hello")
       public String hello(Model model) {
       model.addAttribute("name","zhangsan");
           return "index";
       }
       }
      
    3. @SessionAttributes 赋值方法是同名赋值
       @SessionAttributes(value = "user")
       @Controller
       public class UserController {
       /**
       * helloword
       *
       * @return index页面
       */
       @RequestMapping("/hello")
       public ModelAndView hello() {
           ModelAndView mav = new ModelAndView();
           mav.addObject("user", "saddd");
           mav.setViewName("index");
           return mav;
       }
       } 
      
    4. Map
      @Controller
      public class UserController {
      /**
      * helloword
      *
      * @return index页面
      */
      @RequestMapping("/hello")
      public String hello(Map map) {
          map.put("name", "asnkjhauicgayucfyt");
          return "index";
      }
      }
      
    5. @ResponseBody 返回值单独为一个页面
          @RequestMapping("del")
      @ResponseBody
      public String deleteUser(@RequestParam(value = "id") String id, HttpServletResponse response) {
          int i = us.deleteUser(Integer.parseInt(id));
          if (i > 0) {
          return "删除成功";
          } else {
          return "删除失败";
          }
      }
      
    6. @RequestBody 将前台的发送的json对象转换为java对象
        /**
      * json对象序列化json字符串List接受
      *
      * @param jsonObject
      * @return
      */
      

    @RequestMapping("/json.do")
    @ResponseBody
    public void test3(@RequestBody List users) {
    System.out.println(users);
    }

    #### springmvc请求原理
      第一步:发起请求到前端控制器(DispatcherServlet)
      第二步:前端控制器请求处理器映射器HandlerMapping查找 Handler (controller可以根据xml配置、注解进行查找)
      第三步:处理器映射器HandlerMapping向前端控制器返回Handler
      第四步:前端控制器调用处理器适配器HandlerAdapter去执行Handler
      第五步:处理器适配器去执行Handler
      第六步:Handler执行完成给处理器适配器返回ModelAndView
      第七步:处理器适配器向前端控制器返回ModelAndView
          ModelAndView是springmvc框架的一个底层对象,包括 Model和view
      第八步:前端控制器请求视图解析器View resolver去进行视图解析
          根据逻辑视图名解析成真正的视图(jsp)
      第九步:视图解析器向前端控制器返回View
      第十步:前端控制器进行视图渲染
          视图渲染将模型数据(在ModelAndView对象中)填充到request域
      第十一步:前端控制器向用户响应结果
  • 相关阅读:
    public,protected,private辨析
    转载---Java集合对象的深度复制与普通复制
    SSM框架学习之高并发秒杀业务--笔记4-- web层
    PCB布线总的原则
    模拟电子技术目录
    放大器(PA+LAN)在射频上的应用
    AD软件Bug和自我失误的对战
    二、cadence焊盘与封装制作操作步骤详细说明
    图腾柱电路工作原理
    转载:介绍AD另外一种奇葩的多通道复用的方法
  • 原文地址:https://www.cnblogs.com/JaminYe/p/10630441.html
Copyright © 2020-2023  润新知