1. SampleModel.java 생성

- com.anse.web.model 패키지 생성 후 SampleModel.java 생성

 

2. SampleModel.java 소스 수정

-게시판 번호, 타이틀, 컨텐츠 등으로 테스트 할 예정

package com.anse.web.model;

import lombok.Builder;
import lombok.Data;

@Builder 
@Data
public class SampleModel {
	private int board_no;
	private String title;
	private String board_content;
}

 

 

3. SampleController.java 소스 수정 

package com.anse.web.controller;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.servlet.ModelAndView;

import com.anse.web.model.SampleModel;

@Controller
public class SampleController {
	@GetMapping("/sample")
	public ModelAndView goHome(HttpServletRequest request, ModelAndView mv) {
		mv.setViewName("sample/sample");
		
		List<SampleModel> list = new ArrayList<>();
		SampleModel sampleModel;
		
        // test1
		sampleModel = SampleModel.builder()
						.board_no(1)
						.title("제목 1")
						.board_content("내용 1").build();
		list.add(sampleModel);
        
        
        // test2
		sampleModel = SampleModel.builder()
						.board_no(2)
						.title("제목 2")
						.board_content("내용 2").build();
		list.add(sampleModel);
		
		mv.addObject("list", list);
		
		return mv;
	}
}

 

 

4. sample.html 소스 수정

- model 객체를 담은 list를 타임리프 문법으로 loop 돌려 사용

<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml"
	xmlns:th="http://www.thymeleaf.org"
	xmlns:layout="http://www.ultraq.net.nz/thymeleaf/layout"
<title>Sample</title>
	<body>
		<h2>This is Sample</h2>
		<table border="1">
		<tr>
			<th>boardId</th>
			<th>title</th>
			<th>boardContent</th>
		</tr>
		<th:block th:each="info : ${list}">
			<tr>
				<td th:text="${info.board_no}"></td>
				<td th:text="${info.title}"></td>
				<td th:text="${info.board_content}"></td>
			</tr>
		</th:block>
	</table>
	</body>
</html>

 

 

5. 화면 확인

 

+ Recent posts