目标
- 前台和路由配置
- 后台
前台
components/sys/constanttype/ConstantAdd.vue
<template>
<el-form :model="ruleForm" :rules="rules" ref="ruleForm" label-width="130px" class="demo-ruleForm">
<el-form-item label="常数类别编码" prop="constantTypeCode">
<el-input v-model="ruleForm.constantTypeCode"></el-input>
</el-form-item>
<el-form-item label="常数类别名称" prop="constantTypeName">
<el-input v-model="ruleForm.constantTypeName"></el-input>
</el-form-item>
<el-form-item>
<el-button type="primary" @click="submitForm('ruleForm')">立即创建</el-button>
<el-button @click="resetForm('ruleForm')">重置</el-button>
</el-form-item>
</el-form>
</template>
<script>
export default {
data() {
return {
ruleForm: {
constantTypeCode: '',
constantTypeName: ''
},
rules: {
constantTypeCode: [
{ required: true, message: '请输入常数类别编码', trigger: 'blur' },
{ min: 3, max: 64, message: '长度在 3 到 64 个字符', trigger: 'blur' }
],
constantTypeName: [
{ required: true, message: '请输入常数类别名称', trigger: 'blur' },
{ min: 3, max: 64, message: '长度在 3 到 64 个字符', trigger: 'blur' }
]
}
};
},
methods: {
submitForm(formName) {
this.$refs[formName].validate((valid) => {
if (valid) {
//如果验证通过,向后台请求添加
alert('submit!');
this.$axios.post('http://localhost:8082/sys/constanttype/add',this.ruleForm,{headers: {
'Content-Type': 'application/json;charset=UTF-8'
}})
.then(response=>{
let code=response.data.code;
if(code=='0'){
alert('添加成功')
//回到列表页
}else{
//失败,打印错误原因
alert(response.data.msg)
}
})
} else {
console.log('error submit!!');
return false;
}
});
},
resetForm(formName) {
this.$refs[formName].resetFields();
}
}
}
</script>
路由配置
{
path:'/constantAdd',
name:'ConstantAdd',
component:ConstantAdd
}
后台关键代码
- ConstantTypeController
package com.neuedu.demo.controller;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.neuedu.demo.domain.Constanttype;
import com.neuedu.demo.domain.ResponseBean;
import com.neuedu.demo.service.IConstantTypeService;
@RestController
@RequestMapping("/sys/constanttype")
public class ConstantTypeController {
@Autowired
IConstantTypeService constantTypeServiceImpl;
@RequestMapping("add")
public ResponseBean add(@RequestBody Constanttype type){
ResponseBean result=new ResponseBean();
Constanttype db_type=constantTypeServiceImpl.save(type);
if(db_type!=null){
result.setCode("0");
result.setMsg("操作成功");
}else{
result.setCode("-1");
result.setMsg("操作失败");
}
return result;
}
}
-
Constanttype实体
package com.neuedu.demo.domain;
import java.io.Serializable;
import javax.persistence.*;
/**
* The persistent class for the constanttype database table.
* 常数类别管理
*/
@Entity
@NamedQuery(name="Constanttype.findAll", query="SELECT c FROM Constanttype c")
public class Constanttype implements Serializable {
private static final long serialVersionUID = 1L;
@Column(name = "constantTypeCode", nullable = true, length = 64)
private String constantTypeCode;
@Column(name = "constantTypeName", nullable = true, length = 64)
private String constantTypeName;
private int delMark=1;
@GeneratedValue(strategy=GenerationType.IDENTITY)
@Id
private int id;
public Constanttype() {
}
public String getConstantTypeCode() {
return this.constantTypeCode;
}
public void setConstantTypeCode(String constantTypeCode) {
this.constantTypeCode = constantTypeCode;
}
public String getConstantTypeName() {
return this.constantTypeName;
}
public void setConstantTypeName(String constantTypeName) {
this.constantTypeName = constantTypeName;
}
public int getDelMark() {
return this.delMark;
}
public void setDelMark(int delMark) {
this.delMark = delMark;
}
public int getId() {
return this.id;
}
public void setId(int id) {
this.id = id;
}
}
网友评论