首页 > 其他分享 >ssm框架整合 理解及搭建

ssm框架整合 理解及搭建

时间:2023-04-23 15:34:44浏览次数:32  
标签:xml web 框架 spring ssm controller mvc org 搭建


如何开发一个java-web的开发模式 。三大块 前端 后端 存储。分层 ,首先用户的请求 到 view ,view 调后端controller,controller业务逻辑处理存储,数据模型层 model。按照这种模式开发。用框架实现mvc 。目前用springmvc,最早期的 controller层用的是struts1,servlet,再往后是struts+hibernate,现在开发用的是spring+ibatis (是m层)。

整个工程的目录结构   。mvc是设计模式,开发中也有三层结构 controller service dao,controller是一个控制转发的,service负责业务逻辑实现,dao与数据交互。中间传输对象一般是bean。两个方向。一个是设计思想,一个是开发的规范。controller 是由两个 springmvc+前端 组成。持久层主要内容是dao,model作为bean存在于整个环节,只要需要。现在controller层主要是框架,strut2,springmvc。但他们的组成部分中都在servlet。不过也还会有一些其它组件。

ssm框架整合 理解及搭建_spring

 

 

在web.xml 文件中配置两个大项,一个是ContextLoaderlListener (内容加载监听),contextConfigLocation,。DispatcherServlet(调度程序容器),主要配置名称、路径<servlet>  </servlet>, <servlet-mapping>  </servlet-mapping>。   在<servlet> 中 配置出spring-mvc 路径,进而配置spring-mvc.xml, 在spring-mvc中配置一个是<context:component-scan,代表自动扫描 controller, 另一个配置 视图解析工具

ssm框架整合 理解及搭建_spring_02

两个坑 一个是 在配置web.xml时候 <servlet-mapping> 配置成*.do。  一个是在spring-mvc.xml中,视图解析工具名称是VelocityViewResolver,而不是VelocityLayoutViewResolver。

 

ssm框架整合 理解及搭建_mvc_03

首先配置web.xml ,对servlet做映射,映射到spring上去,

 

ssm框架整合 理解及搭建_mvc_04

 

ssm框架整合 理解及搭建_spring_05

 

在配置web.xml的时候加入依赖,导入spring-mvc 、spring-web的jar包,配置完web.xml后,配置applicationContext.xml、datasource.xml、spring-mvc.xml。

web.xml内容

 

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
    http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">

    <!-- 配置web.xml,使其具有springmvc特性,主要配置两处,一个是ContextLoaderListener,一个是DispatcherServlet -->

    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>classpath*:spring/applicationContext.xml</param-value>
    </context-param>
    <!-- 配置ContextLoaderListener表示,该工程要以spring的方式启动。启动时会默认在/WEB-INF目录下查找applicationContext.xml
  作为spring容器的配置文件,该文件里可以初始化一些bean -->
    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>

    <!-- 字符集过滤器 -->
    <filter>
        <filter-name>encodingFilter</filter-name>
        <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
        <init-param>
            <param-name>encoding</param-name>
            <param-value>UTF-8</param-value>
        </init-param>
    </filter>
    <filter-mapping>
        <filter-name>encodingFilter</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>

    <!-- spring mvc 配置 -->
    <!-- 配置DispatcherServlet表示,该工程将采用springmvc的方式。启动时也会默认在/WEB-INF目录下查找XXX-servlet.xml作为配置文件,
        XXX就是DispatcherServlet的名字,该文件中将配置两项重要的mvc特性:HandlerMapping,负责为DispatcherServlet这个前端控制器的请求查找Controller;
        ViewResolver,负责为DispatcherServlet查找ModelAndView的视图解析器。
        此处使用指定的配置文件spring-mvc.xml -->
    <servlet>
        <servlet-name>contacts</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <init-param>
            <param-name>contextConfigLocation</param-name>
            <!--<param-value>/WEB-INF/classes/spring-mvc-servlet.xml</param-value>-->
            <param-value>classpath*:/spring/spring-mvc.xml</param-value>
        </init-param>
        <load-on-startup>1</load-on-startup>
    </servlet>
    <servlet-mapping>
        <servlet-name>contacts</servlet-name>
        <url-pattern>*.do</url-pattern>
    </servlet-mapping>

    <welcome-file-list>
        <welcome-file>index.jsp</welcome-file>
    </welcome-file-list>
</web-app>

 

 

 

applicationContext.xml,

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

 

 

 

 

 

 

 

datasource.xml,配置数据源,数据源都有哪些,为什么要用数据源,本身是一个连接池,不同类型的连接池,德鲁伊druid, c3p0,自己写的jdbc 不用数据源,没有抽象出一个池子,没有池,即用即销毁,如果对db连接频繁,会创建很多对象,创建很多链接对象,在old区,会有很多对象,会导致频繁full gc,这样tps上不去,这是用数据源的其中一个原因,用一个池子管理,初始化那么多对象,用过后,扔到池子里,不用再创建新的连接对象,避免jvm产生大量fullgc。bean 依赖注入概念,spring特性注入叫ioc,自动往代码里new 对象,new的对象是单例,对象不可能是一个,ref 指引用的是哪个bean。

 

 

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

    <context:property-placeholder location="classpath:db.properties"/>
    <!-- 配置 C3P0 数据源 -->
    <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
        <property name="user" value="root"></property>
        <property name="password" value="empty"/>
        <property name="jdbcUrl" value="jdbc:mysql://localhost:3306/test"></property>
        <property name="driverClass" value="com.mysql.jdbc.Driver"></property>
        <!-- 当连接池中的连接用完时,C3P0一次性创建新连接的数目 -->
        <property name="acquireIncrement" value="5"></property>
        <!-- 初始化时创建的连接数,必须在minPoolSize和maxPoolSize之间 -->
        <property name="initialPoolSize" value="10"></property>
        <property name="maxPoolSize" value="20"></property>
        <property name="minPoolSize" value="5"></property>
        <!-- jdbc的标准参数 用以控制数据源内加载的PreparedStatement数量,但由于预缓存的Statement属于单个Connection而不是整个连接 -->
        <property name="maxStatements" value="20"></property>
    </bean>
    <bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
        <property name="dataSource" ref="dataSource"/>
    </bean>
    <bean id="transactionManager"
          class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        <property name="dataSource" ref="dataSource"></property>
    </bean>
    <!--<bean id="serverDao" class="com.testing91.dao.OrderDao">-->
    <!--<property name="jdbcT">-->
    <!--<ref bean="jdbcTemplate"/>-->
    <!--</property>-->
    <!--</bean>-->
</beans>

 

 

 

 

spring-mvc.xml,前端框架用的velocity,是mvc显示层的一个框架,最早期的是servlet ,后来用jsp映射,现在用velocity,需要配置一个引擎、一个解析工具,用pom.xml文件引入velocity的依赖,velocity 在这个引擎下面,解析velocity的文件,前端文件,前端文件就是加载前端的资源,会配置一个路径,加载哪个路径下的资源,就是在配置的路径,加载完了后,解析加载的文件。组建扫描,前端发请求找到后端的controller,自动扫描controller,工程机起动会加载出所有的controller文件,包名必须一致。

 

 

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:mvc="http://www.springframework.org/schema/mvc"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:aop="http://www.springframework.org/schema/aop"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
    http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
    http://www.springframework.org/schema/context
    http://www.springframework.org/schema/context/spring-context-3.1.xsd
    http://www.springframework.org/schema/mvc
    http://www.springframework.org/schema/mvc/spring-mvc-3.1.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd">

    <aop:aspectj-autoproxy proxy-target-class="true"/>
    <mvc:default-servlet-handler/>

    <mvc:annotation-driven>
        <mvc:message-converters>
            <bean class="com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter">
                <property name="features">
                    <array>
                        <!-- List字段如果为null,输出为[],而非nul -->
                        <value>WriteNullListAsEmpty</value>
                        <!-- 字符类型字段如果为null,输出为"",而非null -->
                        <value>WriteNullStringAsEmpty</value>
                        <!-- 进制循环引用 -->
                        <value>DisableCircularReferenceDetect</value>
                        <!--Boolean字段如果为null,输出为false,而非null -->
                        <value>WriteNullBooleanAsFalse</value>
                        <!--时间 yyyy.MM.dd hh:mm:ss -->
                        <value type="com.alibaba.fastjson.serializer.SerializerFeature">WriteDateUseDateFormat</value>
                    </array>
                </property>
            </bean>
        </mvc:message-converters>
    </mvc:annotation-driven>

    <!--<mvc:resources mapping="/assets/**" location="/assets/"/>-->
    <!--velocity引擎-->
    <bean id="velocityConfigurer"
          class="org.springframework.web.servlet.view.velocity.VelocityConfigurer">
        <property name="resourceLoaderPath" value="/WEB-INF/view"/>
        <!-- 設置模板防止位置-->
        <property name="velocityProperties">
            <props>
                <prop key="input.encoding">UTF-8</prop>
                <prop key="output.encoding">UTF-8</prop>
            </props>
        </property>
    </bean>

    <!-- 设置视图解析工具 -->
    <bean id="viewResolver"
          class="org.springframework.web.servlet.view.velocity.VelocityViewResolver">
        <property name="suffix" value=".vm"/>
        <property name="layoutUrl" value="/layout/layout.vm"/>

        <!-- 避免乱码 -->
        <property name="contentType" value="text/html;charset=UTF-8"/>
        <property name="dateToolAttribute" value="dateTool"/>
        <property name="numberToolAttribute" value="numberTool"/>
        <property name="exposeRequestAttributes" value="false"/>
        <property name="exposeSessionAttributes" value="true"/>
    </bean>
<!-- 组建扫描,自动扫描controller,包名必须一致 -->
<context:component-scan base-package="com.testing91.model"/>

</beans>

 

 

 

 

 

db.properties 配置

 

dbuser=root
dbpassword=
driver=com.mysql.jdbc.Driver
url=jdbc:mysql://localhost:3306/test

 

 

 

 

pom.xml 配置内容

 

<?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>
    <packaging>war</packaging>
    <groupId>com.91testing</groupId>
    <artifactId>Demo</artifactId>
    <version>1.0-SNAPSHOT</version>
    <properties>
        <spring.version>3.2.13.RELEASE</spring.version>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    </properties>
    <dependencies>
        <!-- https://mvnrepository.com/artifact/org.springframework/spring-webmvc -->
        <!-- https://mvnrepository.com/artifact/org.springframework/spring-webmvc -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-webmvc</artifactId>
            <version>4.1.6.RELEASE</version>
        </dependency>
        <!-- https://mvnrepository.com/artifact/org.springframework/spring-context-support -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context-support</artifactId>
            <version>4.1.6.RELEASE</version>
        </dependency>


        <!-- https://mvnrepository.com/artifact/javax.servlet/servlet-api -->
        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>servlet-api</artifactId>
            <version>2.5</version>
        </dependency>

        <!-- https://mvnrepository.com/artifact/c3p0/c3p0 -->
        <dependency>
            <groupId>c3p0</groupId>
            <artifactId>c3p0</artifactId>
            <version>0.9.1.2</version>
        </dependency>
        <!-- https://mvnrepository.com/artifact/org.springframework/spring-jdbc -->
        <!-- https://mvnrepository.com/artifact/org.springframework/spring-jdbc -->


        <!-- https://mvnrepository.com/artifact/org.springframework/spring-jdbc -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-jdbc</artifactId>
            <version>4.1.6.RELEASE</version>
        </dependency>

        <!-- https://mvnrepository.com/artifact/com.alibaba/fastjson -->
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>fastjson</artifactId>
            <version>1.2.31</version>
        </dependency>
        <!-- https://mvnrepository.com/artifact/org.apache.velocity/velocity -->
        <dependency>
            <groupId>org.apache.velocity</groupId>
            <artifactId>velocity</artifactId>
            <version>1.7</version>
        </dependency>
        <!-- https://mvnrepository.com/artifact/org.apache.velocity/velocity-tools -->
        <dependency>
            <groupId>org.apache.velocity</groupId>
            <artifactId>velocity-tools</artifactId>
            <version>2.0</version>
        </dependency>
        <!-- mysql驱动包 -->
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>5.1.29</version>
        </dependency>
        <!-- junit测试包 -->
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.11</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.aspectj</groupId>
            <artifactId>aspectjrt</artifactId>
            <version>1.8.0</version>
        </dependency>
        <dependency>
            <groupId>org.aspectj</groupId>
            <artifactId>aspectjweaver</artifactId>
            <version>1.8.0</version>
        </dependency>
    </dependencies>
    <build>
        <finalName>Demo</finalName>
    </build>
