当前位置: 首页 > wzjs >正文

仿制手机网站教程易观数据

仿制手机网站教程,易观数据,重庆网上办事大厅,重庆彭水网站建设用户注册,登录 播放地址 课设作业图书管理系统_哔哩哔哩_bilibili 对图书进行增删改查 package com.xwr.controller; import com.xwr.entity.Book; import com.xwr.entity.Category; import com.xwr.service.BookService; import com.xwr.service.CategoryServ…

用户注册,登录 

播放地址 课设作业图书管理系统_哔哩哔哩_bilibili

对图书进行增删改查

package com.xwr.controller;

import com.xwr.entity.Book;
import com.xwr.entity.Category;
import com.xwr.service.BookService;
import com.xwr.service.CategoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.List;
import java.util.Map;

/**
* 图书控制器
*/
@Controller
@RequestMapping("/book")
public class BookController {

private final BookService bookService;
private final CategoryService categoryService;

@Autowired
public BookController(BookService bookService, CategoryService categoryService) {
this.bookService = bookService;
this.categoryService = categoryService;
}

/**
* 显示图书列表页面
*/
@GetMapping("/list")
public String list(
@RequestParam(value = "pageNum", defaultValue = "1") int pageNum,
@RequestParam(value = "pageSize", defaultValue = "5") int pageSize,
@RequestParam(value = "title", required = false) String title,
@RequestParam(value = "author", required = false) String author,
@RequestParam(value = "categoryId", required = false) Integer categoryId,
Model model,
HttpSession session) {

// 检查用户是否已登录
if (session.getAttribute("loginUser") == null) {
// 未登录,重定向到登录页面
return "redirect:/user/login";
}

// 查询分类列表
List<Category> categories = categoryService.findAll();
model.addAttribute("categories", categories);

// 查询条件不为空时,执行条件查询
if (title != null && !title.isEmpty() || author != null && !author.isEmpty() || categoryId != null) {
List<Book> books = bookService.findByCondition(title, author, categoryId);
model.addAttribute("books", books);
model.addAttribute("title", title);
model.addAttribute("author", author);
model.addAttribute("categoryId", categoryId);
} else {
// 执行分页查询
Map<String, Object> pageInfo = bookService.findByPage(pageNum, pageSize);
model.addAttribute("pageInfo", pageInfo);
}

return "book/list";
}

/**
* 显示图书详情页面
*/
@GetMapping("/detail/{id}")
public String detail(@PathVariable("id") Integer id, Model model) {
Book book = bookService.findById(id);
model.addAttribute("book", book);
return "book/detail";
}

/**
* 显示添加图书页面
*/
@GetMapping("/add")
public String showAddForm(Model model) {
List<Category> categories = categoryService.findAll();
model.addAttribute("categories", categories);
return "book/add";
}

/**
* 显示编辑图书页面
*/
@GetMapping("/edit/{id}")
public String showEditForm(@PathVariable("id") Integer id, Model model) {
Book book = bookService.findById(id);
List<Category> categories = categoryService.findAll();
model.addAttribute("book", book);
model.addAttribute("categories", categories);
return "book/edit";
}

/**
* 删除图书
*/
@GetMapping("/delete/{id}")
public String delete(@PathVariable("id") Integer id) {
bookService.delete(id);
return "redirect:/book/list";
}
}

package com.xwr.controller;

import com.xwr.entity.Book;
import com.xwr.service.BookService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.propertyeditors.CustomDateEditor;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;

