基于javascript与css开发是Web富文本编辑器,轻量、简洁、易用、开源免费。
代码演示
我们在test.ftl中添加富文本编辑器演示下
Title
运行项目,在浏览器中访问http://localhost:8080/test/t1
首先,打开pom.xml文件,引入Spring MVC文件上传底层依赖和Json Html解析组件,并发布到lib包
commons-fileupload commons-fileupload 1.4 org.jsoup jsoup 1.12.1
打开BookService.java新增创建新的图书的方法,并在BookServiceImpl.java类中添加相应的方法实现。
/*** 创建新的图书* @param book 图书对象* @return 返回图书对象*/public Book createBook(Book book);
/*** 创建新的图书** @param book 图书对象* @return 返回图书对象*/@Transactionalpublic Book createBook(Book book) {bookMapper.insert(book);return book;}
然后在com.ql.reader.controller.management包下创建MBookController.java图书管理控制器类,编写跳转到图书管理页面、上传图片和新增图书的方法。
package com.ql.reader.controller.management;import com.ql.reader.entity.Book;
import com.ql.reader.service.BookService;
import com.ql.reader.service.exception.BussinessException;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.IOException;
import java.net.MalformedURLException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;@Controller
@RequestMapping("/management/book")
public class MBookController {@Resourceprivate BookService bookService;@GetMapping("/index.html")public ModelAndView showBook(){return new ModelAndView("/management/book");}/*** wangEditor文件上传* @param file 上传文件* @param request 原生请求对象* @return* @throws IOException*/@PostMapping("/upload")@ResponseBodypublic Map upload(@RequestParam("img") MultipartFile file, HttpServletRequest request) throws IOException {//得到上传目录String uploadPath = request.getServletContext().getResource("/").getPath() + "/upload/";//文件名String fileName = new SimpleDateFormat("yyyyMMddHHmmssSSS").format(new Date());//扩展名String suffix = file.getOriginalFilename().substring(file.getOriginalFilename().lastIndexOf("."));//保存文件到upload目录file.transferTo(new File(uploadPath+fileName+suffix));Map result = new HashMap();result.put("errno",0);result.put("data", new String[]{"/upload/"+fileName+suffix});return result;}@PostMapping("/create")@ResponseBodypublic Map createBook(Book book){Map result = new HashMap();try {book.setEvaluationQuantity(0);book.setEvaluationScore(0f);Document doc = Jsoup.parse(book.getDescription());//解析图书详情Element img = doc.select("img").first();//获取图书详情第一图的元素对象String cover = img.attr("src");book.setCover(cover);//来自于description描述的第一幅图bookService.createBook(book);result.put("code", "0");result.put("msg", "success");} catch (BussinessException e) {e.printStackTrace();result.put("code", e.getCode());result.put("msg", e.getMsg());}return result;}}
在src/main/webapp/WEB-INF/ftl/management目录下新增图书管理的前端页面book.ftl
图书管理功能
图书列表
运行项目,在浏览器中输入http://localhost:8080/management/book/index.html点击添加,打开新增图书弹框,输入信息提交测试。
提交成功后在浏览器中访问http://localhost:8080/在图书列表的最后面查到刚新增的图书。
打开MBookController.java编写分页查询方法
@GetMapping("/list")@ResponseBodypublic Map list(Integer page, Integer limit){if (page==null){page=1;}if (limit==null){limit=10;}IPage pageObject = bookService.paging(null, null, page, limit);Map result = new HashMap();result.put("code", "0");result.put("msg", "success");result.put("data", pageObject.getRecords());//当前页面数据result.put("count", pageObject.getTotal());//未分页时记录总数return result;}
运行项目,浏览器访问http://localhost:8080/management/book/index.html
打开BookService.java添加更新图书数据方法,并在BookServiceImpl.java添加方法实现
/*** 更新图书* @param book 新图书数据* @return 更新后的数据*/public Book updateBook(Book book);
/*** 更新图书** @param book 新图书数据* @return 更新后的数据*/@Transactionalpublic Book updateBook(Book book) {bookMapper.updateById(book);return book;}
然后打开MBookController.java添加根据Id获取图书和更新图书的方法
/*** 根据图书ID获取图书* @param bookId 图书ID* @return 图书信息*/@GetMapping("/id/{id}")@ResponseBodypublic Map selectById(@PathVariable("id") Long bookId){Book book = bookService.selectById(bookId);Map result = new HashMap();result.put("code", "0");result.put("msg","success");result.put("data", book);return result;}/*** 更新图书数据* @param book* @return*/@PostMapping("/update")@ResponseBodypublic Map updateBook(Book book) {Map result = new HashMap();try {Book rawBook = bookService.selectById(book.getBookId());rawBook.setBookName(book.getBookName());rawBook.setSubTitle(book.getSubTitle());rawBook.setAuthor(book.getAuthor());rawBook.setCategoryId(book.getCategoryId());rawBook.setDescription(book.getDescription());Document doc = Jsoup.parse(book.getDescription());String cover = doc.select("img").first().attr("src");rawBook.setCover(cover);bookService.updateBook(rawBook);result.put("code", "0");result.put("msg", "success");} catch (BussinessException e) {e.printStackTrace();result.put("code", e.getCode());result.put("msg", e.getMsg());}return result;}
运行项目,在浏览器访问http://localhost:8080/management/book/index.html并找到我们上次新增的测试图书信息,并点击修改为
提交数据后修改成功。
打开BookService.java编写删除图书的方法,并在BookServiceImpl.java添加方法实现
/*** 删除图书及相关数据* @param bookId*/public void deleteBook(Long bookId);
/*** 删除图书及相关数据** @param bookId 图书编号*/@Transactionalpublic void deleteBook(Long bookId) {bookMapper.deleteById(bookId);QueryWrapper mrsQueryWrapper = new QueryWrapper<>();mrsQueryWrapper.eq("book_id", bookId);memberReadStateMapper.delete(mrsQueryWrapper);QueryWrapper evaluationQueryWrapper = new QueryWrapper<>();evaluationQueryWrapper.eq("book_id", bookId);evaluationMapper.delete(evaluationQueryWrapper);}
然后打开MBookController.java编写删除图书的控制器方法
@GetMapping("/delete/{id}")@ResponseBodypublic Map deleteBook(@PathVariable("id") Long bookId){Map result = new HashMap();try {bookService.deleteBook(bookId);result.put("code", "0");result.put("msg", "success");} catch (BussinessException e) {e.printStackTrace();result.put("code", e.getCode());result.put("msg", e.getMsg());}return result;}
运行项目,浏览器访问http://localhost:8080/management/book/index.html并找到我们添加的测试数据删除。
删除成功后数据消失。