当前位置:网站首页>SSM整合-前后台协议联调(列表功能、添加功能、添加功能状态处理、修改功能、删除功能)
SSM整合-前后台协议联调(列表功能、添加功能、添加功能状态处理、修改功能、删除功能)
2022-07-03 18:45:00 【夏志121】
目录
一、列表功能
SpringMVC设定拦截了所有路径,它把页面的请求也拦截了,如果去访问页面,一定找不到,过不了SpringMVC,所以需要加一个新的配置类,进行资源的放行。
SpringMvcSupport配置类
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport;
@Configuration
public class SpringMvcSupport extends WebMvcConfigurationSupport {
@Override
protected void addResourceHandlers(ResourceHandlerRegistry registry) {
registry.addResourceHandler("/pages/**").addResourceLocations("/pages/");
registry.addResourceHandler("/css/**").addResourceLocations("/css/");
registry.addResourceHandler("/js/**").addResourceLocations("/js/");
registry.addResourceHandler("/plugins/**").addResourceLocations("/plugins/");
}
}写完配置之后,要让SpringMVC加载上,在@ComponentScan进行加载。
@Configuration
@ComponentScan({"com.itheima.controller","com.itheima.config"})
@EnableWebMvc
public class SpringMvcConfig {
}发送ajax请求,得到返回值,让当前的数据模型加载从后台传进来的数据
books.html
//列表
getAll() {
//发送ajax请求
axios.get("/books").then((res)=>{
this.dataList = res.data.data;
});
},页面列表展示效果

二、添加功能
弹出添加窗口功能
//弹出添加窗口
handleCreate() {
this.dialogFormVisible = true;
this.resetForm();
},添加功能
//添加
handleAdd () {
//发送ajax请求
axios.post("/books",this.formData).then((res)=>{
//如果操作成功,关闭弹层,显示数据
this.dialogFormVisible = false;
this.getAll();
});
},添加功能效果

添加成功后效果
三、添加功能状态处理
给添加功能状态处理,如有异常进行状态显示,无异常成功。
//添加
handleAdd () {
//发送ajax请求
axios.post("/books",this.formData).then((res)=>{
console.log(res.data);
//如果操作成功,关闭弹层,显示数据
if(res.data.code == 20011){
this.dialogFormVisible = false;
this.$message.success("添加成功");
}else if(res.data.code == 20010){
this.$message.error("添加失败");
}else{
this.$message.error(res.data.msg);
}
}).finally(()=>{
this.getAll();
});
},添加成功效果

在BookServiceImpl中添加受影响行数大于0,受影响行数大于0为真,等于0为假
public boolean save(Book book) {
return bookDao.save(book) > 0;
}
public boolean update(Book book) {
return bookDao.update(book) > 0;
}
public boolean delete(Integer id) {
return bookDao.delete(id) > 0;
}添加操作时输入大于数据表要求字段的最大长度20,既能得到失败效果


因为做了双向绑定,所以会进行回显,进行重置表单,在弹出添加功能进行调用。
//重置表单
resetForm() {
this.formData = {};
},四、修改功能
弹出编辑窗口,根据id查询,进行展示弹层,加载数据。
//弹出编辑窗口
handleUpdate(row) {
//查询数据,根据id查询
axios.get("/books/"+row.id).then((res)=>{
if(res.data.code == 20041){
//展示弹层,加载数据
this.formData = res.data.data;
this.dialogFormVisible4Edit = true;
}else{
this.$message.error(res.data.msg);
}
});
},编辑窗口效果

编辑功能与添加功能差不多,修改put请求及修改相应的状态码和对应的提示信息即可。
//编辑
handleEdit() {
//发送ajax请求
axios.put("/books",this.formData).then((res)=>{
//如果操作成功,关闭弹层,显示数据
if(res.data.code == 20031){
this.dialogFormVisible4Edit = false;
this.$message.success("修改成功");
}else if(res.data.code == 20030){
this.$message.error("修改失败");
}else{
this.$message.error(res.data.msg);
}
}).finally(()=>{
this.getAll();
});
},修改成功功能效果


修改失败功能效果


五、删除功能
弹出提示框,提示操作,根据id进行删除,还进行取消删除操作提示。
// 删除
handleDelete(row) {
//1.弹出提示框
this.$confirm("此操作永久删除当前数据,是否继续?","提示",{
type:'info'
}).then(()=>{
//2.做删除业务
axios.delete("/books/"+row.id).then((res)=>{
if(res.data.code == 20021){
this.$message.success("删除成功");
}else{
this.$message.error("删除失败");
}
}).finally(()=>{
this.getAll();
});
}).catch(()=>{
//3.取消删除
this.$message.info("取消删除操作");
});
}删除提示框效果

删除成功效果

删除取消效果

边栏推荐
- Compose LazyColumn 顶部添加控件
- 189. Rotation array
- Unity webgl optimization
- Pytorch introduction to deep learning practice notes 13- advanced chapter of cyclic neural network - Classification
- TypeScript 官网教程
- [combinatorics] generating function (positive integer splitting | repeated ordered splitting | non repeated ordered splitting | proof of the number of repeated ordered splitting schemes)
- Change is the eternal theme
- Processing of user input parameters in shell script
- Common PostgreSQL commands
- Xception for deeplab v3+ (including super detailed code comments and original drawing of the paper)
猜你喜欢

2022-2028 global physiotherapy clinic industry research and trend analysis report

22.2.14 -- station B login with code -for circular list form - 'no attribute' - 'needs to be in path selenium screenshot deviation -crop clipping error -bytesio(), etc

my. INI file not found

Raft log replication

There are several levels of personal income tax

Sensor debugging process

Zhengda futures news: soaring oil prices may continue to push up global inflation

Recommend a simple browser tab

What is SQL get connection

Read the paper glodyne global topology preserving dynamic network embedding
随机推荐
Implementation of cqrs architecture mode under Kratos microservice framework
“google is not defined” when using Google Maps V3 in Firefox remotely
Caddy server agent
After the festival, a large number of people change careers. Is it still time to be 30? Listen to the experience of the past people
cipher
041. (2.10) talk about manpower outsourcing
Processing of user input parameters in shell script
Sqlalchemy - subquery in a where clause - Sqlalchemy - subquery in a where clause
2022-2028 global petroleum pipe joint industry research and trend analysis report
[combinatorics] exponential generating function (example 2 of solving multiple set permutation with exponential generating function)
leetcode:11. 盛最多水的容器【雙指針 + 貪心 + 去除最短板】
2022-2028 global solid phase extraction column industry research and trend analysis report
In addition to the prickles that pierce your skin, there are poems and distant places that originally haunt you in plain life
Win 11 major updates, new features love love.
Multifunctional web file manager filestash
[combinatorics] exponential generating function (concept of exponential generating function | permutation number exponential generating function = combinatorial number ordinary generating function | e
SQL: special update operation
Software development freelancer's Road
How does GCN use large convolution instead of small convolution? (the explanation of the paper includes super detailed notes + Chinese English comparison + pictures)
204. Count prime
