Spring Boot+Vue前后端分离项目案例

Spring Boot+Vue前后端分离项目案例一、构建项目使用vue-cli创建项目:然后导入编辑器(我使用的是webstorm),先进行启动下,看能否访问到localhost:8080。能访问到表示使用vue-cli创建项目正常。二、进行前端代码编写记得添加修改config下 的index.js文件前端页面代码:Footer.vue<template> <…

大家好,又见面了,我是你们的朋友全栈君。

一、构建项目

使用vue-cli创建项目:

Spring Boot+Vue前后端分离项目案例

然后导入编辑器(我使用的是webstorm),先进行启动下,看能否访问到localhost:8080。

能访问到表示使用vue-cli创建项目正常。

Spring Boot+Vue前后端分离项目案例

 

二、进行前端代码编写

记得添加修改config下 的index.js文件

 

Spring Boot+Vue前后端分离项目案例

 

前端页面代码:

Footer.vue

<template>
  <div>
    页面尾部
  </div>
</template>

<script>
    export default {
        name: "footer"
    }
</script>

<style scoped>

</style>

Header.vue

<template>
  <div>
    页面头部
  </div>
</template>

<script>
    export default {
        name: "header"
    }
</script>

<style scoped>

</style>

Index.vue

<template>
  <div>
    <blog-header></blog-header>
    <hr/>
    <div>
      这是首页,嘻嘻嘻。
    </div>
    <hr/>
    <blog-footer></blog-footer>
  </div>
</template>

<script>
  import Header from '@/components/common/Header'
  import Footer from '@/components/common/Footer'
    export default {
        name: "index",
      // Header/Footer组件给申明到components里面然后在template里面使用
      components: { Header, Footer }
    }
</script>

<style scoped>

</style>

Login.vue 

<template>
  <div>
    <header></header>
    <hr/>
    <div>
      用户名:<input type="text" v-model="loginInfoVo.username" placeholder="请输入用户名" />
      <br/>
      密码:<input type="password" v-model="loginInfoVo.password" placeholder="请输入密码" />
      <br/>
      <button v-on:click="login">登录</button>
      <br/>
      登录验证情况:<textarea cols="30" rows="10" v-model="responseResult"></textarea>
    </div>
    <hr/>
    <footer></footer>
  </div>
</template>

<script>
  import Header from '@/components/common/Header'
  import Footer from '@/components/common/Footer'
    export default {
        name: "login",
  // Header、Footer组件给申明到components里面然后在template里面使用
  components: { Header, Footer },
  data () {
    return {
      loginInfoVo: { username: '', password: '' },
      responseResult: []
    }
   },
  methods: {
    login () {
      this.$axios
        .post('/login', {
          username: this.loginInfoVo.username,
          password: this.loginInfoVo.password
        })
        .then(successResponse => {
          this.responseResult = JSON.stringify(successResponse.data)
          if (successResponse.data.code === 200) {
            this.$router.replace({path: '/index'})
          }
        })
        .catch(failResponse => {})
    }
  }
  }
</script>

<style scoped>

</style>

index.js

import Vue from 'vue'
import Router from 'vue-router'
import Login from '@/components/manage/Login'
import Index from '@/components/home/Index'

Vue.use(Router)

export default new Router({
  routes: [
    {
      path: '/',
      redirect: '/login'
    },
    {
      path: '/index',
      name: 'Index',
      component: Index
    },
    {
      path: '/manage',
      redirect: '/login'
    },
    {
      path: '/login',
      name: 'Login',
      component: Login
    }
  ]
})

 main.js

// The Vue build version to load with the `import` command
// (runtime-only or standalone) has been set in webpack.base.conf with an alias.
import Vue from 'vue'
import App from './App'
import router from './router'

// 引用axios,并设置基础URL为后端服务api地址
var axios = require('axios')
axios.defaults.baseURL = 'http://localhost:8443/api'
// 将API方法绑定到全局
Vue.prototype.$axios = axios
Vue.config.productionTip = false

/* eslint-disable no-new */
new Vue({
  el: '#app',
  router,
  components: { App },
  template: '<App/>'
})

 

