第12讲 使用ThymeLeaf模板实现数据添加与删除

接 第11讲 使用ThymeLeaf模板实现数据修改

1 修改控制器

修改ProductController.java,代码如下:

package com.zjipc.jpa.controller;

import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.zjipc.jpa.dao.ProductRepository;
import com.zjipc.jpa.model.Product;

@Controller
@RequestMapping("/product")
public class ProductController {
	private Logger log = LoggerFactory.getLogger(getClass());
	
	@Autowired
	private ProductRepository productRepository;
	
	/**
	 * 拦截 /product/list 的post请求,返回数据
	 * @RestController =@Controller + @ResponseBody
	 * @return
	 */
	@PostMapping("/list")
	@ResponseBody
	public List list(){
		List list = productRepository.findAll();
		return list;
	}
	
	/**
	 * 捕捉通过Get请求,得到页面内容
	 * @param id
	 * @param map
	 * @return
	 */
	@GetMapping("/detail/{id}")
	public String getDetail(@PathVariable long id, ModelMap map) {
		Product p = productRepository.getOne(id);
		map.addAttribute("id", id);
		map.addAttribute("name", p.getName());
		map.addAttribute("price", p.getPrice());
		map.addAttribute("category", p.getCategory());
		map.addAttribute("pnum", p.getPnum());
		map.addAttribute("imgurl", p.getImgurl());
		map.addAttribute("description", p.getDescription());
		return "/product/detail";
	}

	@GetMapping("/add")
	public String getAdd() {
		return "/product/add";
	}
	
	@PostMapping("/add")
	public String postAdd(Product p) {
		log.warn(p.toString());
		productRepository.save(p);
		return "redirect:../index.html";
	}
	
	@GetMapping("/modify/{id}")
	public String getModify(@PathVariable long id, ModelMap map) {
		Product p = productRepository.getOne(id);
		map.addAttribute("id", id);
		map.addAttribute("name", p.getName());
		map.addAttribute("price", p.getPrice());
		map.addAttribute("category", p.getCategory());
		map.addAttribute("pnum", p.getPnum());
		map.addAttribute("imgurl", p.getImgurl());
		map.addAttribute("description", p.getDescription());
		return "/product/modify";
	}
	
	@PostMapping("/modify/{id}")
	public String postModify(Product p) {
		log.warn(p.toString());
		productRepository.save(p);
		return "redirect:../../index.html";
	}
	
	@GetMapping("/remove/{id}")
	public String getRemove(@PathVariable long id, ModelMap map) {
		Product p = productRepository.getOne(id);
		map.addAttribute("id", id);
		map.addAttribute("name", p.getName());
		map.addAttribute("price", p.getPrice());
		map.addAttribute("category", p.getCategory());
		map.addAttribute("pnum", p.getPnum());
		map.addAttribute("imgurl", p.getImgurl());
		map.addAttribute("description", p.getDescription());
		return "/product/remove";
	}
	
	@PostMapping("/remove/{id}")
	public String postRemove(@PathVariable long id) {
		log.warn("删除的ID="+id);
		productRepository.deleteById(id);
		return "redirect:../../index.html";
	}
}

2 添加记录

在src/main/resources目录下的templates/product/文件夹中,添加文件add.html,代码如下:





添加产品







	
	
图片

3 删除记录

在src/main/resources目录下的templates/product文件夹下,添加文件remove.html,代码如下:





查看产品







	
	
图片
书名
价格
种类
数量

简介

4 完毕

你可能感兴趣的:(Spring,Boot学习)