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