0508 수업내용
POST = 자원을 보내는 것
GET = 자원을 받는 것
<form th:action="@{|/answer/create/${question.id}|}" method="post">
// 폼안에 뭘할지 (포스트) 를 쓰고 타임리프/링크 설정 \
// 폼태그는 데이터를 감싸주는 태그
<textarea name="content" id="content" rows="15"></textarea>
//텍스트 에어리어 == 텍스트를 쓸 수 있는 칸 생성
<input type="submit" value="답변등록">
// 인풋 텍스트박스
</form>
@PostMapping("/create/{id}")
public String createAnswer(Model model, @PathVariable("id") Integer id, @RequestParam String content) {
Question question = this.questionService.getQuestion(id);
//답변을 저장할 질문을 가져옴.
this.answerService.create(question, content);
// 받은 값들을 서비스로 넘겨 서비스안의 생성메서드로 답변을 생성한다.
return String.format("redirect:/question/detail/%s", id);
// 답변을 만든 후 새로고침하여 확인하기 위해 작성함
}
@RequiredArgsConstructor
// 인스턴스변수가 하나가 있으면 자동으로 생성자함수 처리
// 예: 생성자에서 데이터를 받을경우 자동으로 this. 생성
@Service
public class AnswerService {
private final AnswerRepository answerRepository;
//앤서 리포지터리를 받음.
public void create(Question question, String content) {
//앤서 생성하는 메서드
Answer answer = new Answer();
// 앤서타입 가져와서 객체선언
answer.setContent(content);
// 컨텐츠에는 작성한 컨텐츠
answer.setCreateDate(LocalDateTime.now());
// 생성일은 자동으로 현재시간 저장
answer.setQuestion(question);
// 질문엔 해당메서드로 받은 질문 넣어줌.
this.answerRepository.save(answer);
// 값을 넣은 답변을 리포지터리를 이용하여 DB에 저장
}
}
스태틱 (정적) 디렉터리
/스타일시트 파일은 스프링부트의 스태틱 디렉터리에 저장해야 한다. /
부트스트랩(Bootstrap)은 디자이너의 도움 없이도 개발자 혼자서
상당히 괜찮은 수준의 웹 페이지를 만들수 있게 도와주는 프레임워크
//디비젼 ( 구역 )
div{
display:block;
// 가로가 끝까지 있음 (선언안해도 기본)
//inline-block ( 가로가 글씨만큼만
background: red;
//백그라운드 컬러 빨강설정
height:300px;
border:4px solid black;
// 테두리 : 크기 타입 색상
padding :10px;
// 글씨와 테두리 사이 여백추가(보더 안쪽)
margin:10px;
// 테두리와 페이지 사이 여백추가 (보더 밖)
}
(인라인블럭으로 기본 )