解释如下代码:@GetMapping("/index") public String getPost(@RequestParam(value = "status",required = false)Integer status, @RequestParam(value = "flag",required = false) Integer flag, @RequestParam(value = "type",required = false) String typeNmae){ if (status==null) { if (typeNmae==null) { return "forward:/index/2/2/false"; }else { return "forward:/index/2/2/"+typeNmae; } } return "forward:/index/"+status+"/"+flag+"/false"; }
时间: 2023-06-05 11:07:18 浏览: 83
这是一段使用 Spring MVC 框架的 Java 代码。它定义了一个使用 @GetMapping 注解的方法,该方法的路径为 "/index"。在方法中,使用了三个请求参数:status、flag 和 typeNmae。其中,status 和 flag 的类型都是 Integer,使用了 @RequestParam 注解将它们作为参数传递进来,required=false 表示它们是可选参数。typeNmae 的类型是 String,并且也是一个可选参数。
接下来对代码逻辑进行解释:首先判断 status 是否为空,如果为空则进一步判断 typeNmae 是否为空。如果 typeNmae 为空,则返回一个请求转发到路径为 "/index/2/2/false" 的页面。如果 typeNmae 不为空,则返回一个请求转发到路径为 "/index/2/2/" + typeNmae 的页面。如果 status 不为空,则返回一个请求转发到路径为 "/index/" + status + "/" + flag + "/false" 的页面。
总的来说,这段代码的功能是根据传递进来的参数,返回不同的页面路径,用于在 Spring MVC 中进行页面跳转。
相关问题
SSM框架分页展示Hdfs文件列表上传文件,删除文件,下载文件前后端代码
### 回答1:
首先需要明确的是,HDFS是一个分布式文件系统,与传统的文件系统不同,因此需要使用特定的API进行操作。在Java中,可以使用Hadoop提供的API进行操作。
一、分页展示HDFS文件列表
前端代码:
```html
<!-- 文件列表展示 -->
<table class="table table-bordered table-hover">
<thead>
<tr>
<th>文件名</th>
<th>文件大小</th>
<th>修改时间</th>
</tr>
</thead>
<tbody>
<tr th:each="file : ${fileList}">
<td th:text="${file.name}"></td>
<td th:text="${file.size}"></td>
<td th:text="${#dates.format(file.modificationTime, 'yyyy-MM-dd HH:mm:ss')}"></td>
</tr>
</tbody>
</table>
<!-- 分页 -->
<nav aria-label="Page navigation">
<ul class="pagination">
<li th:class="${pageInfo.hasPreviousPage} ? '':'disabled'">
<a th:href="@{/file/list(pageNum=1)}" aria-label="Previous">
<span aria-hidden="true">«</span>
</a>
</li>
<li th:each="pageNum : ${pageInfo.navigatepageNums}">
<a th:href="@{/file/list(pageNum=${pageNum})}" th:text="${pageNum}" th:class="${pageNum == pageInfo.pageNum} ? 'active':''"></a>
</li>
<li th:class="${pageInfo.hasNextPage} ? '':'disabled'">
<a th:href="@{/file/list(pageNum=${pageInfo.pages})}" aria-label="Next">
<span aria-hidden="true">»</span>
</a>
</li>
</ul>
</nav>
```
后端代码:
```java
@RequestMapping("/file/list")
public String listFiles(Model model,
@RequestParam(defaultValue = "1") Integer pageNum,
@RequestParam(defaultValue = "10") Integer pageSize) {
try {
// 获取Hadoop配置信息
Configuration conf = new Configuration();
conf.set("fs.defaultFS", "hdfs://localhost:9000");
conf.set("fs.hdfs.impl", "org.apache.hadoop.hdfs.DistributedFileSystem");
// 获取HDFS文件列表
FileSystem fs = FileSystem.get(conf);
Path path = new Path("/");
FileStatus[] fileStatuses = fs.listStatus(path);
// 文件列表分页展示
PageHelper.startPage(pageNum, pageSize);
List<HdfsFile> fileList = Arrays.stream(fileStatuses)
.map(HdfsFile::new)
.collect(Collectors.toList());
PageInfo<HdfsFile> pageInfo = new PageInfo<>(fileList);
model.addAttribute("fileList", fileList);
model.addAttribute("pageInfo", pageInfo);
} catch (IOException e) {
e.printStackTrace();
}
return "file/list";
}
```
二、上传文件
前端代码:
```html
<form method="post" enctype="multipart/form-data" th:action="@{/file/upload}" th:object="${file}">
<div class="form-group">
<label for="name">文件名:</label>
<input type="text" class="form-control" id="name" th:field="*{name}" placeholder="请输入文件名" required>
</div>
<div class="form-group">
<label for="file">选择文件:</label>
<input type="file" id="file" name="file" required>
</div>
<button type="submit" class="btn btn-success">上传</button>
</form>
```
后端代码:
```java
@PostMapping("/file/upload")
public String uploadFile(@RequestParam("file") MultipartFile file,
@RequestParam("name") String name) {
try {
// 获取Hadoop配置信息
Configuration conf = new Configuration();
conf.set("fs.defaultFS", "hdfs://localhost:9000");
conf.set("fs.hdfs.impl", "org.apache.hadoop.hdfs.DistributedFileSystem");
// 上传文件到HDFS
FileSystem fs = FileSystem.get(conf);
Path dstPath = new Path("/" + name);
InputStream inputStream = file.getInputStream();
OutputStream outputStream = fs.create(dstPath);
IOUtils.copyBytes(inputStream, outputStream, 4096, true);
} catch (IOException e) {
e.printStackTrace();
}
return "redirect:/file/list";
}
```
三、删除文件
前端代码:
```html
<a th:href="@{/file/delete(name=${file.name})}" class="btn btn-danger btn-sm" onclick="return confirm('确定要删除该文件吗?')">删除</a>
```
后端代码:
```java
@GetMapping("/file/delete")
public String deleteFile(@RequestParam("name") String name) {
try {
// 获取Hadoop配置信息
Configuration conf = new Configuration();
conf.set("fs.defaultFS", "hdfs://localhost:9000");
conf.set("fs.hdfs.impl", "org.apache.hadoop.hdfs.DistributedFileSystem");
// 删除文件
FileSystem fs = FileSystem.get(conf);
Path path = new Path("/" + name);
fs.delete(path, false);
} catch (IOException e) {
e.printStackTrace();
}
return "redirect:/file/list";
}
```
四、下载文件
前端代码:
```html
<a th:href="@{/file/download(name=${file.name})}" class="btn btn-primary btn-sm">下载</a>
```
后端代码:
```java
@GetMapping("/file/download")
public ResponseEntity<InputStreamResource> downloadFile(@RequestParam("name") String name) {
try {
// 获取Hadoop配置信息
Configuration conf = new Configuration();
conf.set("fs.defaultFS", "hdfs://localhost:9000");
conf.set("fs.hdfs.impl", "org.apache.hadoop.hdfs.DistributedFileSystem");
// 下载文件
FileSystem fs = FileSystem.get(conf);
Path path = new Path("/" + name);
InputStream inputStream = fs.open(path);
HttpHeaders headers = new HttpHeaders();
headers.add(HttpHeaders.CONTENT_DISPOSITION, "attachment; filename=" + name);
return ResponseEntity.ok().headers(headers).body(new InputStreamResource(inputStream));
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
```
以上是SSM框架分页展示HDFS文件列表上传文件,删除文件,下载文件前后端代码的实现。
### 回答2:
SSM框架是指使用Spring + SpringMVC + MyBatis的开发环境,下面将介绍如何利用SSM框架来实现HDFS文件列表的分页展示、上传文件、删除文件和下载文件的前后端代码。
首先,需要在后端编写对应的Controller来处理前端的请求。假设后端Controller的路径为"/hdfs",那么可以按照以下方式定义相关方法:
1. 分页展示HDFS文件列表:
```
@RequestMapping(value = "/filelist", method = RequestMethod.GET)
@ResponseBody
public List<String> getFileList(@RequestParam("pageNum") int pageNum, @RequestParam("pageSize") int pageSize) {
// 在这里使用HDFS API获取指定路径下的文件列表
// 根据pageNum和pageSize进行分页处理,并返回一页的文件列表
}
```
其中,`pageNum`表示页码,`pageSize`表示每页的文件数量。通过HDFS API获取文件列表后,根据pageNum和pageSize来进行分页操作,最后返回一页的文件列表。
2. 上传文件:
```
@RequestMapping(value = "/upload", method = RequestMethod.POST)
public void uploadFile(@RequestParam("file") MultipartFile file) {
// 在这里使用HDFS API将文件上传到指定路径
}
```
通过`@RequestParam("file") MultipartFile file`来接收前端传递的文件,并使用HDFS API将文件上传到指定的路径。
3. 删除文件:
```
@RequestMapping(value = "/delete", method = RequestMethod.DELETE)
public void deleteFile(@RequestParam("fileName") String fileName) {
// 在这里使用HDFS API删除指定路径下的文件
}
```
通过`@RequestParam("fileName") String fileName`来接收需要删除的文件名,然后使用HDFS API来删除指定路径下的文件。
4. 下载文件:
```
@RequestMapping(value = "/download", method = RequestMethod.GET)
public ResponseEntity<Resource> downloadFile(@RequestParam("fileName") String fileName) {
// 在这里使用HDFS API下载指定路径下的文件,并返回ResponseEntity<Resource>
}
```
通过`@RequestParam("fileName") String fileName`来接收需要下载的文件名,然后使用HDFS API下载指定路径下的文件,并将文件内容封装到ResponseEntity<Resource>中返回。
上述代码中的HDFS API的使用可以参考Hadoop官方文档或者第三方Hadoop相关的库,具体使用方式可以根据实际需求进行调整。
在前端方面,可以使用Ajax进行异步请求,通过jQuery等工具发送请求到后端Controller的对应路径,处理相应的操作。
以上介绍了利用SSM框架实现HDFS文件列表的分页展示、上传文件、删除文件和下载文件的前后端代码,希望对你有所帮助。
### 回答3:
以下是一个使用SSM框架实现分页展示HDFS文件列表、上传文件、删除文件和下载文件的前后端代码示例:
后端代码(Java):
@Controller
@RequestMapping("/hdfs")
public class HdfsController {
@Autowired
private HdfsService hdfsService;
@RequestMapping("/list")
@ResponseBody
public List<HdfsFile> getFileList(@RequestParam(defaultValue = "1") int pageNum, @RequestParam(defaultValue = "10") int pageSize) {
return hdfsService.getFileList(pageNum, pageSize);
}
@RequestMapping("/upload")
@ResponseBody
public String uploadFile(MultipartFile file) {
if (file != null) {
return hdfsService.uploadFile(file);
} else {
return "上传文件为空";
}
}
@RequestMapping("/delete")
@ResponseBody
public String deleteFile(@RequestParam String fileName) {
return hdfsService.deleteFile(fileName);
}
@RequestMapping("/download")
public void downloadFile(@RequestParam String fileName, HttpServletResponse response) {
hdfsService.downloadFile(fileName, response);
}
}
@Service
public class HdfsServiceImpl implements HdfsService {
@Autowired
private FileSystem fileSystem;
@Override
public List<HdfsFile> getFileList(int pageNum, int pageSize) {
// 实现文件列表的分页查询逻辑
// ...
return fileList;
}
@Override
public String uploadFile(MultipartFile file) {
// 实现文件上传逻辑
// ...
return "文件上传成功";
}
@Override
public String deleteFile(String fileName) {
// 实现文件删除逻辑
// ...
return "文件删除成功";
}
@Override
public void downloadFile(String fileName, HttpServletResponse response) {
// 实现文件下载逻辑
// ...
}
}
前端代码(HTML + JavaScript):
<html>
<head>
<script src="https://cdn.bootcss.com/jquery/3.4.1/jquery.min.js"></script>
</head>
<body>
<h1>HDFS文件列表</h1>
<table id="fileListTable">
<thead>
<th>文件名</th>
<th>操作</th>
</thead>
<tbody>
</tbody>
</table>
<input type="file" id="uploadFile" />
<button onclick="upload()">上传文件</button>
<input type="text" id="deleteFileName" placeholder="请输入要删除的文件名" />
<button onclick="deleteFile()">删除文件</button>
<input type="text" id="downloadFileName" placeholder="请输入要下载的文件名" />
<button onclick="downloadFile()">下载文件</button>
<script>
function refreshFileList(pageNum, pageSize) {
$.ajax({
type: "GET",
url: "/hdfs/list?pageNum=" + pageNum + "&pageSize=" + pageSize,
success: function(data) {
var fileListTable = $("#fileListTable tbody");
fileListTable.empty();
$.each(data, function(index, file) {
fileListTable.append("<tr><td>" + file.fileName + "</td><td><a href='/hdfs/download?fileName=" + file.fileName + "'>下载</a></td></tr>");
});
}
});
}
function upload() {
var formData = new FormData();
var file = $("#uploadFile")[0].files[0];
formData.append("file", file);
$.ajax({
type: "POST",
url: "/hdfs/upload",
data: formData,
processData: false,
contentType: false,
success: function(data) {
alert(data);
refreshFileList(1, 10);
}
});
}
function deleteFile() {
var fileName = $("#deleteFileName").val();
$.ajax({
type: "POST",
url: "/hdfs/delete?fileName=" + fileName,
success: function(data) {
alert(data);
refreshFileList(1, 10);
}
});
}
function downloadFile() {
var fileName = $("#downloadFileName").val();
window.location.href = "/hdfs/download?fileName=" + fileName;
}
$(document).ready(function() {
refreshFileList(1, 10);
});
</script>
</body>
</html>
以上代码是一个简单的示例,实际应用中可能需要根据具体需求进行适当修改和调整。
使用springboot+mybatis-plus+vue+element-ui技术编写高校教师评价管理系统的前后端分离代码
很高兴能为您提供这方面的帮助,以下是基本的代码结构和技术选型:
## 后端代码结构
```
├── src
│ ├── main
│ │ ├── java
│ │ │ ├── com
│ │ │ │ ├── example
│ │ │ │ │ ├── config
│ │ │ │ │ │ ├── MybatisPlusConfig.java
│ │ │ │ │ │ ├── WebMvcConfig.java
│ │ │ │ │ ├── controller
│ │ │ │ │ │ ├── TeacherController.java
│ │ │ │ │ ├── dao
│ │ │ │ │ │ ├── TeacherDao.java
│ │ │ │ │ ├── entity
│ │ │ │ │ │ ├── Teacher.java
│ │ │ │ │ ├── service
│ │ │ │ │ │ ├── TeacherService.java
│ │ │ │ │ │ ├── impl
│ │ │ │ │ │ │ ├── TeacherServiceImpl.java
│ │ │ │ │ ├── Application.java
│ │ │ │ ├── common
│ │ │ │ │ ├── Result.java
│ │ │ │ ├── interceptor
│ │ │ │ │ ├── LoginInterceptor.java
│ │ │ │ ├── mapper
│ │ │ │ │ ├── TeacherMapper.java
│ │ │ │ ├── utils
│ │ │ │ │ ├── JwtUtils.java
│ │ ├── resources
│ │ │ ├── application.yml
│ │ │ ├── mapper
│ │ │ │ ├── TeacherMapper.xml
│ │ ├── static
│ │ ├── templates
```
## 技术选型
- 后端技术栈:Spring Boot、Mybatis-Plus、JWT、MySQL
- 前端技术栈:Vue、Element UI、Axios
## 后端代码实现
### 配置文件
在 `application.yml` 中配置数据库和 JWT 相关信息。
```yaml
server:
port: 8080
spring:
datasource:
url: jdbc:mysql://localhost:3306/teacher_evaluation?useUnicode=true&characterEncoding=utf-8&useSSL=false&serverTimezone=Asia/Shanghai
username: root
password: 123456
driver-class-name: com.mysql.cj.jdbc.Driver
mybatis-plus:
mapper-locations: classpath:mapper/*.xml
type-aliases-package: com.example.entity
configuration:
map-underscore-to-camel-case: true
jwt:
secret: secret
expire: 3600
```
### 实体类
```java
@Data
public class Teacher {
private Integer id;
private String name;
private Integer age;
private String sex;
private String phone;
private String email;
private String address;
private Integer departmentId;
private Integer status;
}
```
### Mapper 接口
```java
public interface TeacherMapper extends BaseMapper<Teacher> {
}
```
### DAO 层
```java
public interface TeacherDao {
Teacher selectById(Integer id);
List<Teacher> selectList();
int insert(Teacher teacher);
int update(Teacher teacher);
int delete(Integer id);
}
```
### Service 层
```java
public interface TeacherService {
Teacher selectById(Integer id);
List<Teacher> selectList();
boolean save(Teacher teacher);
boolean update(Teacher teacher);
boolean delete(Integer id);
}
```
### Service 实现类
```java
@Service
public class TeacherServiceImpl implements TeacherService {
@Autowired
private TeacherDao teacherDao;
@Override
public Teacher selectById(Integer id) {
return teacherDao.selectById(id);
}
@Override
public List<Teacher> selectList() {
return teacherDao.selectList();
}
@Override
public boolean save(Teacher teacher) {
return teacherDao.insert(teacher) > 0;
}
@Override
public boolean update(Teacher teacher) {
return teacherDao.update(teacher) > 0;
}
@Override
public boolean delete(Integer id) {
return teacherDao.delete(id) > 0;
}
}
```
### Controller 层
```java
@RestController
@RequestMapping("/teacher")
public class TeacherController {
@Autowired
private TeacherService teacherService;
@GetMapping("/list")
public Result list() {
List<Teacher> list = teacherService.selectList();
return Result.success(list);
}
@PostMapping("/save")
public Result save(@RequestBody Teacher teacher) {
boolean result = teacherService.save(teacher);
return result ? Result.success() : Result.fail("添加失败");
}
@PostMapping("/update")
public Result update(@RequestBody Teacher teacher) {
boolean result = teacherService.update(teacher);
return result ? Result.success() : Result.fail("更新失败");
}
@PostMapping("/delete")
public Result delete(@RequestParam Integer id) {
boolean result = teacherService.delete(id);
return result ? Result.success() : Result.fail("删除失败");
}
}
```
### JWT 鉴权
```java
@Component
public class LoginInterceptor implements HandlerInterceptor {
@Autowired
private JwtUtils jwtUtils;
@Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
String token = request.getHeader("Authorization");
if (token == null || !token.startsWith("Bearer ")) {
throw new RuntimeException("无效的token");
}
token = token.replace("Bearer ", "");
if (!jwtUtils.validateToken(token)) {
throw new RuntimeException("无效的token");
}
return true;
}
}
```
## 前端代码实现
### Axios 请求封装
```js
import axios from 'axios'
import { Message } from 'element-ui'
let instance = axios.create({
baseURL: '/api',
timeout: 5000,
headers: {
'Content-Type': 'application/json;charset=UTF-8'
}
})
instance.interceptors.request.use(config => {
let token = localStorage.getItem('token')
if (token) {
config.headers.Authorization = `Bearer ${token}`
}
return config
}, error => {
return Promise.reject(error)
})
instance.interceptors.response.use(response => {
if (response.status === 200) {
return response.data
} else {
Message.error('请求错误')
}
}, error => {
if (error.response.status === 401) {
Message.error('登录过期')
localStorage.removeItem('token')
window.location.href = '/login'
} else if (error.response.status === 403) {
Message.error('权限不足')
} else if (error.response.status === 500) {
Message.error('服务器错误')
} else {
Message.error('请求错误')
}
return Promise.reject(error)
})
export default instance
```
### Vue 页面
```vue
<template>
<div>
<el-row>
<el-col :span="24"><h2>教师列表</h2></el-col>
</el-row>
<el-row>
<el-col :span="24"><el-button type="primary" @click="add">添加教师</el-button></el-col>
</el-row>
<el-row>
<el-col :span="24">
<el-table :data="tableData" style="width: 100%">
<el-table-column prop="id" label="ID"></el-table-column>
<el-table-column prop="name" label="姓名"></el-table-column>
<el-table-column prop="age" label="年龄"></el-table-column>
<el-table-column prop="sex" label="性别"></el-table-column>
<el-table-column prop="phone" label="电话"></el-table-column>
<el-table-column prop="email" label="邮箱"></el-table-column>
<el-table-column prop="address" label="地址"></el-table-column>
<el-table-column prop="departmentId" label="部门"></el-table-column>
<el-table-column prop="status" label="状态"></el-table-column>
<el-table-column label="操作">
<template slot-scope="scope">
<el-button type="primary" size="mini" @click="edit(scope.row)">编辑</el-button>
<el-button type="danger" size="mini" @click="remove(scope.row)">删除</el-button>
</template>
</el-table-column>
</el-table>
</el-col>
</el-row>
<el-dialog title="添加教师" :visible.sync="addDialogVisible">
<el-form :model="addForm" :rules="addRules" ref="addForm">
<el-form-item label="姓名" prop="name">
<el-input v-model="addForm.name"></el-input>
</el-form-item>
<el-form-item label="年龄" prop="age">
<el-input v-model="addForm.age"></el-input>
</el-form-item>
<el-form-item label="性别" prop="sex">
<el-radio-group v-model="addForm.sex">
<el-radio label="男"></el-radio>
<el-radio label="女"></el-radio>
</el-radio-group>
</el-form-item>
<el-form-item label="电话" prop="phone">
<el-input v-model="addForm.phone"></el-input>
</el-form-item>
<el-form-item label="邮箱" prop="email">
<el-input v-model="addForm.email"></el-input>
</el-form-item>
<el-form-item label="地址" prop="address">
<el-input v-model="addForm.address"></el-input>
</el-form-item>
<el-form-item label="部门" prop="departmentId">
<el-select v-model="addForm.departmentId">
<el-option v-for="item in departmentList" :key="item.value" :label="item.label" :value="item.value"></el-option>
</el-select>
</el-form-item>
</el-form>
<div slot="footer">
<el-button @click="addDialogVisible = false">取消</el-button>
<el-button type="primary" @click="addFormSubmit">添加</el-button>
</div>
</el-dialog>
<el-dialog title="编辑教师" :visible.sync="editDialogVisible">
<el-form :model="editForm" :rules="editRules" ref="editForm">
<el-form-item label="ID" prop="id">
<el-input v-model="editForm.id" disabled></el-input>
</el-form-item>
<el-form-item label="姓名" prop="name">
<el-input v-model="editForm.name"></el-input>
</el-form-item>
<el-form-item label="年龄" prop="age">
<el-input v-model="editForm.age"></el-input>
</el-form-item>
<el-form-item label="性别" prop="sex">
<el-radio-group v-model="editForm.sex">
<el-radio label="男"></el-radio>
<el-radio label="女"></el-radio>
</el-radio-group>
</el-form-item>
<el-form-item label="电话" prop="phone">
<el-input v-model="editForm.phone"></el-input>
</el-form-item>
<el-form-item label="邮箱" prop="email">
<el-input v-model="editForm.email"></el-input>
</el-form-item>
<el-form-item label="地址" prop="address">
<el-input v-model="editForm.address"></el-input>
</el-form-item>
<el-form-item label="部门" prop="departmentId">
<el-select v-model="editForm.departmentId">
<el-option v-for="item in departmentList" :key="item.value" :label="item.label" :value="item.value"></el-option>
</el-select>
</el-form-item>
</el-form>
<div slot="footer">
<el-button @click="editDialogVisible = false">取消</el-button>
<el-button type="primary" @click="editFormSubmit">保存</el-button>
</div>
</el-dialog>
</div>
</template>
<script>
import api from '@/utils/api'
export default {
name: 'TeacherList',
data() {
return {
tableData: [],
addDialogVisible: false,
addForm: {
name: '',
age: '',
sex: '男',
phone: '',
email: '',
address: '',
departmentId: ''
},
addRules: {
name: [
{ required: true, message: '请输入姓名', trigger: 'blur' },
{ min: 2, max: 20, message: '长度在 2 到 20 个字符', trigger: 'blur' }
],
age: [
{ required: true, message: '请输入年龄', trigger: 'blur' },
{ type: 'number', message: '年龄必须为数字', trigger: 'blur' }
],
sex: [
{ required: true, message: '请选择性别', trigger: 'change' }
],
phone: [
{ required: true, message: '请输入电话', trigger: 'blur' },
{ pattern: /^1[3456789]\d{9}$/, message: '手机号码格式不正确', trigger: 'blur' }
],
email: [
{ required: true, message: '请输入邮箱', trigger: 'blur' },
{ type: 'email', message: '邮箱格式不正确', trigger: 'blur' }
],
address: [
{ required: true, message: '请输入地址', trigger: 'blur' }
],
departmentId: [
{ required: true, message: '请选择所属部门', trigger: 'change' }
]
},
editDialogVisible: false,
editForm: {
id: '',
name: '',
age: '',
sex: '男',
phone: '',
email: '',
address: '',
departmentId: ''
},
editRules: {
name: [
{ required: true, message: '请输入姓名', trigger: 'blur' },
{ min: 2, max: 20, message: '长度在 2 到 20 个字符', trigger: 'blur' }
],
age: [
{ required: true, message: '请输入年龄', trigger: 'blur' },
{ type: 'number', message: '年龄必须为数字', trigger: 'blur' }
],
sex: [
{ required: true, message: '请选择性别', trigger: 'change' }
],
phone: [
{ required: true, message: '请输入电话', trigger: 'blur' },
{ pattern: /^1[3456789]\d{9}$/, message: '手机号码格式不正确', trigger: 'blur' }
],
email: [
{ required: true, message: '请输入邮箱', trigger: 'blur' },
{ type: 'email', message: '邮箱格式不正确', trigger: 'blur' }
],
address: [
{ required: true, message: '请输入地址', trigger: 'blur' }
],
departmentId: [
{ required: true, message: '请选择所属部门', trigger: 'change' }
]
},
departmentList: [
{ value: 1, label: '计算机科学与技术' },
{ value: 2, label: '信息管理与信息系统' },
{ value: 3, label: '软件工程' }
]
}
},
created() {
this.getList()
},
methods: {
getList() {
api.get('/teacher/list').then(res => {
this.tableData = res.data
})
},
add() {
this.addDialogVisible = true
},
addFormSubmit() {
this.$refs.addForm.validate(valid => {
if (valid) {
api.post('/teacher/save', this.addForm).then(res => {
this.addDialogVisible = false
this.getList()
})
}
})
},
edit(row) {
this.editForm = Object.assign({}, row)
this.editDialogVisible = true
},
editFormSubmit() {
this.$refs.editForm.validate(valid => {
if (valid) {
api.post('/teacher/update', this.editForm).then(res => {
this.editDialogVisible = false
this.getList()
})
}
})
},
remove(row) {
this.$confirm('确定删除?', '提示', {
confirmButtonText: '确定',
cancelButtonText: '取消',
type: 'warning'
}).then(() => {
api.post('/teacher/delete', { id: row.id }).then(res => {
this.getList()
})
})
}
}
}
</script>
```
阅读全文