1. Search Products
Find out all the information from the database ssm1 products in the product table, and show on the page.
1.1 Create a new product category in the domain package for packaging products
Because the database departureTime is Date type, productStatus only 0 or 1, for these two new data also shows the need to add two String type on the page in the product class member variables departureTimeStr and productStatusStr, the original two variables encapsulated type String.
. 1 Package club.nipengfei.domain; 2 . 3 Import club.nipengfei.utils.DateUtils; . 4 . 5 Import java.util.Date; . 6 . 7 public class Product { . 8 Private String ID; // primary key . 9 Private String productNum; // Number The only 10 Private String productName; // name 11 Private String cityName; // departure city 12 Private a Date departureTime; // departure time 13 Private String departureTimeStr; 14 Private Double productPrice; // Price 15 Private String productDesc; // Product Description 16 Private Integer productStatus; // state 0 open closed 1 . 17 Private String productStatusStr; 18 is . 19 public String getId () { 20 is return ID ; 21 is } 22 is 23 is public void the setId (String ID) { 24 the this .id = ID; 25 } 26 is 27 public String getProductNum() { 28 return productNum; 29 } 30 31 public void setProductNum(String productNum) { 32 this.productNum = productNum; 33 } 34 35 public String getProductName() { 36 return productName; 37 } 38 39 public void setProductName(String productName) { 40 this.productName = productName; 41 } 42 43 public String getCityName() { 44 return cityName; 45 } 46 47 public void setCityName(String cityName) { 48 this.cityName = cityName; 49 } 50 51 public Date getDepartureTime() { 52 return departureTime; 53 } 54 55 public void setDepartureTime(Date departureTime) { 56 this.departureTime = departureTime; 57 } 58 59 public String getDepartureTimeStr() { 60 if (departureTime != null){ 61 departureTimeStr = DateUtils.date2String(departureTime,"yyyy-MM-dd HH:mm:ss"); 62 } 63 return departureTimeStr; 64 } 65 66 public void setDepartureTimeStr(String departureTimeStr) { 67 this.departureTimeStr = departureTimeStr; 68 } 69 70 public double getProductPrice() { 71 return productPrice; 72 } 73 74 public void setProductPrice(double productPrice) { 75 this.productPrice = productPrice; 76 } 77 78 public String getProductDesc() { 79 return productDesc; 80 } 81 82 public void setProductDesc(String productDesc) { 83 this.productDesc = productDesc; 84 } 85 86 public Integer getProductStatus() { 87 return productStatus; 88 } 89 90 public void setProductStatus(Integer productStatus) { 91 this.productStatus = productStatus; 92 } 93 94 public String getProductStatusStr() { 95 if (productStatus == 0){ 96 productStatusStr="关闭"; 97 }else if (productStatus == 1){ 98 productStatusStr="开启"; 99 } 100 return productStatusStr; 101 } 102 103 public void setProductStatusStr(String productStatusStr) { 104 this.productStatusStr = productStatusStr; 105 } 106 }
. 1 Package club.nipengfei.utils; 2 . 3 Import the java.text.SimpleDateFormat; . 4 Import java.util.Date; . 5 . 6 public class DateUtils { . 7 . 8 / ** . 9 * converted into a string patt date of 10 * @ param DATE . 11 * @param PATT 12 is * @return 13 is * / 14 public static String date2String (a Date DATE, String PATT) { 15 the SimpleDateFormat SDF = new new SimpleDateFormat(patt); 16 String format = sdf.format(date); 17 return format; 18 } 19 }
1.2 New dao package in a IProductDao interfaces, write a query findAll method and writes data by @Select notes sql statement
1 package club.nipengfei.dao; 2 3 import club.nipengfei.domain.Product; 4 import org.apache.ibatis.annotations.Select; 5 6 import java.util.List; 7 8 public interface IProductDao { 9 /** 10 * 查询所有产品 11 * @return 12 */ 13 @Select("select * from product") 14 List<Product> findAll() throws Exception; 15 }
1.3 Create a IProductService interfaces at the service package and the new package in a ProductServiceImpl impl interfaces implemented IProductService
1 package club.nipengfei.service.impl; 2 3 import club.nipengfei.dao.IProductDao; 4 import club.nipengfei.domain.Product; 5 import club.nipengfei.service.IProductService; 6 import org.springframework.beans.factory.annotation.Autowired; 7 import org.springframework.stereotype.Service; 8 import org.springframework.transaction.annotation.Transactional; 9 10 import java.util.List; 11 12 @Service 13 @Transactional 14 public class ProductServiceImpl implements IProductService{ 15 16 @Autowired 17 private IProductDao productDao; 18 19 public List<Product> findAll() throws Exception { 20 return productDao.findAll(); 21 } 22 }
1.4 in a new class ProductController packet controller, a write method findAll
Since this method is the product information from the database query is returned to the front end of product-list.jsp pages, the return type is ModelAndView. The product information is added to the list of query objects ModelAndView class generation, the parameter attributeName which the object needs to be consistent with the front page.
1 package club.nipengfei.controller; 2 3 import club.nipengfei.domain.Product; 4 import club.nipengfei.service.impl.ProductServiceImpl; 5 import org.springframework.beans.factory.annotation.Autowired; 6 import org.springframework.stereotype.Controller; 7 import org.springframework.web.bind.annotation.RequestMapping; 8 import org.springframework.web.servlet.ModelAndView; 9 10 import java.util.List; 11 12 @Controller 13 @RequestMapping("/product") 14 public class ProductController { 15 16 @Autowired 17 private ProductServiceImpl productService; 18 19 @RequestMapping("/findAll.do") 20 public ModelAndView findAll() throws Exception { 21 ModelAndView mv = new ModelAndView(); 22 List<Product> ps = productService.findAll(); 23 mv.addObject("productList",ps); 24 mv.setViewName("product-list"); 25 return mv; 26 } 27 }
1.5 show the front page (portion)
1 <c:forEach items="${productList}" var="product"> 2 3 <tr> 4 <td><input name="ids" type="checkbox"></td> 5 <td>${product.id }</td> 6 <td>${product.productNum }</td> 7 <td>${product.productName }</td> 8 <td>${product.cityName }</td> 9 <td>${product.departureTimeStr }</td> 10 <td class="text-center">${product.productPrice }</td> 11 <td>${product.productDesc }</td> 12 <td class="text-center">${product.productStatusStr }</td> 13 <td class="text-center"> 14 <button type="button" class="btn bg-olive btn-xs">订单</button> 15 <button type="button" class="btn bg-olive btn-xs">详情</button> 16 <button type="button" class="btn bg-olive btn-xs">编辑</button> 17 </td> 18 </tr> 19 </c:forEach>
1.6 problems arise: 500 Error reported
When you run the project found 500 error report, the inquiry found that the error was an internal server error, so I create a new page in the pages hello.jsp package, will jump index.jsp page changed pages / hello.jsp can find normal visit, later found the wrong reasons is the introduction of aside.jsp in main.jsp page, and the page labeled using spring-security, and coordinate my pom.xml are some basic integration framework used by ssm coordinates not coordinate spring-security-related, so I introduced coordinate this part of the discovery can be accessed in pom.xml.
2. Save Product Info
Products show will save the front page product-add.jsp page fill in the product information into the database, and keep the
2.1 a write save method IProduceDao interface, and write data by inserting annotation sql statement @Insert
Because it is a method of storing data, it is necessary to save a product parameter passing method
1 package club.nipengfei.dao; 2 3 import club.nipengfei.domain.Product; 4 import org.apache.ibatis.annotations.Insert; 5 import org.apache.ibatis.annotations.Select; 6 7 import java.util.List; 8 9 public interface IProductDao { 10 /** 11 * 查询所有产品 12 * @return 13 */ 14 @Select("select * from product") 15 List<Product> findAll() throws Exception; 16 17 @Insert("insert into product (productNum,productName,cityName,departureTime,productPrice,productDesc,productStatus) values(#{productNum},#{productName},#{cityName},#{departureTime},#{productPrice},#{productDesc},#{productStatus})") 18 void save(Product product)throws Exception; 19 }
2.2 Write a save method in class ProductServiceImpl
1 package club.nipengfei.service.impl; 2 3 import club.nipengfei.dao.IProductDao; 4 import club.nipengfei.domain.Product; 5 import club.nipengfei.service.IProductService; 6 import org.springframework.beans.factory.annotation.Autowired; 7 import org.springframework.stereotype.Service; 8 import org.springframework.transaction.annotation.Transactional; 9 10 import java.util.List; 11 12 @Service 13 @Transactional 14 public class ProductServiceImpl implements IProductService{ 15 16 @Autowired 17 private IProductDao productDao; 18 19 public List<Product> findAll() throws Exception { 20 return productDao.findAll(); 21 } 22 23 public void save(Product product) throws Exception { 24 productDao.save(product); 25 } 26 }
2.3 a write save method in class ProductController
Because when we click the Save button to submit the form information, you need to re-query the database to show product information in product-list.jsp page, so the return value of the method is the type String, the value of "redirect: findAll.do"
1 package club.nipengfei.controller; 2 3 import club.nipengfei.domain.Product; 4 import club.nipengfei.service.impl.ProductServiceImpl; 5 import org.springframework.beans.factory.annotation.Autowired; 6 import org.springframework.stereotype.Controller; 7 import org.springframework.web.bind.annotation.RequestMapping; 8 import org.springframework.web.servlet.ModelAndView; 9 10 import java.util.List; 11 12 @Controller 13 @RequestMapping("/product") 14 public class ProductController { 15 16 @Autowired 17 private ProductServiceImpl productService; 18 19 @RequestMapping("/findAll.do") 20 public ModelAndView findAll() throws Exception { 21 ModelAndView mv = new ModelAndView(); 22 List<Product> ps = productService.findAll(); 23 mv.addObject("productList",ps); 24 mv.setViewName("product-list"); 25 return mv; 26 } 27 28 @RequestMapping("/save.do") 29 public String save(Product product) throws Exception { 30 productService.save(product); 31 return "redirect:findAll.do"; 32 } 33 }
2.4 problems arise: 400 Error reported
When you run the program no error, but the error occurs when http400 fill out the form information submitted, this error indicates an invalid request, the data is passed to the background of a problem.
View IDEA console error messages:
DESCRIPTION error when the package product, the form submission date data of type String, but the product is departureTime Date type, String type of conversion necessary to Date type.
Attributes directly departureTime product class annotate @DateTimeFormat (pattern = "yyyy-MM -dd HH: mm"), the type of pattern to make the annotation string received into Date type. Reference: https://blog.csdn.net/java_zhangshuai/article/details/95951400