中途启动的项目可能会报错:

Spring Boot+Vue前后端分离项目案例

停掉项目,在项目中安装axios,再重新启动项目。

Spring Boot+Vue前后端分离项目案例

基本上就是这个页面:

Spring Boot+Vue前后端分离项目案例

三、后端代码编写

后端使用springboot。

由于我的开发环境用的是IntelliJ IDEA 2017.3.3,创建Spring Boot项目时参考另一篇博文“idea快速搭建springboot项目   ”http://www.cnblogs.com/pengyan-9826/p/8093099.html。 但这里要注意的是,原文人家用MyBatis了,咱这里测试前后端分离,先不用勾选MyBatis,要不还得配置数据库,否则项目启动会报错:
Spring Boot+Vue前后端分离项目案例

 创建相关类:

Spring Boot+Vue前后端分离项目案例

后端写好了,把前端打包生成的dist目录里的文件拷贝到后端项目的static目录下。运行一下,发现启动的是8080端口。想起来Vue那指定的是8443了,得修改一下项目中的application.properties文件:

Spring Boot+Vue前后端分离项目案例

把打包的vue项目放到resources文件夹下,css和js文件夹在static下,index.html在最外面的

Spring Boot+Vue前后端分离项目案例

四、运行

启动idea,输入localhost:8843即可进行跳转

Spring Boot+Vue前后端分离项目案例

登录成功进行跳转:

Spring Boot+Vue前后端分离项目案例

失败:

Spring Boot+Vue前后端分离项目案例

到此,springboot+vue前后端连接上,后面再连接上数据库,替换成数据库数据。

 

 

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请联系我们举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/147168.html原文链接:https://javaforall.net

(0)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • MySQL:MySQL 存储过程

    MySQL:MySQL 存储过程MySQL存储过程(了解)1什么是存储过程MySQL5.0版本开始支持存储过程存储过程(StoredProcedure)是一种在数据库中存储复杂程序,以便外部程序调用的一种数据库对象

    2022年7月3日
    21
  • siamfc-pytorch代码讲解(三):demo&track

    siamfc-pytorch代码讲解(三):demo&track我之前的两篇博客:siamfc-pytorch代码讲解(一):backbone&headsiamfc-pytorch代码讲解(二):train&siamfc代码来自:https://github.com/huanglianghua/siamfc-pytorch今天主要看一下demo的部分,也就是涉及到测试tracking的部分。直接上代码:一、demo.pyfro…

    2022年10月1日
    0
  • docker安装elasticsearch 7.6.2「建议收藏」

    docker安装elasticsearch 7.6.2「建议收藏」小伙伴们,你们好呀!我是老寇!安装elasticsearch的教程数不胜数,本文的安装方式是经过自己测试的,因此分享给有需要的小伙伴,一来是避免小伙伴少走弯路,二来方便后面知识的整合。本文是基于ES7.6.2的版本进行安装的,话不多说,我们开始吧。目录一、提前条件二、安装过程1.拉取镜像2.启动容器3.配置文件4.设置密码5.退出容器6.重启容器7.谷歌插件一、提前条件谷歌插件:elasticsearch-head服务器:centos7.5(

    2022年6月6日
    64
  • httpclient Accept-Encoding 乱码[通俗易懂]

    httpclient Accept-Encoding 乱码[通俗易懂]解决方法1HttpEntityhttpEntity=httpResponse.getEntity();2if(httpEntity!=null){3if(httpEntity.getContentEncoding()!=null){4if("g…

    2022年7月15日
    13
  • Java如何打印输出九九乘法表「建议收藏」

    Java如何打印输出九九乘法表「建议收藏」Java中如何打印输出九九乘法表——————————————————————————打印乘法表的方法1.使用双重for循环打印九九乘法表2.使用do{}while()实现打印九九乘法表双重for循环的使用打印结果如图示:Java程序源代码如下:publicclasstest99{ publicstaticvoidmain(String[]ar…

    2022年7月15日
    18
  • 杭州电 3711 Binary Number

    杭州电 3711 Binary Number

    2022年1月17日
    39

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号