2018-12-21





오늘은 새로운 카테고리를 하나 만들어 보았습니다.








먼저 모달형태의 주황색 버튼을 만들어주고






클릭을 하면 새로운 창이 두둥~!!






입력 후 등록을 누르면~!!




등록 성공!!





짜잔~





완성~!!


아래는 작성한 코드~!





모달 버튼을 누르면 아래의 코드가 실행 됩니다.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
<html>
  <head>
    
  </head>
 
  <body>
    <div class="modal fade" id="myCategoryModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel">
        <div class="modal-dialog" role="document">
            <div class="modal-content">
                <!-- Modal header -->
                <div class="modal-heaer">
                    <button style="margin-right:10px; margin-top:10px;" type="button" class="close" data-dismiss="modal" aria-label="Close">
                        <span aria-hidden="true">&times;</span></button>
                    <h4 style="margin-left: 10px; margin-top: 10px;" class="modal-title" id="myModalLabel">New Category</h4>
                </div>
                <div class="modal-body">
                    <!-- Category Form -->
                    <sf:form id="categoryForm" class="form-horizontal" modelAttribute="category" action="${contextRoot}/manage/category" method="POST">
                        
                        <div class="form-group">
                        
                            <label for="category_name" class="control-label col-md-4">Category Name</label>
                            <div class="col-md-8">
                                <sf:input type="text" path="name" id="category_name" class="form-control" />
                            </div>
                        </div>
                        
                        <div class="form-group">
                        
                            <label for="category_description" class="control-label col-md-4">Category Description</label>
                            <div class="col-md-8">
                                <sf:textarea type="text" path="description" id="category_description" class="form-control" />
                            </div>
                        </div>
                    
                        <div class="form-group">
                        
                            <div class="col-md-offset-4 col-md-8">
                                <input type="submit" value="Add Category" class="btn btn-primary" />
                            </div>
                            
                        </div>
                    
                    </sf:form>
                </div>
            </div>
        </div>
    </div>
  </body>
</html>




그리고 JAVA에서는 아래의 컨트롤러가 실행이 되지요~


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
package kr.astory.front.controller;
 
import java.util.List;
 
import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;
 
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
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.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;
 
import kr.astory.backend.dao.CategoryDAO;
import kr.astory.backend.dao.ProductDAO;
import kr.astory.backend.dto.Category;
import kr.astory.backend.dto.Product;
import kr.astory.front.util.FileUploadUtility;
import kr.astory.front.validator.ProductValidator;
 
@Controller
@RequestMapping(value="/manage")
public class ManagementController {
 
    @Autowired
    private CategoryDAO categoryDAO;
    
    @Autowired
    private ProductDAO productDAO;
    
    private static final Logger logger = LoggerFactory.getLogger(ManagementController.class); 
    
    @RequestMapping(value="/products", method=RequestMethod.GET)
    public ModelAndView showManageProducts(@RequestParam(name="operation", required=false) String operation) {
        ModelAndView mv = new ModelAndView("page");
        
        mv.addObject("userClickManageProduct", true);
        mv.addObject("title", "Manage Products");
        
        Product nProduct = new Product();
        
        //set few of the fields
        nProduct.setSupplierId(1);
        nProduct.setActive(true);
        
        mv.addObject("product", nProduct);
        
        if(operation != null) {
            
            if(operation.equals("product")) {
                mv.addObject("message", "Product Submitted Successfully");
            }
            else if(operation.equals("category")) {
                mv.addObject("message", "Category Submitted Successfully");
            }
            
        }
        
        return mv;
    }
    
    
    @RequestMapping(value="/{id}/product", method=RequestMethod.GET)
    public ModelAndView showEditProduct(@PathVariable int id) {
        ModelAndView mv = new ModelAndView("page");
        
        mv.addObject("userClickManageProduct", true);
        mv.addObject("title", "Manage Products");
        
        // fetch the product from the database
        Product nProduct = productDAO.get(id);
 
        //set the product fetch from database
        mv.addObject("product", nProduct);
        
        return mv;
    }
    
    //handling product submission
    @RequestMapping(value="/products", method=RequestMethod.POST)
    public String handleProductSubmission(@Valid @ModelAttribute("product") Product mProduct, BindingResult results, Model model, HttpServletRequest request) {
        
        // handle image validation for new products
        if(mProduct.getId() == 0) {
            new ProductValidator().validate(mProduct, results);
        }
        else {
            // edit check only when the file has been selected
            if(!mProduct.getFile().getOriginalFilename().equals("")) {
                new ProductValidator().validate(mProduct, results);
            }            
        }
        
        // check if there are any errors
        if(results.hasErrors()) {
            
            model.addAttribute("userClickManageProduct", true);
            model.addAttribute("title", "Manage Product");
            model.addAttribute("message", "Valiation failed for Product Submission!");
            
            return "page";
        }
        
        logger.info(mProduct.toString());
        
        // create a new product record
        if(mProduct.getId() == 0) {
            //create a new product record if id is 0
            productDAO.add(mProduct);
        }
        else {
            //update the product if id is not 0
            productDAO.update(mProduct);
        }
        
        //upload the file
        if(!mProduct.getFile().getOriginalFilename().equals("")) {
            FileUploadUtility.uploadFile(request, mProduct.getFile(), mProduct.getCode());
        }
        
        return "redirect:/manage/products?operation=product";
    }
    
    @RequestMapping(value="/product/{id}/activation", method=RequestMethod.POST)
    @ResponseBody
    public String handleProductActivation(@PathVariable int id) {
        
        // is going to fetch the product from the database
        Product product = productDAO.get(id);
        boolean isActive = product.isActive();
        // activating and deactivating based on the value of active field
        product.setActive(!product.isActive());
        // updating the product
        productDAO.update(product);
        
        return (isActive) 
                    ? "You have succesfully deactivated the product with id" + product.getId() 
                    : "You have succesfully activated the product with id" + product.getId();
    }
    
    // to handle category submission
    @RequestMapping(value="/category", method=RequestMethod.POST)
    public String handleCategorySubmission(@ModelAttribute Category category) {
        
        // add the new category
        categoryDAO.add(category);
        
        return "redirect:/manage/products/?operation=category";
    }
    
    // returning categories for all the request mapping
    @ModelAttribute("categories")
    public List<Category> getCategories() {
        return categoryDAO.list();
    }
    
    
    @ModelAttribute("category")
    public Category getCategory() {
        return new Category();
    }
    
    
    
    
}
 



오늘의 일지 끝~~!

'프로그래밍 > 작업일지' 카테고리의 다른 글

2018-12-20  (0) 2018.12.20
TAGS.

Comments