Advertisement
KuoHsiangYu

/SpringMVC1/src/main/java/com/web/store/controller/HomeContr

Jul 3rd, 2019
225
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.92 KB | None | 0 0
  1. //檔案位置:/SpringMVC1/src/main/java/com/web/store/controller/HomeController.java
  2. //這是 Controller code
  3. package com.web.store.controller;
  4.  
  5. import java.io.UnsupportedEncodingException;
  6. import java.sql.Blob;
  7.  
  8. import javax.servlet.http.HttpServletRequest;
  9. import javax.sql.rowset.serial.SerialBlob;
  10.  
  11. import org.springframework.beans.factory.annotation.Autowired;
  12. import org.springframework.stereotype.Controller;
  13. import org.springframework.ui.Model;
  14. import org.springframework.validation.BindingResult;
  15. import org.springframework.web.bind.annotation.ModelAttribute;
  16. import org.springframework.web.bind.annotation.RequestMapping;
  17. import org.springframework.web.bind.annotation.RequestMethod;
  18. import org.springframework.web.multipart.MultipartFile;
  19.  
  20. import com.web.store.model.RecipeBean;
  21. import com.web.store.service.IProductService;
  22.  
  23. @Controller
  24. public class HomeController {
  25.  
  26.     private IProductService service = null;
  27.  
  28.     @Autowired
  29.     public HomeController(IProductService service) {
  30.         /* 從建構子注入service屬性 */
  31.         this.service = service;
  32.     }
  33.  
  34.     @RequestMapping("/")
  35.     public String home() {
  36.         // 這個method是一開始顯示首頁用的
  37.         return "index";
  38.     }
  39.  
  40.     @RequestMapping("/home")
  41.     public String backHome() {
  42.         // 這個method是為了讓我返回首頁用的
  43.         return "index";
  44.     }
  45.  
  46.     @RequestMapping(value = "/insert")
  47.     public String insert() {
  48.         // 新增
  49.         // System.out.println("insert");
  50.         return "insert";
  51.     }
  52.  
  53.     @RequestMapping("/delete")
  54.     public String delete() {
  55.         // 刪除
  56.         return "delete";
  57.     }
  58.  
  59.     @RequestMapping("/update")
  60.     public String update() {
  61.         // 修改
  62.         return "update";
  63.     }
  64.  
  65.     @RequestMapping("/select")
  66.     public String select() {
  67.         // 查詢
  68.         return "select";
  69.     }
  70.  
  71. //  @RequestMapping("/InsertForm")
  72. //  public String insertForm(HttpServletRequest request) throws UnsupportedEncodingException {
  73. //      這是在學SpringMVC框架之前寫過的程式。
  74. //      request.setCharacterEncoding("UTF-8");
  75. //      // System.out.println("@RequestMapping(\"/InsertForm\")");
  76. //      // recipe_name
  77. //      // System.out.println("request.getParameter(\"recipe_name\")" +
  78. //      // request.getParameter("recipe_name"));
  79. //      RecipeBean recipeBean=new RecipeBean();
  80. //      recipeBean.setRecipe_name(request.getParameter("recipe_name"));
  81. //      recipeBean.setRecipe_quantity(request.getParameter("Recipe_quantity"));
  82. //      recipeBean.setRecipe_image(null);//加入圖片
  83. //      recipeBean.setRecipe_summary(request.getParameter("recipe_summary"));
  84. //      recipeBean.setRecipe_time(request.getParameter("recipe_time"));
  85. //      recipeBean.setRecipe_note(request.getParameter("recipe_note"));
  86. //      recipeBean.setRecipe_date(null);//加入日期
  87. //      service.insertRecipe(recipeBean);
  88. //      return "successPage";//有一個JSP叫successPage.jsp
  89. //  }
  90.  
  91.     /* 跟 <a href="insert">新增</a> 有關 */
  92.     @RequestMapping(value = "/insert", method = RequestMethod.GET)
  93.     public String getAddNewProductForm(Model model) {
  94.         System.out.println("#1");
  95.         RecipeBean recipe = new RecipeBean();
  96.         model.addAttribute("RecipeBean", recipe);
  97.         return "insert";/* 呼叫insert.jsp檔案 */
  98.     }
  99.  
  100.     @RequestMapping(value = "/insert", method = RequestMethod.POST)
  101.     public String processAddNewProductForm(@ModelAttribute("RecipeBean") RecipeBean recipe, Model model,
  102.             BindingResult result, HttpServletRequest request) throws UnsupportedEncodingException {
  103.         request.setCharacterEncoding("UTF-8");
  104.         System.out.println("#2");
  105.         System.out.println("recipeBean.getRecipe_name() -> " + recipe.getRecipe_name());
  106.         System.out.println("request.getParameter(\"recipe_name\") -> " + request.getParameter("recipe_name"));
  107.         recipe.setRecipe_name("測試名字");
  108.         // 重要事項-start
  109.         // 這三個地方一定要一樣,都只打RecipeBean
  110.         // <form:form method="POST" modelAttribute="【RecipeBean】"
  111.         // model.addAttribute("【RecipeBean】", recipeBean);
  112.         // public String processAddNewProductForm(@ModelAttribute("【RecipeBean】")
  113.         // RecipeBean recipeBean
  114.         // 重要事項-end
  115.  
  116.         /* 開始處理圖片檔案-start */
  117.         MultipartFile imageFile = recipe.getImage_file();
  118.         // 建立Blob物件,交由 Hibernate 寫入資料庫
  119.         if (imageFile != null && !imageFile.isEmpty()) {
  120.             String originalFilename = imageFile.getOriginalFilename();
  121.             recipe.setFileName(originalFilename);
  122.  
  123.             try {
  124.                 byte[] byteArray = imageFile.getBytes();
  125.                 Blob recipe_image = new SerialBlob(byteArray);
  126.                 recipe.setRecipe_image(recipe_image);
  127.             } catch (Exception e) {
  128.                 e.printStackTrace();
  129.                 throw new RuntimeException("圖片上傳發生異常:" + e.getMessage());
  130.             }
  131.         }
  132.         /* 開始處理圖片檔案-end */
  133.  
  134.         service.insertRecipe(recipe);
  135.  
  136.         // 將上傳的檔案移到指定的資料夾
  137.         return "redirect:/successPage";/* 讓瀏覽器再次發出請求,呼叫successPage.jsp檔案 */
  138.     }// end of processAddNewProductForm mathod
  139.  
  140.     @RequestMapping(value = "/successPage")
  141.     public String successPage() {
  142.         return "successPage";
  143.     }
  144. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement