티스토리 뷰
1. 이제 글쓰기를 작성해볼것이다
write_view.jsp
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061 <%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>Insert title here</title> </head> <body> <table width="500" cellpadding="0" cellspacing="0" border="1"> <form action="write" method="post"> <tr> <td> 이름 </td> <td> <input type="text" name="bName" size = "50"> </td> </tr> <tr> <td> 제목 </td> <td> <input type="text" name="bTitle" size = "50"> </td> </tr> <tr> <td> 내용 </td> <td> <textarea name="bContent" rows="10" ></textarea> </td> </tr> <tr > <td colspan="2"> <input type="submit" value="입력"> <a href="list.do">목록보기</a></td> </tr> </form> </table> </body> </html> cs
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 | <%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>Insert title here</title> </head> <body> <table width="500" cellpadding="0" cellspacing="0" border="1"> <form action="write" method="post"> <tr> <td> 이름 </td> <td> <input type="text" name="bName" size = "50"> </td> </tr> <tr> <td> 제목 </td> <td> <input type="text" name="bTitle" size = "50"> </td> </tr> <tr> <td> 내용 </td> <td> <textarea name="bContent" rows="10" ></textarea> </td> </tr> <tr > <td colspan="2"> <input type="submit" value="입력"> <a href="list.do">목록보기</a></td> </tr> </form> </table> </body> </html> | cs |
2. 현재 글쓰기 게시판 만들기의 호출 순서를 보면Controller -> Command -> Dao -> Dto 순서로 호출이 된다.
여기서 중요한것이, 이벤트 발생했을시 넘어가는 부분 처리도 생각해줘야한다는것(JSP파일에서)
Conroller.java
Conroller.java
1234567891011121314151617181920212223242526272829303132333435363738394041424344 // 2. write_view로 요청이 들어왔을 경 @RequestMapping("/write_view") public String write_view(Model model) { System.out.println("write_view Call"); return "write_view"; } // 3. 데이터 처리를 해주기 위한 Mapping(HttpServeltReqeust를 받는 // 왜? 위에서 처리한 write_view의 폼을 받기 위해 // 여기서 회원가입을 다 처리한다음에 이어질것을 list화면으로 redirect 하기위함 // addAttribute 란 모델안에 "reqeust"라는 이름으로 모델에 추가후 jsp파일에서 ${reqeust} 형식으로 사용하기위 @RequestMapping("/write") public String write(HttpServletRequest request,Model model) { System.out.println("write()"); model.addAttribute("request",request); command = new BWriteCommand(); command.execute(model); return "redirect:list" } cs
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 | // 2. write_view로 요청이 들어왔을 경 @RequestMapping("/write_view") public String write_view(Model model) { System.out.println("write_view Call"); return "write_view"; } // 3. 데이터 처리를 해주기 위한 Mapping(HttpServeltReqeust를 받는 // 왜? 위에서 처리한 write_view의 폼을 받기 위해 // 여기서 회원가입을 다 처리한다음에 이어질것을 list화면으로 redirect 하기위함 // addAttribute 란 모델안에 "reqeust"라는 이름으로 모델에 추가후 jsp파일에서 ${reqeust} 형식으로 사용하기위 @RequestMapping("/write") public String write(HttpServletRequest request,Model model) { System.out.println("write()"); model.addAttribute("request",request); command = new BWriteCommand(); command.execute(model); return "redirect:list" } | cs |
Command.java
1234567891011121314151617181920212223242526272829303132333435 package com.javalec.spring_pjt_board_command; import java.util.Map; import javax.servlet.http.HttpServletRequest; import org.springframework.ui.Model; import com.javalec.spring_pjt_board_dao.BDao; public class BWriteCommand implements BCommand { @Override public void execute(Model model) { // TODO Auto-generated method stub Map<String, Object> map = model.asMap(); HttpServletRequest request = (HttpServletRequest) map.get("request"); String bName = request.getParameter("bName"); String bTitle = request.getParameter("bTitle"); String bContent = request.getParameter("bContent"); BDao dao = new BDao(); dao.write(bName,bTitle,bContent); } } Command에서 Dao로 요청들어온값을 보내준다. (요청 데이터 처리) cs
DAO.java(DB스키마 설계)
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299 package com.javalec.spring_pjt_board_dao; import java.sql.Connection; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; import java.sql.Timestamp; import java.util.ArrayList; import javax.naming.Context; import javax.naming.InitialContext; import javax.naming.NamingException; import javax.sql.DataSource; import com.javalec.spring_pjt_board_dto.BDto; //database에 접근해서 작업 하는구 public class BDao { // datasoruce는 context에서 가져오는부분이다. DataSource dataSource; public BDao() { // TODO Auto-generated constructor stub try { Context context = new InitialContext(); dataSource = (DataSource) context.lookup("java:comp/env/jdbc/SpringDS"); } catch (NamingException e) { // TODO Auto-generated catch block e.printStackTrace(); } } //write 받아오는 부분 BWriteCommand에서 public void write(String bName, String bTitle, String bContent) { // TODO Auto-generated method stub Connection connection = null; PreparedStatement preparedStatement = null; int cnt=0; try { connection = dataSource.getConnection(); String query = "insert into mvc_board (bId, bName, bTitle, bContent, bHit, bGroup, bStep, bIndent) values ("+(cnt++)+"0, ?, ?, ?, 0,0,0,0)"; preparedStatement = connection.prepareStatement(query); preparedStatement.setString(1, bName); preparedStatement.setString(2, bTitle); preparedStatement.setString(3, bContent); int rn = preparedStatement.executeUpdate(); }catch(Exception e) { }finally { try { if(preparedStatement != null) { preparedStatement.close(); } if(connection != null) { connection.close(); } }catch(Exception e) { e.printStackTrace(); } } } public ArrayList<BDto> list() { // TODO Auto-generated method stub ArrayList<BDto> dtos = new ArrayList<BDto>(); Connection connection = null; PreparedStatement preparedStatement =null; ResultSet resultSet = null; try { connection = dataSource.getConnection(); // Query 문 작성 String query = "select bId,bName,bTitle,bContent,bDate,bHit,bGroup,bStep,bIndent from mvc_board order by bGroup desc,bStep asc"; preparedStatement = connection.prepareStatement(query); // resultset 으로 데이터를 가져온다 이제 가져온 데이터들을 분리시키는 상황이 필요 resultSet = preparedStatement.executeQuery(); // resultSet으로 레코드들을 모두담는다. while(resultSet.next()) { int bId = resultSet.getInt("bId"); String bName = resultSet.getString("bName"); String bTitle = resultSet.getString("bTitle"); String bContent = resultSet.getString("bContent"); Timestamp bDate = resultSet.getTimestamp("bDate"); int bGroup = resultSet.getInt("bGroup"); int bStep = resultSet.getInt("bStep"); int bHit = resultSet.getInt("bHit"); int bIndent = resultSet.getInt("bIndent"); //이제 가져온 데이터들을 Dto에 담는다. BDto dto = new BDto(bId,bName,bTitle,bContent,bDate,bHit,bGroup,bStep,bIndent); dtos.add(dto); } // 여기까지 DB설계는 완료되으며, 이제 List로 가서 처리할 jsp파일을 생성하여보자! } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); }finally { try { if(resultSet != null) { resultSet.close(); } if(preparedStatement != null) { preparedStatement.close(); } if(connection != null) { connection.close(); } }catch (Exception e) { // TODO: handle exception } } return dtos; } } cs
Dto.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 | package com.javalec.spring_pjt_board_command; import java.util.Map; import javax.servlet.http.HttpServletRequest; import org.springframework.ui.Model; import com.javalec.spring_pjt_board_dao.BDao; public class BWriteCommand implements BCommand { @Override public void execute(Model model) { // TODO Auto-generated method stub Map<String, Object> map = model.asMap(); HttpServletRequest request = (HttpServletRequest) map.get("request"); String bName = request.getParameter("bName"); String bTitle = request.getParameter("bTitle"); String bContent = request.getParameter("bContent"); BDao dao = new BDao(); dao.write(bName,bTitle,bContent); } } Command에서 Dao로 요청들어온값을 보내준다. (요청 데이터 처리) | cs |
DAO.java(DB스키마 설계)
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 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 | package com.javalec.spring_pjt_board_dao; import java.sql.Connection; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; import java.sql.Timestamp; import java.util.ArrayList; import javax.naming.Context; import javax.naming.InitialContext; import javax.naming.NamingException; import javax.sql.DataSource; import com.javalec.spring_pjt_board_dto.BDto; //database에 접근해서 작업 하는구 public class BDao { // datasoruce는 context에서 가져오는부분이다. DataSource dataSource; public BDao() { // TODO Auto-generated constructor stub try { Context context = new InitialContext(); dataSource = (DataSource) context.lookup("java:comp/env/jdbc/SpringDS"); } catch (NamingException e) { // TODO Auto-generated catch block e.printStackTrace(); } } //write 받아오는 부분 BWriteCommand에서 public void write(String bName, String bTitle, String bContent) { // TODO Auto-generated method stub Connection connection = null; PreparedStatement preparedStatement = null; int cnt=0; try { connection = dataSource.getConnection(); String query = "insert into mvc_board (bId, bName, bTitle, bContent, bHit, bGroup, bStep, bIndent) values ("+(cnt++)+"0, ?, ?, ?, 0,0,0,0)"; preparedStatement = connection.prepareStatement(query); preparedStatement.setString(1, bName); preparedStatement.setString(2, bTitle); preparedStatement.setString(3, bContent); int rn = preparedStatement.executeUpdate(); }catch(Exception e) { }finally { try { if(preparedStatement != null) { preparedStatement.close(); } if(connection != null) { connection.close(); } }catch(Exception e) { e.printStackTrace(); } } } public ArrayList<BDto> list() { // TODO Auto-generated method stub ArrayList<BDto> dtos = new ArrayList<BDto>(); Connection connection = null; PreparedStatement preparedStatement =null; ResultSet resultSet = null; try { connection = dataSource.getConnection(); // Query 문 작성 String query = "select bId,bName,bTitle,bContent,bDate,bHit,bGroup,bStep,bIndent from mvc_board order by bGroup desc,bStep asc"; preparedStatement = connection.prepareStatement(query); // resultset 으로 데이터를 가져온다 이제 가져온 데이터들을 분리시키는 상황이 필요 resultSet = preparedStatement.executeQuery(); // resultSet으로 레코드들을 모두담는다. while(resultSet.next()) { int bId = resultSet.getInt("bId"); String bName = resultSet.getString("bName"); String bTitle = resultSet.getString("bTitle"); String bContent = resultSet.getString("bContent"); Timestamp bDate = resultSet.getTimestamp("bDate"); int bGroup = resultSet.getInt("bGroup"); int bStep = resultSet.getInt("bStep"); int bHit = resultSet.getInt("bHit"); int bIndent = resultSet.getInt("bIndent"); //이제 가져온 데이터들을 Dto에 담는다. BDto dto = new BDto(bId,bName,bTitle,bContent,bDate,bHit,bGroup,bStep,bIndent); dtos.add(dto); } // 여기까지 DB설계는 완료되으며, 이제 List로 가서 처리할 jsp파일을 생성하여보자! } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); }finally { try { if(resultSet != null) { resultSet.close(); } if(preparedStatement != null) { preparedStatement.close(); } if(connection != null) { connection.close(); } }catch (Exception e) { // TODO: handle exception } } return dtos; } } | cs |
Dto.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 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 | package com.javalec.spring_pjt_board_dto; import java.sql.Timestamp; // Database의 데이터->객체로 바꿔주는 부분 public class BDto { int bId; String bName; String bTitle; String bContent; Timestamp bDate; int bHit; int bGroup; int bStep; int bIndent; // 파라미터 없는 Structure public BDto() { // TODO Auto-generated constructor stub } // 파라미터 있는 Structrue public BDto(int bId,String bName,String bTitle,String bContent,Timestamp bDate,int bHit,int bGroup, int bStep,int bIndent) { // TODO Auto-generated constructor stub this.bId = bId; this.bName = bName; this.bTitle = bTitle; this.bDate = bDate; this.bContent = bContent; this.bHit = bHit; this.bGroup = bGroup; this.bStep = bStep; this.bIndent = bIndent; } public int getbId() { return bId; } public void setbId(int bId) { this.bId = bId; } public String getbName() { return bName; } public void setbName(String bName) { this.bName = bName; } public String getbTitle() { return bTitle; } public void setbTitle(String bTitle) { this.bTitle = bTitle; } public String getbContent() { return bContent; } public void setbContent(String bContent) { this.bContent = bContent; } public Timestamp getbDate() { return bDate; } public void setbDate(Timestamp bDate) { this.bDate = bDate; } public int getbHit() { return bHit; } public void setbHit(int bHit) { this.bHit = bHit; } public int getbGroup() { return bGroup; } public void setbGroup(int bGroup) { this.bGroup = bGroup; } public int getbStep() { return bStep; } public void setbStep(int bStep) { this.bStep = bStep; } public int getbIndent() { return bIndent; } public void setbIndent(int bIndent) { this.bIndent = bIndent; } } | cs |
'Spring' 카테고리의 다른 글
[Spring] mysql DateTime Type Error (0) | 2018.03.14 |
---|---|
[Spring] Mysql 한글 깨짐 UTF-8 인코딩 해결하기 (2) | 2018.03.14 |
[Spring] Cannot load JDBC driver class 'com.mysql.jdbc.Driver / tomcat 404 error(spring - No mapping found for HTTP request with URI)해결하기 (0) | 2018.03.13 |
[Spring] MVC - 게시판 만들기 2(게시판 기본 설계) (0) | 2018.03.12 |
[Spring] Form data value validation (0) | 2018.03.05 |
최근에 올라온 글
최근에 달린 댓글
- Total
- Today
- Yesterday
링크
TAG
- Controller
- Spring
- programming
- db
- Android
- 노드
- BFS
- 프로그래밍
- 텐서플로우
- 복습
- C langauge
- TensorFlow
- MVC
- 리버싱
- C언어
- node.js
- 머신러닝
- 알고리즘
- 백준알고리즘
- node
- 코드엔진
- 안드로이드
- 스프링
- 초보자를 위한 C언어 300제
- Algorigm
- 백준
- 감자코딩
- 개발하는 관광이
- 학교
- 감자개발자
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
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 |
글 보관함