11-17 SpringMvc框架

SpringMvc基于MVC设计模式的框架, web层框架,基于请求与响应的模型

一个请求对应一个servlet servlet暴增

servlet需要手动获取请求参数 同时需要手动进行数据转换  数据自动映射没有实现

Servlet获取参数是一个一个的获取 需要手动把参数封装成一个对象

后端不能不能把数据放到域当中  所以后端需要把数据变成json数据形式

(前端需要的是Json数据,servle是不能实现的,我们的servlet需要手动把对象转换成json字符串,通过Responses的响应输出流给客户端

如果前端请求参数是json数据, Servlet只能通过Request的请求输入流读取json数据,把json转换为java对象,

对Resultful风格的url的数据, Servlet只能获取URL,通过字符串的截取获取数据

案例:

创建一个maven-web项目 springmvc-helloworld

1.加依赖

<?xml version="1.0" encoding="UTF-8"?>

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>

  <groupId>com.hsue</groupId>
  <artifactId>springmvc-helloworld</artifactId>
  <version>1.0-SNAPSHOT</version>
  <packaging>war</packaging>

  <name>springmvc-helloworld Maven Webapp</name>
  <!-- FIXME change it to the project's website -->
  <url>http://www.example.com</url>

  <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <maven.compiler.source>1.8</maven.compiler.source>
    <maven.compiler.target>1.8</maven.compiler.target>
    <spring.version>5.2.15.RELEASE</spring.version>
  </properties>

  <dependencies>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>4.11</version>
      <scope>test</scope>
    </dependency>

    <dependency>
      <groupId>log4j</groupId>
      <artifactId>log4j</artifactId>
      <version>1.2.12</version>
    </dependency>

    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-context</artifactId>
      <version>${spring.version}</version>
    </dependency>

    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-web</artifactId>
      <version>${spring.version}</version>
    </dependency>

    <!--springmvc的依赖-->
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-webmvc</artifactId>
      <version>${spring.version}</version>
    </dependency>
    <!--servlet-api -->
    <dependency>
      <groupId>javax.servlet</groupId>
      <artifactId>javax.servlet-api</artifactId>
      <version>4.0.1</version>
      <scope>provided</scope>
    </dependency>

    <!--jsp-api-->
    <dependency>
      <groupId>javax.servlet.jsp</groupId>
      <artifactId>jsp-api</artifactId>
      <version>2.1</version>
      <scope>provided</scope>
    </dependency>

    <!--log4j-->
    <dependency>
      <groupId>org.slf4j</groupId>
      <artifactId>slf4j-log4j12</artifactId>
      <version>1.7.36</version>
    </dependency>


  </dependencies>

  <build>
    <finalName>springmvc-helloworld</finalName>
    <pluginManagement><!-- lock down plugins versions to avoid using Maven defaults (may be moved to parent pom) -->
      <plugins>
        <plugin>
          <artifactId>maven-clean-plugin</artifactId>
          <version>3.1.0</version>
        </plugin>
        <!-- see http://maven.apache.org/ref/current/maven-core/default-bindings.html#Plugin_bindings_for_war_packaging -->
        <plugin>
          <artifactId>maven-resources-plugin</artifactId>
          <version>3.0.2</version>
        </plugin>
        <plugin>
          <artifactId>maven-compiler-plugin</artifactId>
          <version>3.8.0</version>
        </plugin>
        <plugin>
          <artifactId>maven-surefire-plugin</artifactId>
          <version>2.22.1</version>
        </plugin>
        <plugin>
          <artifactId>maven-war-plugin</artifactId>
          <version>3.2.2</version>
        </plugin>
        <plugin>
          <artifactId>maven-install-plugin</artifactId>
          <version>2.5.2</version>
        </plugin>
        <plugin>
          <artifactId>maven-deploy-plugin</artifactId>
          <version>2.8.2</version>
        </plugin>
      </plugins>
    </pluginManagement>
  </build>
</project>

日志依赖log4j

向hello.do这个url发起请求

<?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_4_0.xsd"
         version="4.0">

    <servlet>
        <servlet-name>springmvc</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>springmvc</servlet-name>
        <url-pattern>*.do</url-pattern>
    </servlet-mapping>

</web-app>
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:mvc="http://www.springframework.org/schema/mvc"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       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">

    <!--mvc注解驱动-->
    <mvc:annotation-driven />

    <!--视图解析器 -->
    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix" value="/"></property>
        <property name="suffix" value=".jsp"></property>
    </bean>

    <!-- 开启扫描-->
    <context:component-scan base-package="com.huse.web" />

</beans>

现在不要servlet

现在用controller  控制器  不需要继承什么接口 实现那个类  这个类没有要求 很灵活

传送了一个参数word

el表达式  ${}表达式表示从域中获取数据

往域里面存数据  然后跳转页面

事务注解起作用  -> 使用什么Transaction注解

基于注解开发

导入命名空间

由于spring基于MVC设计模式  需要配置控制器 web.xml(记得改成4.0版本的)

前端控制器(MVC)  用于接收请求  他是一个servlet,他的名字叫做dispatcherServlet

javaweb  程序启动时创建servlet    

配置视图解释器  它可以根据controller的方法设置逻辑视图名 找到对应的物理视图

internalResouceViewResolver类  -> 把他作为一个对象交给spring管理  ->用到bean

返回的逻辑视图名 :hello  -> 我们要的是物理视图名

通过拼接prefix 和 suffix得到物理视图名

注解此时没有被扫描

得去扫满service dao mapper里面的注解

@Controller @Resources  

context....

单例bean得到

通过单例bean得到对象  从而可以调用方法

进行数据转换

一个controller可以解决多个请求,传参和接收会进行自动的数数据类型转换

由于已经被spring管理了

注入service可以实现了

.RequestMappingHandlerMapping 处理器映射器

web.xml:一定要先在web.xml里进行前端控制器的配置,不然controller中的方法不能暴露出去

SpringMVC的流程和原理????

请求->disparcherServlet处理(控制中心),不做事(老板地位)->找到HandlerMapping  处理器映射器,让他根据url去项目的处理器结果一是 返回对象,只是找,拦截器(参数可以绑定到url中的形参的原理)或者结果二是404 -> 处理适配器 每个部门的经理,去找呢手下的人去做事->找到处理器:处理器会进行数据存储和设置一个逻辑视图名,然后进行返回

dispatcherServlet 前端控制器处理请求

geHandler 获得处理器,HandlerMapping

<mvc:annotation-driven>

注解驱动会自动使用基于注解的处理器映射器,处理适配器

其他处理器映射器,处理器适配器处理器映射器:

交给Spring管理

没有注解驱动的话:(先注释掉)

报错:500

原因:

That'sOk

此时绑定了两个Handler

实际开发使用注解的处理器映射器和注解的处理器映射器

只需要配置一个Mvc的处理器映射器的一个配置

<mvc:annotation-driven />

因为mvc这种驱动方式,对于处理器没有限制(方法数量等扽都不限制),继承任何类

提供@RequestMapping @RequestBody @ResponseMapping等等注解

SSM整合

最主要的是Spring和mybatis整合 

SpringMvc是Spring的一个子模块

不用再和Spring进行一个整合了!!

网上书城:

创web-maven项目

web.xml处理一下    book-ssm-system项目名

加java Resource目录 pom.xml改一下jdk版本 然后就是

<spring.version>5.2.15.REALEASE</spring.version>

spring:orm context web test mvc(有依赖传递)

mybatis mybatis-spring(整合用到) mysql 连接池C3p0

servlet-api servlet-jsp

scope:只有打包成war包才会丢掉  test阶段仍然在

tips  -> 跳过测试

spring配置文件 mybatis(sql映射文件 主配置(可省略)) db.properties log4j.properties

Spring与my 整合完毕

配置事务管理器

事务增强

织入:

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值