Back to the HTML

HTML name속성, value속성 => MVC 패턴 DB 올리기

Backcoder 2022. 7. 25. 13:08

< HTML 연결고리 정리 > (MVC패턴)

1. 게시판 글 목록, 게시글 내용 등을 db에서 '받아올때'
MVC 패턴을 사용한다.


( db에서 받아옴 => Model에 담음 => html뿌림 )

( ThymeLeaf문법 사용 )
model.addAttribute("boards", boardlist);

<tr th:each="board : ${boards}">
<td th:text="${board.id}"> </td>

2. 게시판 작성한 글을 DB에 '올릴때'
@PostMappingname=" " 속성을 사용한다.


( html에서 사용자입력=> name연결고리 => Post => DTO에 담음 => db에 저장 )

<form action="/writeboard" method="post">

<th>제목</th>
<td> <input type="text" name="title"> </td>
<td> <input type="text" name="cont"> </td>
<input type="submit" value="작성하기">

@PostMapping("writeboard")
public String saving(BoardDTO dto)
{ Board board = dto.toEntity(); boardDAO.saving(board);

(1) name 연결고리 걸어놓기
(2) form태그 post방식으로 PostMapping으로 submit 보내기
(3) PostMapping에서 DTO 매개변수로 받으면 name연결고리에 해당하는 전역변수로 값이 들어감


3. 게시판 수정하기 => 올리기 + 받아오기 (value) 둘다 사용

- value 로 기본 값을 MVC 패턴으로 가져오고
(여기선 게시판 내용이니까 input 안으로 가져오는 것이기 때문에, value 값으로 넣는다)

- 수정하고 완료된 내용은 name 연결고리, PostMapping으로 올리고

{{#editkey}}
<form action="/board/{id}/edit/" method="post" >

<input type="hidden" name="id" value="{{id}}" />


<label class="form-label">제목</label>
<input type="text" name="title" value="{{title}}" />

<label class="form-label">내용</label>
<textarea class="form-control" rows="5" name="contents">{{contents}}</textarea>

( textarea는 value 필요없이 그냥 내용칸에 넣어주면 된다. )

<button type="submit" class="btn btn-primary">수정</button>
</form>
{{/editkey}}