파일 업로드 COS.jar 등록
다운로드 사이트
다운로드 링크 주소 : http://servlets.com/cos/cos-26Dec2008.zip
AdminProductWriteFormAction
package net.macaronics.web.admin.controller; import java.io.IOException; import javax.servlet.ServletException; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import net.macaronics.web.controll.action.Action; public class AdminProductWriteFormAction implements Action { @Override public void execute(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { String url="admin/product/productWrite.jsp"; String kindList[] ={"남성운동화", "여성운동화","남성구두", "여성구두", "On Sale"}; request.setAttribute("kindList", kindList); request.getRequestDispatcher(url).forward(request, response); } }
productWrite.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %> <%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %> <!DOCTYPE html> <html class="no-js"> <head> <jsp:include page="../../include/AdminHeader.jsp" /> <!-- Bootstrap --> <link rel="stylesheet" type="text/css" href="../../Bootstrap-Admin/vendors/bootstrap-wysihtml5/src/bootstrap-wysihtml5.css"></link> </head> <body> <jsp:include page="../../include/AdminTopFixMenu.jsp" /> <div class="container-fluid"> <div class="row-fluid"> <div class="span3" id="sidebar"> <jsp:include page="../../include/AdminLeftMenu.jsp" /> </div> <!--/span--> <div class="span9" id="content"> <div class="row-fluid"> <div class="alert alert-success"> <button type="button" class="close" data-dismiss="alert">×</button> <h4>상품 등록</h4> </div> </div> <div> <form action="MacaronicsServlet?command=admin_product_write" method="post" enctype="multipart/form-data" name="form1"> <div class="table-responsive"> <table class="table"> <tr> <th class="success">상품 분류</th> <td colspan="5"> <select class="span6 m-wrap" name="kind"> <c:forEach items="${kindList }" var="kind" varStatus="status"> <option value="${status.count}">${kind }</option> </c:forEach> </select> </td> </tr> <tr> <th class="success">상품명</th> <td colspan="5"><input type="text" name="name" id="name"></td> </tr> <tr> <th>원가</th><td><input type="number" name="price1" id="price1" min="1000"></td> <th>판매가</th><td><input type="text" name="price2" id="price2" onblur="go_ab()" min="1000"></td> <th>마진</th><td width="10%"><input type="text" name="price3" id="price3" readonly="readonly"></td> </tr> <tr> <th>베스트 상품</th><td><input type="checkbox" name="bestyn" /></td> <th>사용유무</th><td colspan="3"><input type="checkbox" name="useyn" checked="checked"/></td> </tr> <tr> <th>상세설명</th> <td colspan="5"> <textarea id="bootstrap-editor" style="width:98%;height:200px;" name="content"></textarea> </td> </tr> <tr> <th>상품 이미지</th> <td colspan="5"> <input type="file" name="image" id="image"> </td> </tr> </table> </div> <div class="text-center"> <input type="button" value="등록" class="btn btn-success" onclick="productSubmit()"> <input type="reset" value="취소" class="btn btn-warning"> </div> </form> </div> </div> </div> </div> <hr> <div style="margin-bottom: 150px;"> </div> <!-- FOOTER --> <nav class="avbar navbar-inverse navbar-fixed-bottom" style="height:50px ; background-color:#000 ;"> <div class="container text-center" style="color:#fff;"> <p> </p> <p ><a href="http://macaronics.net/" style=" color:#fff;">Macaronics.net</a></p> <p> </p><p> </p> </div> </nav> <!--/.fluid-container--> <script src="../../Bootstrap-Admin/vendors/bootstrap-wysihtml5/lib/js/wysihtml5-0.3.0.js"></script> <script src="../../Bootstrap-Admin/vendors/jquery-1.9.1.min.js"></script> <script src="../../Bootstrap-Admin/bootstrap/js/bootstrap.min.js"></script> <script src="../../Bootstrap-Admin/vendors/bootstrap-wysihtml5/src/bootstrap-wysihtml5.js"></script> <script src="../../Bootstrap-Admin/vendors/bootstrap-wysihtml5/src/bootstrap-wysihtml5.js"></script> <script type="text/javascript"> $(function() { // Bootstrap $('#bootstrap-editor').wysihtml5(); }); function go_ab(){ var price1=$("#price1").val(); var price2=$("#price2").val(); if($.trim(price2) <= $.trim(price1)){ alert("판매가는 원가 보다 작을 수 없습니다."); $("#price1").val(""); $("#price2").val(""); $("#price1").focus(); return; } $("#price3").val(price2-price1); } function productSubmit(){ var name=$("#name").val(); var price1=$("#price1").val(); var price2=$("#price2").val(); var content =$("content").val(); if(name==""){ alert("상품명을 입력해 주세요."); $("#name").focus(); return ; } if(price1==""){ alert("원가를 입력해 주세요."); $("#price1").focus(); return ; } if(price2==""){ alert("판매가를 입력해 주세요."); $("#price2").focus(); return; } if( $("#image").val() != "" ){ var ext = $('#image').val().split('.').pop().toLowerCase(); if($.inArray(ext, ['gif','png','jpg','jpeg']) == -1) { alert('gif,png,jpg,jpeg 파일만 업로드 할수 있습니다.'); return; } document.form1.submit(); }else{ alert('이미지를 선택해 주세요'); return; } } </script> </body> </html>
AdminProductWriteAction
package net.macaronics.web.admin.controller; import java.io.IOException; import javax.servlet.ServletContext; import javax.servlet.ServletException; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import javax.servlet.http.HttpSession; import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.Logger; import com.oreilly.servlet.MultipartRequest; import com.oreilly.servlet.multipart.DefaultFileRenamePolicy; import net.macaronics.web.admin.controller.dao.AdminProductDAO; import net.macaronics.web.controll.action.Action; import net.macaronics.web.dto.ProductVO; public class AdminProductWriteAction implements Action { private static Logger logger =LogManager.getLogger(AdminProductWriteAction.class); @Override public void execute(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { String url="MacaronicsServlet?command=admin_product_list"; HttpSession session=request.getSession(); int sizeLimit =5 * 1024 * 1024; String savePath="images"; String uploadFilePath =session.getServletContext().getRealPath(savePath); MultipartRequest multi=new MultipartRequest(request, //1.요청객체 uploadFilePath, //2.업로드될 파일이 저장될 파일 경로명 sizeLimit, //3. 업로드될 파일의 최대 크기 (5MB) "UTF-8", //4. 인코딩 타입 지정 new DefaultFileRenamePolicy() // 5. 덮어씌기를 방지 위한 부분 ); // 이 시점을 기해 파일은 이미 저장이 되었다. String bestyn=multi.getParameter("bestyn"); String useyn=multi.getParameter("useyn"); if(bestyn==null) bestyn="n"; else bestyn="y"; if(useyn==null) useyn="n"; else useyn="y"; ProductVO productVO=new ProductVO(); productVO.setKind(multi.getParameter("kind")); productVO.setName(multi.getParameter("name")); productVO.setPrice1(Integer.parseInt(multi.getParameter("price1"))); productVO.setPrice2(Integer.parseInt(multi.getParameter("price2"))); productVO.setPrice3(Integer.parseInt(multi.getParameter("price2")) - Integer.parseInt(multi.getParameter("price1"))); productVO.setBestyn(bestyn); productVO.setUseyn(useyn); productVO.setContent(multi.getParameter("content")); productVO.setImage(multi.getFilesystemName("image")); logger.info("AdminProductWriteAction - {}" , productVO.toString()); AdminProductDAO adminProductDAO =AdminProductDAO.getInstance(); adminProductDAO.insertProudct(productVO); response.sendRedirect(url); } }
DAO
//상품 등록 public void insertProudct(ProductVO product){ try{ sqlSession=MybatisService.getFactory().openSession(); sqlSession.insert("adminProduct.insertProudct", product); }catch(Exception e){ e.printStackTrace(); }finally{ sqlSession.commit(); MybatisService.sessionClose(sqlSession); } }
mapper
<insert id="insertProudct"> insert into TBL_PRODUCT(pseq, name, kind, price1, price2, price3, content, image , useyn, bestyn) values( product_seq.nextval, #{name}, #{kind}, #{price1}, #{price2}, #{price3}, #{content}, #{image}, #{useyn}, #{bestyn} ) </insert>
제작 : macaronics.net - Developer Jun Ho Choi
소스 : https://github.com/braverokmc79/jsp_sin
${request.getContextPath() } 처리를 안한 부분이 있으므로
루트 설정( http://macaronics.net/index.php/m01/jsp/view/1352) 및 server.xml 에서 DB 컨넥션 설정은 필수 설정이다.
댓글 ( 4)
댓글 남기기