/**
* 图书REST控制器
* 用于处理AJAX请求和文件上传
*/
@RestController
@RequestMapping("/api/book")
public class BookRestController {

private final BookService bookService;

@Autowired
public BookRestController(BookService bookService) {
this.bookService = bookService;
}

/**
* 注册自定义日期编辑器
*/
@InitBinder
public void initBinder(WebDataBinder binder) {
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
dateFormat.setLenient(false);
binder.registerCustomEditor(Date.class, new CustomDateEditor(dateFormat, true));
}

/**
* 添加图书
*/
@PostMapping("/add")
public Map<String, Object> add(Book book, @RequestParam(value = "coverImage", required = false) MultipartFile coverImage, HttpServletRequest request) {
Map<String, Object> result = new HashMap<>();

try {
// 处理上传图片
if (coverImage != null && !coverImage.isEmpty()) {
String uploadPath = request.getServletContext().getRealPath("/upload/");
File uploadDir = new File(uploadPath);
if (!uploadDir.exists()) {
uploadDir.mkdirs();
}

// 生成唯一文件名
String originalFilename = coverImage.getOriginalFilename();
String extension = originalFilename.substring(originalFilename.lastIndexOf("."));
String fileName = UUID.randomUUID().toString() + extension;

// 保存文件
File destFile = new File(uploadPath + fileName);
coverImage.transferTo(destFile);

// 设置图片路径
book.setImageUrl("/upload/" + fileName);
}

// 保存图书信息
boolean success = bookService.add(book);
result.put("success", success);
result.put("message", success ? "添加成功" : "添加失败");
result.put("book", book);
} catch (IOException e) {
result.put("success", false);
result.put("message", "上传图片失败: " + e.getMessage());
} catch (Exception e) {
result.put("success", false);
result.put("message", "添加失败: " + e.getMessage());
}

return result;
}

/**
* 更新图书
*/
@PostMapping("/update")
public Map<String, Object> update(Book book, @RequestParam(value = "coverImage", required = false) MultipartFile coverImage, HttpServletRequest request) {
Map<String, Object> result = new HashMap<>();

try {
// 如果有新上传的图片
if (coverImage != null && !coverImage.isEmpty()) {
String uploadPath = request.getServletContext().getRealPath("/upload/");
File uploadDir = new File(uploadPath);
if (!uploadDir.exists()) {
uploadDir.mkdirs();
}

// 生成唯一文件名
String originalFilename = coverImage.getOriginalFilename();
String extension = originalFilename.substring(originalFilename.lastIndexOf("."));
String fileName = UUID.randomUUID().toString() + extension;

// 保存文件
File destFile = new File(uploadPath + fileName);
coverImage.transferTo(destFile);

// 设置新的图片路径
book.setImageUrl("/upload/" + fileName);
}

// 更新图书信息
boolean success = bookService.update(book);
result.put("success", success);
result.put("message", success ? "更新成功" : "更新失败");
} catch (IOException e) {
result.put("success", false);
result.put("message", "上传图片失败: " + e.getMessage());
} catch (Exception e) {
result.put("success", false);
result.put("message", "更新失败: " + e.getMessage());
}

return result;
}

/**
* 删除图书
*/
@PostMapping("/delete/{id}")
public Map<String, Object> delete(@PathVariable("id") Integer id) {
Map<String, Object> result = new HashMap<>();

try {
// 删除图书
boolean success = bookService.delete(id);
result.put("success", success);
result.put("message", success ? "删除成功" : "删除失败");
} catch (Exception e) {
result.put("success", false);
result.put("message", "删除失败: " + e.getMessage());
}

return result;
}
}

http://www.dtcms.com/wzjs/357072.html

相关文章:

  • 成品app软件大全seo工作室
  • 上国外网站用什么机箱好湘潭网站制作
  • 花生壳域名注册官网seo职位描述
  • 深圳品牌网站设计电话域名注册查询工具
  • 哪些网站做简历合适韶山seo快速排名
  • 网站设计常识今日国际军事新闻头条
  • 烟台网站制作专业世界疫情最新数据
  • 苏州集团网站制作设计淘宝美工培训
  • 如何取一个大气的名字的做网站制作网站教学
  • 把网站内容全删掉 在重新建立会不会被k网络营销岗位有哪些
  • b2b2c多用户商城网站推广seo设置
  • java做网站6开发网站用什么软件
  • 号号网站开发如何进行网络营销
  • 好的手表网站seo顾问是什么
  • 电商网站 案例怎样制作网页设计
  • 做的好的地方网站棋牌软件制作开发多少钱
  • 西安医院网站建设营销型网站建设推荐
  • 永清建设局网站打开全网搜索
  • 网站规划与设计课程设计公司网站定制
  • 专业轻电商网站建设公司网站设计公司报价
  • 郴州高端网站建设昆明seo博客
  • 我为群众办实事项目清单情感网站seo
  • 最好的开发网站建设价格免费外网加速器
  • 南通网站建设找哪家好互动营销的概念
  • wordpress 大型网站seo的基础是什么
  • wordpress 默认登陆优化关键词排名
  • 网站怎么挂服务器企业营销策划实训报告
  • 浙江省网站建设公司排名关键字
  • seo关键词选择及优化深圳seo排名哪家好
  • 广州信科做网站b2b平台是什么意思