Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //檔案位置:/SpringMVC1/src/main/java/com/web/store/controller/HomeController.java
- //這是 Controller code
- package com.web.store.controller;
- import java.io.UnsupportedEncodingException;
- import java.sql.Blob;
- import javax.servlet.http.HttpServletRequest;
- import javax.sql.rowset.serial.SerialBlob;
- import org.springframework.beans.factory.annotation.Autowired;
- import org.springframework.stereotype.Controller;
- import org.springframework.ui.Model;
- import org.springframework.validation.BindingResult;
- import org.springframework.web.bind.annotation.ModelAttribute;
- import org.springframework.web.bind.annotation.RequestMapping;
- import org.springframework.web.bind.annotation.RequestMethod;
- import org.springframework.web.multipart.MultipartFile;
- import com.web.store.model.RecipeBean;
- import com.web.store.service.IProductService;
- @Controller
- public class HomeController {
- private IProductService service = null;
- @Autowired
- public HomeController(IProductService service) {
- /* 從建構子注入service屬性 */
- this.service = service;
- }
- @RequestMapping("/")
- public String home() {
- // 這個method是一開始顯示首頁用的
- return "index";
- }
- @RequestMapping("/home")
- public String backHome() {
- // 這個method是為了讓我返回首頁用的
- return "index";
- }
- @RequestMapping(value = "/insert")
- public String insert() {
- // 新增
- // System.out.println("insert");
- return "insert";
- }
- @RequestMapping("/delete")
- public String delete() {
- // 刪除
- return "delete";
- }
- @RequestMapping("/update")
- public String update() {
- // 修改
- return "update";
- }
- @RequestMapping("/select")
- public String select() {
- // 查詢
- return "select";
- }
- // @RequestMapping("/InsertForm")
- // public String insertForm(HttpServletRequest request) throws UnsupportedEncodingException {
- // 這是在學SpringMVC框架之前寫過的程式。
- // request.setCharacterEncoding("UTF-8");
- // // System.out.println("@RequestMapping(\"/InsertForm\")");
- // // recipe_name
- // // System.out.println("request.getParameter(\"recipe_name\")" +
- // // request.getParameter("recipe_name"));
- // RecipeBean recipeBean=new RecipeBean();
- // recipeBean.setRecipe_name(request.getParameter("recipe_name"));
- // recipeBean.setRecipe_quantity(request.getParameter("Recipe_quantity"));
- // recipeBean.setRecipe_image(null);//加入圖片
- // recipeBean.setRecipe_summary(request.getParameter("recipe_summary"));
- // recipeBean.setRecipe_time(request.getParameter("recipe_time"));
- // recipeBean.setRecipe_note(request.getParameter("recipe_note"));
- // recipeBean.setRecipe_date(null);//加入日期
- // service.insertRecipe(recipeBean);
- // return "successPage";//有一個JSP叫successPage.jsp
- // }
- /* 跟 <a href="insert">新增</a> 有關 */
- @RequestMapping(value = "/insert", method = RequestMethod.GET)
- public String getAddNewProductForm(Model model) {
- System.out.println("#1");
- RecipeBean recipe = new RecipeBean();
- model.addAttribute("RecipeBean", recipe);
- return "insert";/* 呼叫insert.jsp檔案 */
- }
- @RequestMapping(value = "/insert", method = RequestMethod.POST)
- public String processAddNewProductForm(@ModelAttribute("RecipeBean") RecipeBean recipe, Model model,
- BindingResult result, HttpServletRequest request) throws UnsupportedEncodingException {
- request.setCharacterEncoding("UTF-8");
- System.out.println("#2");
- System.out.println("recipeBean.getRecipe_name() -> " + recipe.getRecipe_name());
- System.out.println("request.getParameter(\"recipe_name\") -> " + request.getParameter("recipe_name"));
- recipe.setRecipe_name("測試名字");
- // 重要事項-start
- // 這三個地方一定要一樣,都只打RecipeBean
- // <form:form method="POST" modelAttribute="【RecipeBean】"
- // model.addAttribute("【RecipeBean】", recipeBean);
- // public String processAddNewProductForm(@ModelAttribute("【RecipeBean】")
- // RecipeBean recipeBean
- // 重要事項-end
- /* 開始處理圖片檔案-start */
- MultipartFile imageFile = recipe.getImage_file();
- // 建立Blob物件,交由 Hibernate 寫入資料庫
- if (imageFile != null && !imageFile.isEmpty()) {
- String originalFilename = imageFile.getOriginalFilename();
- recipe.setFileName(originalFilename);
- try {
- byte[] byteArray = imageFile.getBytes();
- Blob recipe_image = new SerialBlob(byteArray);
- recipe.setRecipe_image(recipe_image);
- } catch (Exception e) {
- e.printStackTrace();
- throw new RuntimeException("圖片上傳發生異常:" + e.getMessage());
- }
- }
- /* 開始處理圖片檔案-end */
- service.insertRecipe(recipe);
- // 將上傳的檔案移到指定的資料夾
- return "redirect:/successPage";/* 讓瀏覽器再次發出請求,呼叫successPage.jsp檔案 */
- }// end of processAddNewProductForm mathod
- @RequestMapping(value = "/successPage")
- public String successPage() {
- return "successPage";
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement