整合Mybits,Spring,SpringMVC(SSM整合开发)的网络Web程序----OA办公系统(一)

Spring和SpringMVC父子容器关系

在Spring整体框架的核心概念中,容器是核心思想,就是用来管理Bean的整个生命周期的,而在一个项目中,容器不一定只有一个,Spring中可以包括多个容器,而且容器有上下层关系,目前最常见的一种场景就是在一个项目中引入Spring和SpringMVC这两个框架,那么它其实就是两个容器,Spring是父容器,SpringMVC是其子容器,并且在Spring父容器中注册的Bean对于SpringMVC容器中是可见的,而在SpringMVC容器中注册的Bean对于Spring父容器中是不可见的,也就是子容器可以看见父容器中的注册的Bean,反之就不行。

搭建一个javaee项目
在这里插入图片描述

导入SSM整合需要的jar包
这里附上链接:链接 提取码: mbx6
关于每个jar包的用途,在看完这个博主的文章后很有启发
ssm框架常用jar包认识

导入jar包
在这里插入图片描述
添加applicationContext-dao.xml,applicationContext-service.xml,applicationContext-tracs.xml,springmvc.xml在src目录下
在这里插入图片描述
给每个文件添加约束头:
我把约束头文件都放在这里:
https://download.csdn.net/download/qq_43171656/12681988
把约束头加进每个xml文件中

各个配置文件的作用:
在这里插入图片描述
tracs.xml:AOP面向切面编程的核心配置文件,AOP在前面的讲过
添加log4j.propert日志控制文件

Log4j是Apache的一个开源项目,通过使用Log4j,我们可以控制日志信息输送的目的地是控制台、文件、GUI组件,甚至是套接口服务器、NT的事件记录器、UNIX Syslog守护进程等;我们也可以控制每一条日志的输出格式;通过定义每一条日志信息的级别,我们能够更加细致地控制日志的生成过程。最令人感兴趣的就是,这些可以通过一个配置文件来灵活地进行配置,而不需要修改应用的代码。

这里是链接:log4j.properties
构建基本的文档目录结构
在这里插入图片描述

配置applicationContext-dao.xml
mybatis的基本配置,用于配置数据库,链接数据库,在前面启用pagehelper插件也是在这里配置
在这之前先添加db.properties数据库的连接配置文件,里面加载数据库连接的信息

<?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:property-placeholder location="classpath:db.properties"></context:property-placeholder>
<!--    加载数据库-->
    <bean class="org.springframework.jdbc.datasource.DriverManagerDataSource" name="dataSource">
        <property name="driverClassName" value="${jdbc.driver}"></property>
        <property name="url" value="${jdbc.url}"></property>
        <property name="username" value="${jdbc.username}"></property>
        <property name="password" value="${jdbc.password}"></property>
    </bean>
<!--    加载sqlsession工厂-->
    <bean class="org.mybatis.spring.SqlSessionFactoryBean">
        <property name="dataSource" ref="dataSource"></property>
    </bean>
<!--    把mapper下面所有的类都交给Spring管理-->
    <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
        <property name="basePackage" value="com.oa.mapper"></property>
    </bean>
</beans>

配置applicationContext-service.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"> <!-- bean definitions here -->
<!--    把service文件夹下面的所有类交给spring管理-->
    <context:component-scan base-package="com.oa.service"></context:component-scan>
</beans>

配置springmvc.xml
作用:扫描controller控制器,配置注释来使用springmvc,放行静态资源,配置jsp,html视图解析器,
在配置springmvc之前先导入这次oa办公系统的前端部分
我把资源放在这里:oa办公系统的前端部分界面原型
导入后项目的结构如下:
在这里插入图片描述
注意这里要把文件都放在WEB-INF文件下面,这样可以防止别人直接访问你的资源,只有自己可以通过控制器访问
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:p="http://www.springframework.org/schema/p"
       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/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
<!--    扫描controller下面的所有控制器交给spring管理-->
    <context:component-scan base-package="com.oa.controller"></context:component-scan>
<!--    启用注释来配置springmvc-->
    <mvc:annotation-driven></mvc:annotation-driven>
<!--    放行静态资源-->
    <mvc:resources mapping="/css/**" location="/WEB-INF/css/"></mvc:resources>
    <mvc:resources mapping="/images/**" location="/WEB-INF/images/"></mvc:resources>
    <mvc:resources mapping="/js/**" location="/WEB-INF/js/"></mvc:resources>
    <mvc:resources mapping="/editor/**" location="/WEB-INF/editor/"></mvc:resources>
    <mvc:resources mapping="/My97DatePicker/**" location="/WEB-INF/My97DatePicker/"></mvc:resources>
<!--    配置jsp视图解析器,把加工好的视图返回-->
    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix" value="/WEB-INF/jsp/"></property> <!--前缀-->
        <property name="suffix" value=".jsp"></property> <!--后缀-->
        <property name="order" value="1"></property> <!--优先级-->
    </bean>

    <!-- html视图解析器 -->
    <!-- 配置freeMarker的模板路径 -->
    <bean class="org.springframework.web.servlet.view.freemarker.FreeMarkerConfigurer">
        <property name="templateLoaderPath">
            <value>/WEB-INF/</value>  <!--前缀-->
        </property>
        <property name="defaultEncoding">
            <value>utf-8</value>
        </property>
    </bean>
    <!-- 配置freeMarker视图解析器 -->
    <bean class="org.springframework.web.servlet.view.freemarker.FreeMarkerViewResolver">
        <property name="viewClass" value="org.springframework.web.servlet.view.freemarker.FreeMarkerView"/>
        <property name="contentType" value="text/html; charset=utf-8"/>
        <property name="suffix" value=".html" />  <!--后缀-->
        <property name="order" value="0"/>  <!--解析器的优先级,数字越小优先级越高-->
    </bean>
</beans>

配置web.xml文件
在web.xml主要配置servlet和filter的映射关系

<?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">
<!--    配置在开始时载入applicationContext-service.xml,applicationContext-dao.xml,applicationContext-tracs.xml-->
    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>classpath:applicationContext-*.xml</param-value>
    </context-param>
    
<!--    配置监听器,在tomcat启动时,加载-contextServlet-->
    <listener>
        <display-name></display-name>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>
    
<!--    配置中央调度器-->
    <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>
    
    <!--    配置中央过滤器-->
    <!--    过滤所有请求编码为utf-8-->
    <filter>
        <filter-name>characterEncodingFilter</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>characterEncodingFilter</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>
</web-app>

最后,添加tomcat的服务器,前期的配置工作就大功告成,可以尝试启动服务器
在这里插入图片描述
在这里插入图片描述
导入成功后,尝试运行:
运行成功:
在这里插入图片描述
尝试看能不能切换到login页面,因为已经配置了视图解析器,尝试看看能不能进入login页面:
在这里插入图片描述
显示404,想起还没有写页面控制器,这里我们写第一个页面控制器,控制页面的跳转:
com.oa.controller.PageController:

package com.oa.controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

@Controller
public class PageController {
    @RequestMapping(value = "/{page}",produces = "text/html;charset=utf-8")
    public String page(@PathVariable String page){
        return page;
    }
}

再次输入:http://localhost:8080/OaProject/login尝试进入登陆页面
结果成功
在这里插入图片描述

可以看到这只是html的静态资源,按钮的控件都没有响应,也没有页面间的控制跳转和数据库连接。

总结:

初步搭建了大致框架,后续在继续完善页面的控制跳转和各个功能的实现,主要功能有登录,添加员工,查看员工信息,添加职位,查看职位,签到,分页查询等功能

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值