前端使用angularjs的$resource发送
这是方法:
@RequestMapping(value = "/studentTest")
public class StudentController {
/**
* 测试
*/
@RequestMapping(value = "/save",method = RequestMethod.POST,consumes = "application/json")
@ResponseBody
public HashMap<String,Object> save(@RequestBody Student student){
HashMap<String,Object> hashMap = new HashMap<String,Object>();
ApplicationContext ctx = new ClassPathXmlApplicationContext("springmvc.xml");
StudentDao studentDao = (StudentDao)ctx.getBean("StudentBean");
if(studentDao.save(student)){
hashMap.put("error", 0);
hashMap.put("msg", "插入测试成功");
}else{
hashMap.put("error", 1);
hashMap.put("msg", "插入测试失败");
}
return hashMap;
}
}
这是前端angularjs的$resource
TestApp.factory('TestService',function($resource){
return $resource(projectName + '/student/courseList',{},{
insert: {
url: projectName + '/studentTest/save',
method: 'POST',
isArray: false,
}
});
});
$scope.registerInfo = [{
'snumber': 'qubxxk',
'sname': "接口测试",
'sage': 10
}]
TestService.insert($scope.registerInfo,function(res){
if(res.error === 0){
if(res.msg !== null){
console.log(res.msg);
}else{
console.log("插入测试成功");
}
}else{
if(res.msg !== null){
console.log(res.msg);
}else{
console.log("插入测试失败");
}
}
console.log(res);
//恢复按钮状态
$scope.test_btn = '插入测试';
$scope.insert_disabled = false;
},function(){
console.log("api连接失败");
$scope.test_btn = '插入测试';
$scope.insert_disabled = false;
});
下面是配置文件springmvc.xml
``
<!--配置注解映射器和适配器 (这样不需要单独配置json转化器) -->
<mvc:annotation-driven/>
<!--因为在web.xml中配置的url-pattern所有的都由DispatcherServlet处理,而静态文件不需要,所以按如下配置 -->
<!--配置静态文件 -->
<!-- <mvc:default-servlet-handler/> -->
<mvc:resources location="WEB-INF/js/" mapping="/js/**"/>
<mvc:resources location="img/" mapping="/img/**"/>
<mvc:resources location="Public/" mapping="/Public/**"/>
<!-- 扫描Spring注解的配置 -->
<!-- 对于注解的Handler可以单个配置,实际开发中建议使用组件扫描 -->
<!-- 可以扫描controller、service、...这里让扫描controller,指定controller的包 -->
<!-- <context:component-scan base-package="com.tgb.itoo.controller"></context:component-scan> -->
<bean class="com.tgb.itoo.controller.CourseController" />
<bean class="com.tgb.itoo.controller.IndexController" />
<bean class="org.controller.StudentController" />`
另一个配置文件
${driverClassName}
${url}
${username}
<!-- ${password} -->
<!-- 告知Hibernate的sessionFactory -->
<bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
<property name="dataSource" ref="dataSource"></property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.MySQL5Dialect</prop>
<prop key="hibernate.show_sql">true</prop>
<prop key="hibernate.format_sql">true</prop>
<prop key="current_session_context_class">thread</prop>
</props>
</property>
<property name="mappingResources">
<list>
<value>org/bean/Student.hbm.xml</value>
</list>
</property>
</bean>
<bean id="StudentBean" class="org.dao.StudentDaoImp">
<property name="sessionFactory">
<ref bean="sessionFactory"/>
</property>
</bean>
![图片说明](https://img-ask.csdn.net/upload/201604/25/1461563276_434072.png)