vue和Java做数据交互_基于vue和springmvc前后端分离,json类接口调用介绍
基于vue和springmvc前后端分离,json类接口调用介绍
版本要求:spring-3.2.9.RELEASE、vue-2.9.2、axios-0.17.1,其中axios作为http client供vue访问springmvc的接口。
1,maven配置
org.springframework
spring-aop
3.2.9.RELEASE
org.springframework
spring-beans
3.2.9.RELEASE
org.springframework
spring-context
3.2.9.RELEASE
org.springframework
spring-core
3.2.9.RELEASE
org.springframework
spring-expression
3.2.9.RELEASE
org.springframework
spring-web
3.2.9.RELEASE
org.springframework
spring-webmvc
3.2.9.RELEASE
org.springframework
spring-tx
3.2.9.RELEASE
org.springframework
spring-jdbc
3.2.9.RELEASE
javax.servlet
servlet-api
2.5
commons-lang
commons-lang
2.5
log4j
log4j
1.2.16
com.fasterxml.jackson.core
jackson-databind
2.9.0
com.fasterxml.jackson.core
jackson-core
2.9.0
2,spring配置,spring-app.xml
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"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.2.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.2.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.2.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd">
3.编写控制类
package spring.mvc.controler;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import spring.mvc.model.User;
@Controller
@Scope("prototype")
@RequestMapping(value="/test")
public class HelloWorldController {
@RequestMapping(value="/users/{userName}", method=RequestMethod.GET)
public @ResponseBody User findUser(@PathVariable("userName") String userName) {
User user = new User();
user.setUserName(userName);
user.setSex("男");
return user;
}
@RequestMapping(value="/users/add", method=RequestMethod.POST)
public @ResponseBody Long createUser(@RequestBody User user) {
user.setId(System.currentTimeMillis());
userMapper.insert(user);
return user.getId();
}
}
4.编写实体类
package spring.mvc.model;
public class User {
private Long id;
private String userName;
private String sex;
public User() {
}
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getUserName() {
return userName;
}
public void setUserName(String userName) {
this.userName = userName;
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
}
5,编写跨域filter,如果vue容器和springmvc服务容器分开部署,则在springmvc返回消息中增加Access-Control-Allow-Origin为*。
package spring.mvc.filter;
import java.io.IOException;
import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletResponse;
public class CorsFilter implements Filter{
@Override
public void destroy() {
// TODO Auto-generated method stub
}
@Override
public void doFilter(ServletRequest arg0, ServletResponse arg1, FilterChain arg2)
throws IOException, ServletException {
// TODO Auto-generated method stub
HttpServletResponse httpServletResponse = (HttpServletResponse) arg1;
httpServletResponse.setHeader("Access-Control-Allow-Origin", "*");
arg2.doFilter(arg0, httpServletResponse);
}
@Override
public void init(FilterConfig arg0) throws ServletException {
// TODO Auto-generated method stub
}
}
6,假设springmvc服务部署在tomcat,应用名称为springmvc
web.xml配置如下
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
index.html
springMVC
org.springframework.web.servlet.DispatcherServlet
contextConfigLocation
classpath*:/spring-app.xml
1
springMVC
/
default
*.html
corsFilter
spring.mvc.filter.CorsFilter
corsFilter
/*
7,vue_hello.html编写,注意{host}和{port}按实际情况设置
{{message}}
查询用户
var app99 = new Vue({
el: '#app99',
data: {
message: 'user'
},
methods: {
findUser: function() {
axios.get('http://{host}:{port}/springmvc/test1/users/1')
.then(response => {
console.log(response.data);
this.message = response.data;
})
.catch(error => {
console.log(error);
this.message = error;
})
}
}
})
8,启动tomcat,然后用浏览器打开vue_hello.html,页面显示如下
user
查询用户
点击“查询用户”按钮,页面显示如下
{ "id": null, "userName": "1", "sex": "男" }
查询用户
10,至此,基于vue和springmvc,可实现前后端分离,两者之间通过http+json交互数据。
vue和Java做数据交互_基于vue和springmvc前后端分离,json类接口调用介绍相关推荐
- 黑加仑妞 使用vue+flask做全栈开发的全过程(实现前后端分离)
黑加仑妞 使用vue+flask做全栈开发的全过程(实现前后端分离) 花了几天的时间终于在本地把前后端跑通了,以一篇博客记录我这几天的心酸... 1.安装nodejs(自带npm,可能会出现版本错误, ...
- 学科投票系统-基于Python-Django实现的前后端分离项目
学科投票系统-基于Python-Django实现的前后端分离开发 作者:代昌松 项目详情代码请参考: vote_api:https://gitee.com/dcstempt_ping/vote_api ...
- 2021年数据库课设该怎么做?一个超市管理系统,简单的前后端分离项目,带你从概要设计走到项目发布!(Vue.js+SpringBoot+MybatisPlus)
由于本文格式原来是word,所以文中有些格式不太对.如果这篇文章对你有帮助,麻烦点赞评论一下谢谢!源码和word文档可私聊领取~ 目录 一.相关技术介绍 1.1 RDBMS: 1.2应用程序开发环境: ...
- echarts前后端交互数据_SpringBoot2.0实战(26)整合SpringSecurity前后端分离JSON交互...
在前端的文章中,我们实现了使用 SpringSecurity 实现登录鉴权,并使用数据库存储用户信息,实现登录鉴权 SpringBoot2.0实战(24)整合SpringSecurity之最简登录方法 ...
- cnn生成图像显著图_基于CNN与图像前背景分离的显著目标检测
基于 CNN 与图像前背景分离的显著目标检测 东野长磊 ; 万文鑫 [期刊名称] <软件导刊> [年 ( 卷 ), 期] 2020(019)001 [ 摘 要 ] 为 了 解 决 计 算 ...
- 图解基于 Node.js 实现前后端分离 - CSDN博客
因为会上出了个意外,ppt图片全部丢失,只好对着白板跟大家交流了半个多小时.由于我做演讲不喜欢写太多的文字,没有图片的情况下讲漏了一些内容.这篇文章是我在会上分享内容对照ppt进行地整理. 基本介绍 ...
- 【转】Vue.js入门教程(一)从静态页面到前后端分离开发
第一章:基础知识 我能看懂吗? 只要你现在能用html+css+js制作一个静态页面,相信我,你100%可以读懂这篇文章. 本文尤其适合那些想要了解前后端分离开发技术,或者刚刚脱离传统MVC开发模式的 ...
- 图解基于node.js实现前后端分离
因为会上出了个意外,ppt图片全部丢失,只好对着白板跟大家交流了半个多小时.由于我做演讲不喜欢写太多的文字,没有图片的情况下讲漏了一些内容.这篇文章是我在会上分享内容对照ppt进行地整理. 基本介绍 ...
- 【Java从0到架构师】项目实战 - 前后端分离、后端校验、Swagger、全局异常处理
项目实战 - 前后端分离.后端校验.Swagger Layui 同源策略 SpringMVC 实现 CORS 后端校验 - hibernate-validator 方法的 Model 参数校验 方法的 ...
最新文章
- JavaEE(14) - JPA关联映射
- c语言测试代码怎么写,初学C语言,写了一个测试手速的工具,代码发上来,存着。。...
- ecshop“发货查询”中加入收货人、收货地址、发货时间、配送方式
- 如何通过 C# 自动捕获一个文件的变更?
- 深入学习二叉树(四) 二叉排序树
- 高效管理http连接
- fiddler拦截response
- UVA11752 The Super Powers【超级幂+暴力+数论】
- 线性表java实现之顺序存储源码
- c语言文件操作rewand,C语言程序设计第章概述.ppt
- 如何卸载服务(Service)?
- 软件设计模式(持续更新)
- 树的计数 Prufer序列+Cayley公式
- 电池SOC仿真系列-基于GA-BP神经网络的电池SOC估算方法
- 解决 手机能连接上wifi而电脑却却不能连接上wifi的情况
- 微信小游戏Laya引擎声音Bug的解决方案
- 分享一个matplotlib柱状图
- 对TCL/TK的一个小总结
- C语言——蔡勒(Zeller)公式:快速将任意日期转换为星期数
- 7-298 sdut-C语言实验-众数7-299 sdut-C语言实验-求一个3*3矩阵对角线元素之和
热门文章
- 起底方舟编译器的引用计数!
- 升级核心产品,不和用户竞争,UCloud进军产业互联网差异化路线
- 微软在一步一步把 Windows 10 玩出碎片化?
- 熊市投身区块链,程序员的出路在哪里?
- 高通誓要“逼杀”苹果!
- 金立手机用60亿“砸死”了自己
- 不到 1000 元,你的所有隐私竟然都能随便查!!!
- 远程服务器 上传公钥,SSH远程连接报错Permission denied (publickey)
- mysql对所有id求积_sql 行列式 转换,
- app营销实训报告_第七届全国金融与证券投资模拟实训大赛圆满落幕!