포트폴리오/SPRINGBOOT 옛날

2-10 : 질문 리스트 > 질문 상세

서버관리자 페페 2022. 10. 1. 18:30

단 하나의 맥락

: 템플릿 간의 이동

 

 


Semiflow Bundle

: question_list.html에 상세 링크 추가하기

: QuestionController에 question/detail URL Post매핑

: question_detail.html 및 임시 dummydata작성

: 서비스에서 Question 값 조회해서

: Controller 에서 model에 값을 담아 template으로 넘기기

: template에서 받아서 사용

: prefix

 


자각

 

질문 리스트 > 질문 상세 로 넘어가기 위해

> question_list.html에서

- 단순 텍스트 출력인 <td:text="${question.subject}"></td> 를

- <td></td> 사이 <a th:href="@{|/question/detail/${question.id}|}" th:text="${question.subject}"></a> 로 링크를 입힌다

 

-

 

404 처리, 질문 상세 URL 매핑을 위해

> QuestionController에서

- question_detail을 return하는 public String detail 메소드를 구현한다

- @RequestionMapping(value = "question/detail/{id}") 를 적용한다

- signal로는 (Model model, @PathVariable("id") Integer id) 가 들어오는데

- 경로값이 다중인 해당 어노테이션은 "import org.springframework.web.bind.annotation.PathVariable;" P/I 으로 사용 가능하다

 

-

 

 

500 처리, 화면에 띄울 템플릿을 위해

> question_detail.html을 신규 작성

- 더미 데이터로 <h1>제목</h1>, <div>내용</div>을 넣는다

 

 

-

 

실제 Question 내용을 상세페이지에 띄우기 위해 QuestionController에서 조회에 사용할 서비스를 만든다

> QuestionService에서

- public Question getQuestion{} 메소드 생성

- signal은 (Integer id) 로 받는다

- A1 : Optional<Question> question = this.questionRepository.findById(id);

- A2 : if (question.isPresent()) { return question.get(); }

- A3 : else { throw new DataNotFoundException("question not found") }

 

(... 생략 ...)
import java.util.Optional;
import com.mysite.sbb.DataNotFoundException;
(... 생략 ...)
public class QuestionService {

    (... 생략 ...)

    public Question getQuestion(Integer id) {  
        Optional<Question> question = this.questionRepository.findById(id);
        if (question.isPresent()) {
            return question.get();
        } else {
            throw new DataNotFoundException("question not found");
        }
    }
}

- DataNotFoundException은 "import com.mysite.sbb.DataNotFoundException" P/I로 사용 가능하며, 직접 만들어야 한다

 

 

package com.mysite.sbb;

import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.ResponseStatus;

@ResponseStatus(value = HttpStatus.NOT_FOUND, reason = "entity not found")
public class DataNotFoundException extends RuntimeException {
    private static final long serialVersionUID = 1L;
    public DataNotFoundException(String message) {
        super(message);
    }
}

- DataNotFoundException은 RuntimeException을 상속하는 class이다

- RuntimeException은 따로 P/I가 필요 없다

- 해당 class 위에 @ResponseStatus(value = HttpStatus.NOT_FOUND, reason = "entity not found") 어노테이션을 작성한다

 

 

-

 

 

만들어진 서비스를 사용하기 위해

> QuestionController에서

- A1 : Question question = this.questionService.getQuestion(id);

- A2 : model.addAttribute("question", question); 으로 question 값을 template에 전달한다

(... 생략 ...)
public class QuestionController {

    (... 생략 ...)

    @RequestMapping(value = "/question/detail/{id}")
    public String detail(Model model, @PathVariable("id") Integer id) {
        Question question = this.questionService.getQuestion(id);
        model.addAttribute("question", question);
        return "question_detail";
    }
}

 

 

-

 

전달받은 자바 객체값은

> question_detail.html에

- <h1 th:text="${question.subject}"></h1>

- <div th:text="${question.content}"></div> 로 사용한다

 

 

-

 

- @RequestMapping 을 메소드에만 쓰는 게 아닌, class 위에 공통 부분을 기재함으로써(prefix), 각 method는 상세 경로만 기재한다

 

'포트폴리오 > SPRINGBOOT 옛날' 카테고리의 다른 글

2-12 : Static Directory and CSS  (1) 2022.10.02
2-11 : Answer Create  (3) 2022.10.01
2-09 : Service  (4) 2022.10.01
2-08 : redirecting to ROOT URL  (1) 2022.10.01
2-07 : template  (3) 2022.09.30