从无到有搭建SSM项目(前台)

一、准备工作

通过vue init webpack xxx命令搭建Vue项目

首先执行四个命令
npm install element-ui -S
npm install axios -S
npm install qs -S
npm install vue-axios -S

然后在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 ElementUI from 'element-ui' // 新添加 1
import 'element-ui/lib/theme-chalk/index.css' // 新添加 2
import App from './App'
import axios from '@/api/http'  //vue项目对axios的全局配置
import VueAxios from 'vue-axios'
import router from './router'

Vue.use(ElementUI);   // 新添加 3
Vue.use(VueAxios,axios);
Vue.config.productionTip = false

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

然后导入api文件夹与后台进行关联

目录结构
在这里插入图片描述

其中action.js就是放置后台写的接口

action.js

/**
 * 对后台请求的地址的封装,URL格式如下:
 * 模块名_实体名_操作
 */
export default {
	'SERVER': 'http://localhost:8080/', //服务器
	'SYSTEH_LIST': '/book/list', //查询
	'SYSTEH_DEL': '/book/del',	//删除
	'SYSTEH_ADD': '/book/add',	//增加
	'SYSTEH_EDIT': '/book/edit',	//修改
	'getFullPath': k => { //获得请求的完整地址,用于mockjs测试时使用
		return this.SERVER + this[k];
	}
}

http.js

/**
 * vue项目对axios的全局配置
 */
import axios from 'axios'
import qs from 'qs'
 
//引入action模块,并添加至axios的类属性urls上
import action from '@/api/action'
axios.urls = action
 
// axios默认配置
axios.defaults.timeout = 10000; // 超时时间
// axios.defaults.baseURL = 'http://localhost:8080/j2ee15'; // 默认地址
axios.defaults.baseURL = action.SERVER;
 
//整理数据
// 只适用于 POST,PUT,PATCH,transformRequest` 允许在向服务器发送前,修改请求数据
axios.defaults.transformRequest = function(data) {
	data = qs.stringify(data);
	return data;
};
 
 
 
export default axios;

二、组件开发

准备工作做完后,现在我们开始搭建组件Articles

在这里插入图片描述在这里插入图片描述

<template>
  <div>
    <el-form :inline="true" :model="formInline" class="user-search">
      <el-form-item label="搜索:">
        <el-input size="small" v-model="formInline.name" placeholder="输入书籍名"></el-input>
      </el-form-item>
      <el-form-item>
        <el-button size="small" type="primary" icon="el-icon-search" @click="search">搜索</el-button>
        <el-button size="small" type="primary" icon="el-icon-plus" @click="handleEdit()">添加</el-button>
      </el-form-item>
    </el-form>
    <!--列表-->
    <el-table size="small" :data="listData" element-loading-text="拼命加载中" style="width: 100%;">
      <el-table-column align="center" type="selection" min-width="1">
      </el-table-column>
      
			<el-table-column sortable prop="id" label="书籍ID" min-width="1">
      </el-table-column>
			
			<el-table-column sortable prop="name" label="书籍名" min-width="1">
			</el-table-column>
			
			<el-table-column sortable prop="pinyin" label="拼音" min-width="1">
			</el-table-column>
			
			<el-table-column sortable prop="cid" label="书籍类型" min-width="1">
			</el-table-column>
			
			<el-table-column sortable prop="author" label="作者" min-width="1">
			</el-table-column>
			
			<el-table-column sortable prop="price" label="价格" min-width="1">
			</el-table-column>
			
			
      <el-table-column sortable prop="image" label="图片路径" min-width="1">
      </el-table-column>
			
			<el-table-column sortable prop="publishing" label="出版社" min-width="1">
			</el-table-column>
			
			<el-table-column sortable prop="description" label="描述" min-width="1">
			</el-table-column>
			
			<el-table-column sortable prop="state" label="书籍状态" min-width="1">
			</el-table-column>
			
			<el-table-column sortable prop="deployTime" label="上架时间" min-width="1">
			</el-table-column>
			
			<el-table-column sortable prop="sales" label="销量" min-width="1">
			</el-table-column>
			
      <el-table-column align="center" label="操作" min-width="1">
        <template slot-scope="scope">
          <el-button size="mini" @click="doEdit(scope.$index, scope.row)">编辑</el-button>
          <el-button size="mini" type="danger" @click="deleteUser(scope.$index, scope.row)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-pagination style="margin-top: 20px;" @size-change="handleSizeChange" @current-change="handleCurrentChange"
      :current-page="formInline.page" :page-sizes="[10, 20, 30, 50]" :page-size="100"
      layout="total, sizes, prev, pager, next, jumper" :total="total">
    </el-pagination>
    <!-- 编辑界面 -->
    <el-dialog :title="title" :visible.sync="editFormVisible" width="30%" @before-close="closeDialog">
      <el-form label-width="120px" :model="editForm" :rules="rules" ref="editForm">
        <el-form-item label="书籍名" prop="name">
          <el-input size="small" v-model="editForm.name" auto-complete="off" placeholder="请输入书籍名"></el-input>
        </el-form-item>
        <el-form-item label="书籍描述" prop="description">
          <el-input size="small" v-model="editForm.description" auto-complete="off" placeholder="请输入书籍描述"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button size="small" @click="closeDialog">取消</el-button>
        <el-button size="small" type="primary" class="title" @click="submitForm('editForm')">保存</el-button>
      </div>
    </el-dialog>
  </div>
