首页 > 其他分享 >SSM整合02 - Spring整合SpringMVC

SSM整合02 - Spring整合SpringMVC

时间:2023-02-18 21:36:27浏览次数:41  
标签:02 xml Service web SpringMVC Spring SSM Controller 整合

Spring与SpringMVC分别各自管理 Service层和 Controller层

spring.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 https://www.springframework.org/schema/context/spring-context.xsd">

    <!--扫描组件-->
    <context:component-scan base-package="com.atguigu.service.impl"></context:component-scan>

</beans>

springmvc.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"
       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 https://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/mvc https://www.springframework.org/schema/mvc/spring-mvc.xsd">

    <!--扫描控制层组件-->
    <context:component-scan base-package="com.atguigu.controller"></context:component-scan>

    <!-- 配置Thymeleaf视图解析器 -->
    <bean id="viewResolver" class="org.thymeleaf.spring5.view.ThymeleafViewResolver">
        <property name="order" value="1"/>
        <property name="characterEncoding" value="UTF-8"/>
        <property name="templateEngine">
            <bean class="org.thymeleaf.spring5.SpringTemplateEngine">
                <property name="templateResolver">
                    <bean class="org.thymeleaf.spring5.templateresolver.SpringResourceTemplateResolver">
                        <!-- 视图前缀 -->
                        <property name="prefix" value="/WEB-INF/templates/"/>
                        <!-- 视图后缀 -->
                        <property name="suffix" value=".html"/>
                        <property name="templateMode" value="HTML5"/>
                        <property name="characterEncoding" value="UTF-8" />
                    </bean>
                </property>
            </bean>
        </property>
    </bean>

    <mvc:annotation-driven />

    <mvc:view-controller path="/" view-name="index"></mvc:view-controller>

</beans>

在 web.xml中组装整合

<?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>
        <load-on-startup>1</load-on-startup>
    </servlet>
    <servlet-mapping>
        <servlet-name>SpringMVC</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping>

    <!-- Listener > Filter > Servlet -->
    <listener>
        <!--在服务器启动时加载Spring的配置文件-->
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>

    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>classpath:spring.xml</param-value>
    </context-param>

</web-app>

可以看到 web.x,ml中,SpringMVC通过 Servlet创建,而 Spring通过 Listener创建,结合服务器启动时三大组件的执行顺序,Spring的 IoC先于 SpringMVC的 IoC,即 Service层先于 Controller层,因为 Controller层依赖 Service层

标签:02,xml,Service,web,SpringMVC,Spring,SSM,Controller,整合
From: https://www.cnblogs.com/Ashen-/p/17133663.html

相关文章

  • SSM整合03 - Spring整合MyBatis
    MyBatis核心配置文件mybatis-config.xmlSpring整合MyBatis的核心点:将MyBatis核心配置文件中的配置尽可能写入Spring的配置文件中原MyBatis核心配置文件mybatis-config.......
  • PAT-basic-1021 个位数统计 java
    一、题目给定一个 k 位整数 N=dk−1​10k−1+⋯+d1​101+d0​ (0≤di​≤9, i=0,⋯,k−1, dk−1​>0),请编写程序统计每种不同的个位数字出现的次数。例如:给定 N=1......
  • PAT-basic-1022 D进制的A+B java
    一、题目输入两个非负10进制整数 A 和 B (≤230−1),输出 A+B 的 D (1<D≤10)进制数。输入格式:输入在一行中依次给出3个整数 A、B 和 D。输出格式:输出......
  • 20230218读书有感
    1.底部形态要研究,否则是摸底 2.关注,否者做不了期货,不是短期价格波动,而是长期周期 3.入场点(足够高低,形态),做错了怎么办,割肉后继续进 4.高点低点是相对的,可以做,或者......
  • 通过整合lightweight openpose的收获
    1.transformer是对lable和image进行处理。2.训练时图片的批度都是一个大小,验证时可不必遵循。3.paf就是向量图一个x一个y。4.neck是左肩和右肩的关键点。5.最后一个key......
  • 笔记20230218
    <!DOCTYPEhtml><!--savedfromurl=(0060)http://demo.cssmoban.com/cssthemes5/cpts_1121_brc/index.html--><html><head><metahttp-equiv="Content-Type"content="t......
  • PAT 甲级 1002 A+B for Polynomials(25)
    Thistime,youaresupposedtofindA+BwhereAandBaretwopolynomials.InputSpecification:Eachinputfilecontainsonetestcase.Eachcaseoccupies2li......
  • 今日知识分享2022-2-18
    我要学习ros2相关的语法知识,因此利用Google搜索技巧“学习内容+wikitutorial”,发现ros的officialwebsitedocument,里边有关于ros2的tutorial,以及howtoinstallandque......
  • 「解题报告」AHOI2022 排列
    这个标题格式我才看出来它的优越性,如果用「AHOI2022排列题解」这种写法会感觉「AHOI2022」「排列」「题解」是并列地位的,比较奇怪,我目前就想到这一种解决方案,也就是APJ......
  • GitHub 入门 与 2023年2月18日10:29:02
    用GitHub有一段时间了,之前一直用来做Hexo的服务器,直到前阵子搞GitHubAction因为命令不熟,把GitHub上的源码强制拉到本地把本地的Hexo搞崩了,博客源码都没了,哭辽......