SpringMVC入门与数据绑定

SpringMVC入门

  1. 什么是MVC
    图片说明
    2.什么是SpringMVC
    图片说明
    图片说明
    3.SpringMVC的环境配置
    • Maven的pom.xml文件中依赖Spring-webmvc。
      <dependency>
          <groupId>org.springframework</groupId>
          <artifactId>spring-webmvc</artifactId>
        <version>5.0.4.RELEASE</version>
      </dependency>
  • 在web.xml中配置DispatcherServlet
    (1)DispatchServlet是SpringMVC最核心的对象,DispatchServlet用于拦截Http请求,
    并根据请求的url调用与之对应的controller方法,完成Http的请求
    (2)“/”代表拦截所有请求
    (3)load-on-startup:在web应用创建时自动创建Spring ioc容器,并初始化DispatchServlet
    (4)在Servlet标签中还要指定applicationContext.xml文件的位置,通过写一个初始化init-param标签,然后param-name是指定的contextConfigLocation。
    <?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">
    <servlet>
    <servlet-name>springmvc</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <!--ApplicationContext-->
    <init-param>
     <param-name>contextConfigLocation</param-name>
     <param-value>classpath:applicationContext.xml</param-value>
    </init-param>
    <!--在web应用创建时自动创建Spring ioc容器,并初始化DispatchServlet-->
    <load-on-startup>0</load-on-startup>
    </servlet>
    <servlet-mapping>
     <servlet-name>springmvc</servlet-name>
     <url-pattern>/</url-pattern>
    </servlet-mapping>
    </web-app>
    • 配置applicationContext的MVC标记,这里将文件以及注释贴在这里
      <?xml version="1.0" encoding="UTF-8"?>
      <beans xmlns="http://www.springframework.org/schema/beans"
        xmlns:mvc="http://www.springframework.org/schema/mvc"
        xmlns:context="http://www.springframework.org/schema/context"
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:mv="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">
      <!--
      context:component-scan 标签作用
      在Spring IOC初始化过程中,自动创建并管理com.imooc.springmvc及子包中
      拥有以下注解的对象.
      @Repository
      @Service
      @Controller
      @Component
      -->
      <context:component-scan base-package="com.imooc.springmvc"></context:component-scan>
      <!--启用Spring MVC的注解开发模式-->
      <mvc:annotation-driven/>
      <!-- 将图片/JS/CSS等静态资源排除在外,可提高执行效率 -->
      <mvc:default-servlet-handler/>
      </beans>
    • 开发controller控制器
      图片说明
    4.SpringMVC处理流程
    图片说明

数据绑定(controller怎么和url绑定到一起的)

  1. URLMapping(URL映射)
    (1)URLMapping指的是URL与controller进行绑定
    (2)通过将URL与方法进行绑定,SpringMVc便可以通过Tomcat对外暴露服务
  2. URL mapping注解
    (1) @RequestMapping - 通用绑定
    @Controller
    @RequestMapping("/um")
    public class URLMappingController {
  • 通常放在类上面,通常是url的全局设置,这样所有的url地址都要添加上um
  • 放在方法上,作用在方法上,不再区分get/post请求(不推荐使用)
    (2) @GetMapping - 绑定get请求
    @GetMapping("/g")
      @ResponseBody
      public String getMapping(@RequestParam("manager_name") String managerName , Date createTime){
          System.out.println("managerName:"+managerName);
          return "This is get method";
      }
    (3) @PostMapping - 绑定post请求
    因为是post请求所以在使用过程中要在html中form表单中改为post方式
    @PostMapping("/p")
      @ResponseBody
      public String postMapping(String username,Long password){
          return "This is post method";
      }
全部评论

相关推荐

叶扰云倾:进度更新,现在阿里云面完3面了,感觉3面答得还行,基本都答上了,自己熟悉的地方也说的比较细致,但感觉面试官有点心不在焉不知道是不是不想要我了,求阿里收留,我直接秒到岗当阿里孝子,学校那边的房子都退租了,下学期都不回学校,全职猛猛实习半年。这种条件还不诱人吗难道 然后现在约到了字节的一面和淘天的复活赛,外加猿辅导。华为笔试完没动静。 美团那边之前投了个base广州的,把我流程卡麻了,应该是不怎么招人,我直接简历挂了,现在进了一个正常的后端流程,还在筛选,不知道还有没有hc。
点赞 评论 收藏
分享
评论
1
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务