從數據庫ssm1中product表中查出全部的產品信息,並在頁面中展現。前端
因爲數據庫中的departureTime是Date類型,productStatus只是0或1,爲了將這兩個數據也展現在頁面上在product類中須要新加兩個String類型的成員變量departureTimeStr和productStatusStr,將原先的兩個變量封裝成String類型。java
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; // 主鍵 9 private String productNum; // 編號 惟一 10 private String productName; // 名稱 11 private String cityName; // 出發城市 12 private Date departureTime; // 出發時間 13 private String departureTimeStr; 14 private double productPrice; // 產品價格 15 private String productDesc; // 產品描述 16 private Integer productStatus; // 狀態 0 關閉 1 開啓 17 private String productStatusStr; 18 19 public String getId() { 20 return id; 21 } 22 23 public void setId(String id) { 24 this.id = id; 25 } 26 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 java.text.SimpleDateFormat; 4 import java.util.Date; 5 6 public class DateUtils { 7 8 /** 9 * 將date轉換成patt的字符串形式 10 * @param date 11 * @param patt 12 * @return 13 */ 14 public static String date2String(Date date,String patt){ 15 SimpleDateFormat sdf = new SimpleDateFormat(patt); 16 String format = sdf.format(date); 17 return format; 18 } 19 }
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 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 }
因爲該方法是從數據庫中查詢產品信息,返回給前端product-list.jsp頁面,所以返回值類型是ModelAndView。將查詢的信息product列表添加到ModelAndView類生成的對象中,其中對象的參數attributeName須要與前端頁面一致。web
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 <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>
當運行該項目時發現報500錯誤,查詢發現該錯誤是服務器內部錯誤,因而我在pages包下新建一個hello.jsp頁面,將index.jsp頁面中的跳轉改成pages/hello.jsp發現可以正常訪問,後來發現錯誤緣由是在main.jsp頁面中引入了aside.jsp,而該頁面中有使用spring-security的標籤,而個人pom.xml中的座標是ssm框架整合用到的一些基本座標沒有有關spring-security的座標,因而我在pom.xml中引入了這部分的座標發現可以正常訪問。spring
將前端頁面product-add.jsp頁面填寫的產品信息保存到數據庫中,並將保存好後的產品信息展現sql
因爲是一個保存數據的方法,所以須要給save方法傳入一個product參數數據庫
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 }
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 }
因爲當咱們點擊保存按鈕提交表單信息後,須要從新從數據庫中查詢產品信息展現在product-list.jsp頁面中,所以該方法的返回值類型是String,值爲"redirect:findAll.do"apache
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 }
當運行該項目時沒有報錯,可是當填好表單信息提交時出現http400的錯誤,該錯誤的出現表示請求無效,傳遞給後臺的數據有問題。服務器
查看IDEA控制檯的錯誤信息:app
說明在封裝product時出現錯誤,表單提交的是String類型的日期數據,可是product中departureTime是Date類型,須要將String類型的轉換成Date類型。框架
能夠直接在product類中的departureTime屬性加上註解@DateTimeFormat(pattern="yyyy-MM-dd HH:mm"),給註解使接收到的pattern類型的字符串轉換成Date類型。參考:https://blog.csdn.net/java_zhangshuai/article/details/95951400