Spring整合SpringMVC

1.Spring整合Springmvc

在这里插入图片描述
整合之后,让springmvc管理控制层,让spring管理service层和dao层!

1.1整合步骤

第一步:导入spring和springmvc、json、jstl、mysql、c3p0对应的jar包
第二步:在web.xml文件中配置spring的监听器和springmvc的前端控制器以及两个Filter
第三步:创建spring和springmvc的配置文件,并配置他们
Springmvc.xml
配置扫描包
配置内部资源视图解析器
Mvc:default-servlet-handler
Mvc:annotation-driven:
applicationContext.xml
配置扫描包
加载properties配置文件
配置数据源
配置JdbcTemplate
配置数据源事务管理器
开启基于注解的事务支持

第四步:测试

扫描包问题,两种解决方案:
第一种:
Springmvc.xml

<context:component-scan base-package="com.offcn.controller"/>

applicationContext.xml:

<context:component-scan base-package="com.offcn.service,com.offcn.dao"/>

第二种:
Springmvc.xml

 <context:component-scan base-package="com.offcn" use-default-filters="false">
      <context:include-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
      <context:include-filter type="annotation" expression="org.springframework.web.bind.annotation.ControllerAdvice"/>
   </context:component-scan>

applicationContext.xml

<context:component-scan base-package="com.offcn">
      <context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
      <context:exclude-filter type="annotation" expression="org.springframework.web.bind.annotation.ControllerAdvice"/>
   </context:component-scan>

1.2Spring的容器和springmvc容器的关系在这里插入图片描述

使用整合之后的框架做一个对产品列表的增删改查操作,建议使用RESTful风格的请求
同步请求:
添加: 添加页面[a:GET]-添加【POST】
修改: 修改页面【id查询:GET】-修改 【PUT】
查询: GET
删除: DELETE

如何将GET请求转换为PUT或者DELETE请求?
借助于js代码和form表单完成:
Js代码:

$(function(){
	 //1.jquery 1.简化代码 2.jquery解决了部分浏览器兼容性 3.当为一组元素绑定事件的时候,就相当于为这一组元素中的每个元素都绑定了一个事件
	 $("a[id ^= delete_]").click(function(){
		 var href = $(this).attr("href");
		 $("#form01").attr("action",href).submit();
		 //取消超链接的默认行为
		 return false;
	 });
 });

form表单:

 <form id="form01" action="" method="post">
       <input type="hidden" name="_method" value="delete">
   </form>

GET请求—》POST请求—》DELETE

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值