java之SSM整合案例 - 2:spring和springmvc整合

接着上一期的说,上一期建立了项目,编写了接口和实现类,还有javabean对象

这一期就说说如何将spring和springmvc进行整合,

在前端点击超链接时,可以请求到后端控制器的方法,然后控制器调用业务层,最后返回给前端

Let's begin~

 

一、编写spring的配置文件applicationContext.xml

要注意:spring只需要扫描业务层,控制器是不需要spring来管的,控制器是交给spring mvc管的

所以在配置扫描的包时,要讲Controller这个注解过滤掉,不扫描

上述中spring的xml引入:

<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:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
       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/tx
        http://www.springframework.org/schema/tx/spring-tx.xsd">

 

二、给服务层加上注解,进行依赖注入

 

ok,这样spring层,就配置完毕了,我们来使用junit测试一下

添加test文件夹,添加测试类

注意:如果在开发过程中无法使用到Test注解,去pom.xml中看一下junit的作用于是否太小,如果是test,那么只能在测试环境下使用,这里为了避免麻烦,我们就改为编译期间,改为compile

 

好了,spring配置完毕,接下来我们来配置spring mvc

 

一、在resources文件夹下添加springmvc的配置文件,并配置

1.扫描(注意:只扫描带有Controller注解的,因为业务层的已经交给了spring去管理了)

2.配置试图解析器

3.静态资源过滤器

4.启用spring mvc注解支持

springmvc的xml提示:

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

 

 

二、在web.xml配置:

1.前端控制器

2.中文编码过滤器

 

ok,spring mvc我们也配置完毕,我们来测试一下吧

1.后台接口添加Controller注解和RequestMapping注解,可以让前端调用

2.后端接口返回一个jsp的名称

3.在index.jsp中给一个a标签,href属性请求到后端控制器的方法

4.在spring mvc的配置文件的试图解析器要求的文件夹下创建指定的jsp页面,请求完毕跳转使用

5.配置tomcat

 

然后运行就可以了

 

 

至此,spring和spring mvc我们都配置完毕了,

 

但是我们想要的结果是,先要在controller中调用业务层,

我们在测试spring时是通过指定spring配置文件路径来加载的spring

但是我们此时没有地方加载spring,如何在controller中使用呢

 

解决办法:

我们在web.xml中配置了前端控制器和乱码过滤器,这个都是在服务器启动时就会生效,我们能否在web.xml中也读取spring配置文件,然后加载进去,然后我们就可以在程序中直接使用了

当然可以,这里要使用监听器,在服务器启动的时候加载spring配置文件

 

这样就可以了,然后我们就可以在controller中去调用业务层了

 

 

Ending~

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值