</template>



<script>
  export default {
    data() {
      return {
        listData: [],
        formInline: {
          page: 1,
          rows: 10,
          title: ''
        },
        total: 0,
        title: '',
        editFormVisible: false,
        editForm: {
          title: '',
          body: '',
          id: 0
        },
        rules: {
          name: [{
              required: true,
              message: '请输入书籍名',
              trigger: 'blur'
            },
            {
              min: 5,
              max: 10,
              message: '长度在 510 个字符',
              trigger: 'blur'
            }
          ],
          description: [{
            required: true,
            message: '请输入书籍描述',
            trigger: 'blur'
          }]
        }
      };
    },
    methods: {
      handleSizeChange(rows) {
        this.formInline.page = 1;
        this.formInline.rows = rows;
        this.search();
      },
      handleCurrentChange(page) {
        this.formInline.page = page;
        this.search();
      },
      doSearch(params) {
        let url = this.axios.urls.SYSTEH_LIST;
        // let url = 'http://localhost:8080/T216_SSH/vue/userAction_login.action';

        this.axios.post(url, params).then((response) => {
          console.log(response);
          this.listData = response.data.result;
          this.total = response.data.pageBean.total;
        }).catch(function(error) {
          console.log(error);
        });
      },
      search() {
        this.doSearch(this.formInline);
    },
    closeDialog() {
      this.clearData();
    },
    handleEdit() {
      this.editFormVisible = true;
      this.title = '新增窗体';
    },
    doEdit(index, row) {
      this.editForm.id = row.id;
      this.editForm.name = row.name;
      this.editForm.description = row.description;
      this.editFormVisible = true;
      this.title = '编辑窗体';
    },
    clearData() {
      this.title = '';
      this.editForm.id = 0;
      this.editForm.name = '';
      this.editForm.pinyin = '';
			this.editForm.cid = '';
			this.editForm.author = '';
			this.editForm.price = '';
			this.editForm.image = '';
			this.editForm.publishing = '';
			this.editForm.description = '';
			this.editForm.state = '';
			this.editForm.deployTime = '';
			this.editForm.sales = '';
      this.editFormVisible = false;
    },
    deleteUser(index, row) {
      this.$confirm('此操作将永久删除该文件,是否继续?','提示',{
        confirmButtonText:'确定',
        cancelButtonText:'取消',
        type:'warning'
      }).then(()=>{
		//删除
        let url = this.axios.urls.SYSTEH_DEL;
        this.axios.post(url, {id:row.id}).then((response) => {
          console.log(response);
          this.$message({
            message:response.data.msg,
            type:'success'
          });
          this.clearData();
          this.search();
        }).catch(function(error) {
          console.log(error);
        });
      }).catch(()=>{
        this.$message({
          type:'info',
          message:'已取消删除'
        });
      });
    },
    submitForm(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          let url;
          if (this.editForm.id == 0) {
            url = this.axios.urls.SYSTEH_ADD;
          } else {
            url = this.axios.urls.SYSTEH_EDIT;
          }

          this.axios.post(url, this.editForm).then((response) => {
            console.log(response);
            this.$message({
              message:response.data.msg,
              type:'success'
            });
            this.clearData();
            this.search();
          }).catch(function(error) {
            console.log(error);
          });
        } else {
          console.log('error submit!!');
          return false;
        }
      });
      }
    },
    created() {
      this.doSearch({});
    }
  }
</script>

<style>

</style>


然后我们在route下的index.js中进行挂载

index.js

import Vue from 'vue'
import Router from 'vue-router'
import HelloWorld from '@/components/HelloWorld'
import Articles from '@/components/Articles'

Vue.use(Router)

export default new Router({
  routes: [
    {
      path: '/',
      name: 'Articles',
      component: Articles
    }
  ]
})

三、测试

在这里插入图片描述
在这里插入图片描述在这里插入图片描述在这里插入图片描述在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

在这里插入图片描述

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值