</project>

 

 

 

 

 

 

 

ssm框架整合 理解及搭建_mvc_06

ssm框架整合 理解及搭建_mvc_07


 


 



 


 


 


 


 


 


 


 


 


 


 


 

标签:xml,web,框架,spring,ssm,controller,mvc,org,搭建
From: https://blog.51cto.com/u_16084838/6217756

相关文章

  • mysql主从-day1——mysql主从搭建、django中使用多数据库做读写分离
    目录一、mysql主从5django使用多数据库做读写分离一、mysql主从#之前做过redis的主从,很简单#mysql稍微复杂一些,搭建mysql主从的目的是? -读写分离-单个实例并发量低,提高并发量-只在主库写,读数据都去从库#mysql主从原理步骤一:主库db的更新事件......
  • 自动化测试框架pytest教程
    快速入门准备安装推荐安装anaconda,默认带pytest#pip3installpytest...$pytest-h#查看帮助...参考资料:https://pytest.orghttps://pypi.org/project/pytest第一个实例(通过)deftest_passing():assert(1,2,3)==(1,2,3)函数test_passing()......
  • 基于SqlSugar的开发框架循序渐进介绍(28)-- 快速构建系统参数管理界面
    在参照一些行业系统软件的时候,发现一个做的挺不错的系统功能-系统参数管理,相当于把任何一个基础的系统参数碎片化进行管理,每次可以读取一个值进行管理,这样有利于我们快速的处理业务需求,是一个挺好的功能。本篇随笔模拟这个功能,基于SqlSugar开发框架的基础上,利用代码生成工具快速生......
  • Unity框架:JKFrame2.0学习笔记(十)——自动生成资源引用代码(2)
    前言上一篇记录了自动生成资源引用代码的内部实现,主要是针对addressable的资源系统的,为了在加载时不会因为名字写错,加载错,也更加方便的使用addressable加载,这一篇记录下如何使用。如何使用之前看过,在编辑器中添加了工具按钮我们可以在addressable的groups面板上添加几个测试资源我......
  • 手把手教你使用Flask搭建ES搜索引擎(预备篇)
    今日鸡汤我死国生,我死犹荣,身虽死精神长生,成功成仁,实现大同。--赵博生/1前言/    Elasticsearch是一个开源的搜索引擎,建立在一个全文搜索引擎库ApacheLucene™基础之上。    那么如何实现 Elasticsearch和Python 的对接成为我们所关心的问题了(怎么什么都要和Pyt......
  • 3个迹象表明,企业是时候搭建CDP了!
    和过去任何时代相比,当下的数字化程度都更加深入,且还在持续加速的进程中。如今,客户数据的重要性已经毋庸置疑。企业应该如何应用数字化改进客户体验,以及由此产生的海量客户数据,已经成为新的焦点。许多企业通过使用客户数据平台(CDP),游刃有余地驾驭了众多品牌和客户的复杂数据。那么问......
  • 日志门面、实现框架和桥接器及实际使用
      之前总是在项目中使用现成的日志框架,用着方便就不会去思考它的框架、发展和组成,别人怎么用我也怎么用。感觉就是很模糊不清楚,说不知道也知道点,说知道又讲不明白,看了不少文章,决定把这一块梳理一下。1、现有日志的组成  可能说到日志,大家都知道一部分,什么slf4j、logback等等......
  • SQL Server2022以及SQL Server Management Studio(SSMS)的下载和安装
    1.下载安装包:浏览搜索SQLSERVER2022 2.进入页面后,点击下载 3.页面下拉,选择安装windows版,点击选择安装设置 4.选择在window上安装 5.填写自己信息:姓名手机号邮箱等;(这里可以随便填) 6.点击Downloadnow,等待下载完成 7.下载之后打开下载文件,选择下载介质 8.......
  • 16.InnoDB Cluster 简单搭建
    1.主机规划列表、iphostname主机角色安装软件192.168.10.134db01mastermysql、myshell192.168.10.130db02slavemysql、myshell192.168.10.131db03slavemysql、myshell192.168.10.132db04managermyshell、myrouter      2.......
  • Python 环境搭建
    Python环境搭建https://www.python.org/downloads/      验证  ......