강의

멘토링

커뮤니티

인프런 커뮤니티 질문&답변

임여명님의 프로필 이미지
임여명

작성한 질문수

스프링부트 db에 저장된 데이터 html에 리스트로 뿌리기

작성

·

880

·

수정됨

0

 

 

1.jpg

↑↑↑↑↑↑↑↑↑↑↑↑↑mysql 에 들어있는 데이터 5개를

2.jpg위처럼 db안의 데이터를 html화면에 리스트로 뿌리려고 합니다

 

컨트롤러

--------------------------------------------------------------------------------------

import org.springframework.stereotype.Controller;

import org.springframework.ui.Model;

import org.springframework.web.bind.annotation.GetMapping;

import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RequestParam;

import lombok.RequiredArgsConstructor;

@RequiredArgsConstructor

@Controller

public class reviewController {

private final reviewService reviewService;

@RequestMapping("/AAA2")

public String review() {

return "AAA2";

}

@GetMapping("/form-action02")

public String list( @RequestParam String reviewStar,

@RequestParam String id,

@RequestParam String reviewContents,

Model model) {

List<review> reviewList = this.reviewService.getList();

model.addAttribute("reviewList",reviewList);

return "AAA2";

}

}

--------------------------------------------------------------------------------------

레파지토리

--------------------------------------------------------------------------------------

import java.util.List;

import org.springframework.data.jpa.repository.JpaRepository;

import org.springframework.data.jpa.repository.Query;

import org.springframework.stereotype.Repository;

@Repository

public interface reviewRepository extends JpaRepository<review, String>{

@Query(value = "SELECT * FROM REVIEW_LIST", nativeQuery = true)

List<review> findAll();

}

--------------------------------------------------------------------------------------

서비스

--------------------------------------------------------------------------------------

import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.stereotype.Service;

import lombok.RequiredArgsConstructor;

@RequiredArgsConstructor

@Service

public class reviewService {

@Autowired

    private final reviewRepository reviewRepository;

public List<review> getList() {

return this.reviewRepository.findAll();

}

}

--------------------------------------------------------------------------------------

엔티티

--------------------------------------------------------------------------------------

import java.time.LocalDateTime;

import jakarta.persistence.Column;

import jakarta.persistence.Entity;

import jakarta.persistence.Id;

import jakarta.persistence.Table;

import lombok.Getter;

@Entity

@Getter

@Table(name = "REVIEW_LIST")

public class review {

@Id

@Column

private int sortnum;

@Column

private String id;

@Column

private String reviewContents;

@Column

private String reviewStar;

@Column

private LocalDateTime createDate;

}

--------------------------------------------------------------------------------------

html

--------------------------------------------------------------------------------------

<form action="form-action02" name="myform" id="myform" method="get">

<fieldset>

<span class="text-bold">평점을 선택해주세요</span>

<input type="radio" name="reviewStar" value="★★★★★" id="rate1"><label

for="rate1">★</label>

<input type="radio" name="reviewStar" value="★★★★☆" id="rate2"><label

for="rate2">★</label>

<input type="radio" name="reviewStar" value="★★★☆☆" id="rate3"><label

for="rate3">★</label>

<input type="radio" name="reviewStar" value="★★☆☆☆" id="rate4"><label

for="rate4">★</label>

<input type="radio" name="reviewStar" value="★☆☆☆☆" id="rate5"><label

for="rate5">★</label>

</fieldset>

<div>

    <input type="text" name="id" placeholder="닉네임 입력"><br><br>

<textarea class="col-auto form-control"  type="text" name="reviewContents" id="reviewContents"

  value="<?php echo $reviewContents?>" placeholder="리뷰 작성란"></textarea>

</div>

    <div class="mainPage02"><br><br>

      <input type="submit" value = "작성">   

    </div>  

</form>

<table class="courseTable" style="border-left: none; border-right: none;">

    <thead>

        <tr>

            <th style="border-left: none;">평점</th>

            <th>닉네임</th>

            <th>내용</th>

            <th style="border-right: none;">작성일자</th>

        </tr>

    </thead>

    <tbody>

        <tr th:each="list : ${reviewList}">

            <td style="border-left: none;" th:text="${list.reviewStar}"></td>

            <td th:text="${list.id}"></td>

            <td th:text="${list.reviewContents}"></td>

            <td style="border-right: none;"> <td th:text="${list.createDate}"></td>

        </tr>

    </tbody>

</table>

--------------------------------------------------------------------------------------

 

제가 짠 코드인데 아무리해도 리스트가 출력이 안됩니다..

도와주십시오

 

답변

답변을 기다리고 있는 질문이에요
첫번째 답변을 남겨보세요!
임여명님의 프로필 이미지
임여명

작성한 질문수

질문하기