1. 컨트롤 부분

	
	@Autowired
	ResourceLoader resourceLoader;
	
    // 파일의 위치
	@Value("${file.path}")
	private String file_Path;

    @RequestMapping("/fileDownload")
	public ResponseEntity<InputStreamResource> download(
			@RequestParam(defaultValue = "test") String fName
			) throws IOException {
		
		System.out.println("fName = " + fName);
		// 파일 경로 
		String path = file_Path + File.separator + fName;
		// 파일 존재 유무 
		boolean fExist = _FileUtil.fileExistInfo(path);
		if(fExist) {
	        File file = new File(path);
	        String fileName = file.getName();
	        // 파일 확장자 
	        String ext = fileName.substring(fileName.lastIndexOf(".") + 1);
	        HttpHeaders header = new HttpHeaders();
	        Path fPath = Paths.get(file.getAbsolutePath());
	        
	        header.add(HttpHeaders.CONTENT_DISPOSITION, "attachment; filename="+fileName);
	        header.add("Cache-Control", "no-cache, no-store, must-revalidate");
	        header.add("Pragma", "no-cache");
	        header.add("Expires", "0");
	        
	        // 대용량일 경우 resource3을 사용해야함 
//	        ByteArrayResource resource = new ByteArrayResource(Files.readAllBytes(fPath ));
//	        Resource resouce2 = resourceLoader.getResource(path); 
	        InputStreamResource resource3 = new InputStreamResource(new FileInputStream(file));
	        
	        return ResponseEntity.ok()
	                .headers(header)
	                .contentLength(file.length())
	                .contentType(MediaType.parseMediaType("application/octet-stream"))
	                .body(resource3);
		}
		return null;
	}

 

 

2. 테스트 하기 

 # 1.zip 파일 다운 받기


 

블로그 이미지

미나미나미

,

# [Spring MVC] thymeleaf 적용하기

1. pom.xml 

<dependency>
	<groupId>org.thymeleaf</groupId>
	<artifactId>thymeleaf-spring5</artifactId>
	<version>3.0.11.RELEASE</version>
</dependency>

2. servlet-context.xml 설정 

 # 만약 thymelead 한글 깨진 다면,  아래의 UTF-8 들어간 부분을 확인하시길 바랍니다.

<beans:property name="characterEncoding" value="UTF-8" />

<beans:property name="fileEncoding" value="UTF-8" />

	<!-- 기존 jsp로 화면 설계 부분 -->
	<!-- 
	<beans:bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> 
		<beans:property name="prefix" value="/WEB-INF/views/" /> <beans:property 
		name="suffix" value=".jsp" /> </beans:bean> 
	-->
	
	<!-- thymeleaf 설정 부분  -->
	<beans:bean id="templateResolver"
		class="org.thymeleaf.templateresolver.ServletContextTemplateResolver">
		<beans:property name="prefix" value="/WEB-INF/views/" />
		<beans:property name="suffix" value=".html" />
		<beans:property name="templateMode" value="HTML5" />
		<beans:property name="cacheable" value="false" />
		<beans:property name="characterEncoding" value="UTF-8" />
	</beans:bean>

	<beans:bean id="templateEngine"
		class="org.thymeleaf.spring5.SpringTemplateEngine">
		<beans:property name="templateResolver"
			ref="templateResolver" />
	</beans:bean>

	<beans:bean
		class="org.thymeleaf.spring5.view.ThymeleafViewResolver">
		<beans:property name="templateEngine"
			ref="templateEngine" />
		<beans:property name="characterEncoding" value="UTF-8" />
	</beans:bean>

	<beans:bean
		class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
		<beans:property name="location"
			value="/resources/config/application.config" />
		<beans:property name="fileEncoding" value="UTF-8" />
	</beans:bean>
	<!-- thymeleaf 설정 부분  -->

3. 페이지 

<!DOCTYPE html>

<!-- thymelead 불러오기 -->
<html lang="ko" xmlns:th="http://www.thymeleaf.org">

<head>
    <meta charset="UTF-8">
	<meta http-equiv="Content-Type" content="text/html; charset=EUC-KR">
	<meta name="viewport" content="width=device-width, initial-scale=1.0">
	<title>파일업로드</title>
</head>

<body>
	<div th:if="${msg!=null}" style="">
		<h1>파일 업로드 결과</h1>
		<h2 th:text="${msg}">${msg}</h2>
	</div>

	<h1>단일 파일 보내기</h1>
	<form method="post" enctype="multipart/form-data" action="" th:action="@{fileUpload}">
		<label>file : </label> <input type="file" name="file"> <label>name
			: </label> <input type="text" name="name"> <input type="submit" value="upload">
	</form>

	<h2>여러개 파일 보내기</h2>
	<form method="post" enctype='multipart/form-data' action="" th:action="@{multiFileUpload}">
		<label>files : </label> <input type="file" name="files" multiple>
		<label>name : </label> <input type="text" name="name"> <input type="submit" value="upload">
	</form>

    <div id="root">
        
    </div>
    <!-- <script src="../resources/dist/app.js"></script> -->
    <script src="../dist/app.js"></script>
</body>

</html>
블로그 이미지

미나미나미

,

1. 파일 업로드  Rest Controller 만들기 

import java.io.File;
import java.io.IOException;
import java.nio.file.FileAlreadyExistsException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.StandardCopyOption;
import java.text.DateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Locale;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.google.gson.Gson;

@RestController
@RequestMapping("restfiles")
public class RestFilesController {

	@Value("${file.path}")
	private String file_Path;

	private static final Logger logger = LoggerFactory.getLogger(RestFilesController.class);
	
	// 단일 파일 업로드 
	@PostMapping("/fileUpload")
	public String fileUpload(@RequestParam(required = false) MultipartFile file,
			@RequestParam(defaultValue = "test") String name) {
		if (file != null) {
			System.out.println("file.getSize() = "+ file.getSize());
			
			String msg = file.getOriginalFilename() + "is upload";
			
			// File.seperator 는 OS종속적이다.
			// Spring에서 제공하는 cleanPath()를 통해서 ../ 내부 점들에 대해서 사용을 억제한다
			Path copyOfLocation = Paths
					.get(file_Path + File.separator + StringUtils.cleanPath(file.getOriginalFilename()));
			try {
				// inputStream을 가져와서
				// copyOfLocation (저장위치)로 파일을 쓴다.
				// copy의 옵션은 기존에 존재하면 REPLACE(대체한다), 오버라이딩 한다
				Files.copy(file.getInputStream(), copyOfLocation, StandardCopyOption.REPLACE_EXISTING);
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		return "upload is complete";
	}
	
	
	// 멀티 파일 업로드 
	@PostMapping("/multiFileUpload")
	public String multiFileUpload(
			@RequestParam(required = false) MultipartFile[] files,
			@RequestParam(defaultValue = "test") String name) {
		
		HashMap<String, String> result = new HashMap<String, String>();
		Gson gson = new Gson();
		
		if (files != null) {
			for(MultipartFile file : files) {
				String msg = file.getOriginalFilename() + "is upload";
				System.out.println("name = " + name);
				System.out.println("msg = " + msg);

				// File.seperator 는 OS종속적이다.
				// Spring에서 제공하는 cleanPath()를 통해서 ../ 내부 점들에 대해서 사용을 억제한다
				Path copyOfLocation = Paths
						.get(file_Path + File.separator + StringUtils.cleanPath(file.getOriginalFilename()));
				try {
					// inputStream을 가져와서
					// copyOfLocation (저장위치)로 파일을 쓴다.
					// copy의 옵션은 기존에 존재하면 REPLACE(대체한다), 오버라이딩 한다
					Files.copy(file.getInputStream(), copyOfLocation, StandardCopyOption.REPLACE_EXISTING);
					result.put(file.getOriginalFilename(), "completed");
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}
		return gson.toJson(result);
	}
}

 


 

 

2. 파일 업로드 Postman으로 테스트 하기

    # 단일 파일 업로드 

 

 

     #  멀티 파일 업로드 

블로그 이미지

미나미나미

,

1. 파일 업로드 페이지 (# fileForm.html)

<%@ page language="java" contentType="text/html; charset=utf-8" pageEncoding="utf-8"%>
<!DOCTYPE html>

<html lang="ko" xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<meta http-equiv="Content-Type" content="text/html; charset=EUC-KR">

<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>파일업로드</title>
</head>

<body>
	
	<div th:if="${msg!=null}" style="${msg!=null ? 'display:block' : 'display:none'}" >
		<h1>파일 업로드 결과</h1>
        <h2 th:text="${msg}">${msg}</h2>
    </div>
    
    <h1>단일 파일 보내기</h1>
    <form method="post" enctype="multipart/form-data" action="./fileUpload" th:action="@{/fileUpload}">
        <label>file : </label>
        <input type="file" name="file">
        <label>name : </label>
        <input type="text" name="name">
        <input type="submit" value="upload">
    </form>
    
    <h2>여러개 파일 보내기</h2>
    <form method="post"  enctype='multipart/form-data' action="./multiFileUpload" th:action="@{/multiFileUpload}">
        <label>files : </label>
        <input type="file" name="files" multiple>
        <label>name : </label>
        <input type="text" name="name">
        <input type="submit" value="upload">
    </form>
    
</body>
</html>

 

 

2.  파일 업로드 페이지 결과

   # 단일 파일 업로드 

 

 

   # 멀티 파일 업로드 


 

블로그 이미지

미나미나미

,

1. 파일 저장 위치 지정  (# application.config)

 

## FilePath 파일 저장 위치 
file.path=C:\\TestDir

 

 

2. 파일 컨트롤러 

import java.io.File;
import java.io.IOException;
import java.nio.file.FileAlreadyExistsException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.StandardCopyOption;
import java.text.DateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Locale;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.google.gson.Gson;

@Controller
@RequestMapping("files")
public class FilesController {
	
	// 파일 저장할 위치
	@Value("${file.path}") 
	private String file_Path;

	private static final Logger logger = LoggerFactory.getLogger(FilesController.class);

	// 파일 폼 매핑 
	@GetMapping("/fileForm")
	public String fileForm(Locale locale, Model model) {
		// fileForm.html을 불러옴 
		return "fileForm";
	}
	
	// 파일 등록 매핑 
    @PostMapping("/fileUpload")
    public String fileUpload(
    		@RequestParam(required=false) MultipartFile file, 
    		@RequestParam(defaultValue = "test")String name,
    		RedirectAttributes attributes){
    	
    	String msg = file.getOriginalFilename() + "is upload";
        attributes.addFlashAttribute("msg" , msg);
        
        // File.seperator 는 OS종속적이다.
        // Spring에서 제공하는 cleanPath()를 통해서 ../ 내부 점들에 대해서 사용을 억제한다
        Path copyOfLocation = Paths.get(file_Path + File.separator + StringUtils.cleanPath(file.getOriginalFilename()));
        try {
            // inputStream을 가져와서
            // copyOfLocation (저장위치)로 파일을 쓴다.
            // copy의 옵션은 기존에 존재하면 REPLACE(대체한다), 오버라이딩 한다
            Files.copy(file.getInputStream(), copyOfLocation, StandardCopyOption.REPLACE_EXISTING);
        } catch (IOException e) {
            e.printStackTrace();
        }

        return "redirect:/files/fileForm";
    }
    
    
    // 여러 개의 파일 업로드 
    @PostMapping("/multiFileUpload")
	public String multiFileUpload(
			@RequestParam(required = false) MultipartFile[] files,
			@RequestParam(defaultValue = "test") String name,
    		RedirectAttributes attributes){
		
    	// 결과값 리턴 
    	HashMap<String, String> result = new HashMap<String, String>();
		Gson gson = new Gson();
		
		// 파일 있을 경우만 
		if (files != null) {
			for(MultipartFile file : files) {
				Path copyOfLocation = Paths
						.get(file_Path + File.separator + StringUtils.cleanPath(file.getOriginalFilename()));
				try {
					Files.copy(file.getInputStream(), copyOfLocation, StandardCopyOption.REPLACE_EXISTING);
					result.put(file.getOriginalFilename(), "completed");
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}
		// 파일 업로드 결과
		attributes.addFlashAttribute("msg" , gson.toJson(result));
		return "redirect:/files/fileForm";
	}
}

 

블로그 이미지

미나미나미

,

#PGR 패턴 : en.wikipedia.org/wiki/Post/Redirect/Get

 

Post/Redirect/Get - Wikipedia

Diagram of a double POST problem encountered in user agents. Diagram of the double POST problem above being solved by PRG. Post/Redirect/Get (PRG) is a web development design pattern that lets the page shown after a form submission be reloaded, shared, or

en.wikipedia.org

Post / Redirect / Get ( PRG )은 양식 제출 후 표시되는 페이지를 다른 시간에 양식을 제출하는 것과 같은 악영향없이 다시로드, 공유 또는 북마크 할 수 있는 웹 개발 디자인 패턴 입니다 .

HTTP POST 요청을 통해 웹 양식이 서버에 제출 될 때 서버 응답을 새로 고치려고하면 원래 POST의 콘텐츠가 다시 제출되어 중복 웹 구매 와 같은 원치 않는 결과가 발생할 수 있습니다 . 

이 문제를 방지하기 위해 많은 웹 개발자 는 웹 페이지를 직접 반환하는 대신 PRG 패턴 [2]을 사용합니다 . POST는 리디렉션을 반환합니다. HTTP 1.1 사양은 HTTP 303 ( "기타보기") 응답 코드를 도입 하여이 상황에서 브라우저가 초기 POST 요청을 다시 제출하지 않고도 서버 응답을 안전하게 새로 고칠 수 있도록합니다.

PRG 패턴은 중복 양식 제출의 모든 시나리오를 다룰 수는 없습니다. 예를 들어, 서버 지연으로 인해 초기 제출이 완료되기 전에 웹 사용자가 새로 고치는 경우 특정 사용자 에이전트에서 중복 POST가 발생합니다.


# controller 부분 

package com.example.demo.junitFormTest;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.validation.ObjectError;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

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

@Controller
@RequestMapping("junitForm")
public class FormController {
    List<Event> eventList = new ArrayList<>();

    /**
     * 이벤트 등록 화면 컨트롤러
     * @param model
     * @return
     */
    @GetMapping("/formEvent")
    public String formEvent(Model model) {
        // formEvent 화면에 Event 그릇 내려 주기
        model.addAttribute("event", new Event());
        // 이벤트 등록 페이지
        return "/junit/FormTest/formEvent";
    }

    /***
     * 이벤트 정보 확인
     * @param event
     * @param bindingResult
     * @return
     */
//    @PostMapping("/regEvent")
//    @ResponseBody
//    public Event regEvent(
//            @ModelAttribute Event event,
//            // 값을 바인딩 할 수 없는 경우네는 BindingResult 4000 에러
//            // 이벤트 변수 타입이 맞지 않은 경우 null 처리
//            BindingResult bindingResult) {
//        if(bindingResult.hasErrors()){
//            // binding 에러가 된 경우, 에러 종류 찍기
//            for (ObjectError allError : bindingResult.getAllErrors()) {
//                System.out.println(allError.toString());
//            }
//        }
//        return event;
//    }

    @PostMapping("/regEvent")
    public String regEvent(
            @Validated  @ModelAttribute Event event,
            BindingResult bindingResult) {
        if(bindingResult.hasErrors()){
            // binding 에러가 된 경우, 에러 종류 찍기
            for (ObjectError allError : bindingResult.getAllErrors()) {
                System.out.println(allError.toString());
            }
            // 에러가 발생했음으로 form으로 되돌아가기 만들기
            return "/junit/FormTest/formEvent";
        }
        eventList.add(event);
        // 에러가 발생하지 않은 경우 리다이렉트로 통한 getmapping 처리
        return "redirect:/junitForm/events/list";
    }

    @GetMapping("/events/list")
    public String getEvents(Model model){
        // getmapping으로 리스트가 처리된 페이지는 form 이벤트가 재발생하지 않는다.
        model.addAttribute(eventList);
        // 이벤트 리스트 보여줄 페이지 리턴
        return "/junit/FormTest/eventList";
    }
}

# Event.java

package com.example.demo.junitFormTest;


        import javax.validation.constraints.Min;
        import javax.validation.constraints.NotBlank;

public class Event {

    @NotBlank
    private String eventName;

    @NotBlank
    private String eventDescription;

    @Min(0)
    private Integer eventCount;

    public Integer getEventCount() {
        return eventCount;
    }

    public void setEventCount(Integer eventCount) {
        this.eventCount = eventCount;
    }

    public String getEventName() {
        return eventName;
    }

    public void setEventName(String eventName) {
        this.eventName = eventName;
    }

    public String getEventDescription() {
        return eventDescription;
    }

    public void setEventDescription(String eventDescription) {
        this.eventDescription = eventDescription;
    }
}
/*
*  validation 사용하기 위해서는 pom.xml 추가 요소
<dependency>
    <groupId>org.hibernate.validator</groupId>
    <artifactId>hibernate-validator</artifactId>
    <version>6.0.7.Final</version>
</dependency>
* */

# /junit/FormTest/formEvent -  formEvent.html

<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title>New Events</title>
</head>
<body>
<form action="#" th:action="@{/junitForm/regEvent}" method="post" th:object="${event}">
    <p th:if="${#fields.hasErrors('eventName')}" th:errors="*{eventName}">eventName Error</p>
    <p th:if="${#fields.hasErrors('eventDescription')}" th:errors="*{eventDescription}">eventDescription Error</p>
    <p th:if="${#fields.hasErrors('eventCount')}" th:errors="*{eventCount}">eventCount Error</p>

    <label for="eventName">eventName</label>
    <input type="text" id="eventName" th:title="eventName" th:field="*{eventName}">
    <br>
    <label for="eventDescription">eventDescription</label>
    <input type="text" id="eventDescription" th:title="eventDescription" th:field="*{eventDescription}">
    <br>
    <label for="eventCount">eventCount</label>
    <input type="text" id="eventCount" th:title="eventCount" th:field="*{eventCount}">
    <br>
    <input type="submit" value="Create">
</form>
</body>
</html>

# /junitForm/events/list -  eventList.html

<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title>Event List</title>
</head>
<body>
<a th:href="@{/junitForm/formEvent}">Create New Event</a>
<div th:unless="${#lists.isEmpty(eventList)}">
    <ul th:each="event:${eventList}">
        <p th:text="${event.eventName}">eventName</p>
        <p th:text="${event.eventDescription}">eventDescription</p>
        <p th:text="${event.eventCount}">eventCount</p>
    </ul>
</div>
</body>
</html>

 

블로그 이미지

미나미나미

,

[SpringMVC] 커스텀 컨트롤러 어노테이션(Annotation) 만들기 

 


1. 어노테이션 생성  

package com.example.demo.annotation;

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

import java.lang.annotation.*;

/**
 * CustomAnnotation.java
 * /custom Controller 생성
 * Runtime 까지 유지
 */
@Documented
// 문서에 그 애노테이션에 대한 정보를 표기할지 결정
@Target({ElementType.METHOD})
// 어디에 사용할 수 있는지를 결정
@Retention(RetentionPolicy.RUNTIME)
//  언제까지 유지할 것인가
//  Source : 소스 코드까지만 유지, 즉 컴파일 하면 해당 애노테이션 정보는 사라진다는 이야기
//  Class : 컴파일 한 .class 파일에도 유지. 즉 런타임 시, 클래스를 메모리로 읽어오면 해당 정보는 사라진다.
//  Runtime : 클래스 메모리에 읽어왔을 떄 까지 유지! 코드에서 이 정보를 바탕으로 특정 로직을 실행 할 수 있다.
@RequestMapping(method = RequestMethod.GET , value = "/custom")
//  커스텀한 애노테이션을 만들 수 있다.
public @interface CustomAnnotation {
}

 

2. 어노테이션 사용한 컨트롤러 생성 

package com.example.demo.annotation;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * CustomController.java
 */
@Controller
@RequestMapping("/testAnnotation")
public class CustomController {

    /**
     * /testAnnotation/custom 으로 접근 가능
     * @return
     */
    @CustomAnnotation
    @ResponseBody
    public String custom(){
        return "custom";
    }
}

 

3. 컨트롤러 테스트 코드 작성 

@WebMvcTest
@RunWith(SpringRunner.class)
class CustomControllerTest {

    @Autowired
    MockMvc mockMvc;

    @Test
    void custom() throws Exception{
        mockMvc.perform(get("/testAnnotation/custom"))
                .andDo(print())
                .andExpect(status().isOk())
        ;
    }
}

 

4. 결과화면 

 

블로그 이미지

미나미나미

,

[Spring] Spring에서 Thymeleaf 테이블 만들기 (index 사용)

- Spring MVC, boot 둘 동일하게 작동 가능(thymeleaf 설정의 차이는 있음)

 

 

 

1. Event.java : 이벤트 내용 정의

package com.example.demo;

import java.time.LocalDateTime;

public class Event {
    private String name;
    private int limitOfEnrollment;
    private LocalDateTime startDateTime;
    private LocalDateTime endDateTime;

    public static Event.EventBuilder builder() {
        return new Event.EventBuilder();
    }

    public String getName() {
        return this.name;
    }

    public int getLimitOfEnrollment() {
        return this.limitOfEnrollment;
    }

    public LocalDateTime getStartDateTime() {
        return this.startDateTime;
    }

    public LocalDateTime getEndDateTime() {
        return this.endDateTime;
    }

    public void setName(final String name) {
        this.name = name;
    }

    public void setLimitOfEnrollment(final int limitOfEnrollment) {
        this.limitOfEnrollment = limitOfEnrollment;
    }

    public void setStartDateTime(final LocalDateTime startDateTime) {
        this.startDateTime = startDateTime;
    }

    public void setEndDateTime(final LocalDateTime endDateTime) {
        this.endDateTime = endDateTime;
    }

    public Event() {
    }

    public Event(final String name, final int limitOfEnrollment, final LocalDateTime startDateTime, final LocalDateTime endDateTime) {
        this.name = name;
        this.limitOfEnrollment = limitOfEnrollment;
        this.startDateTime = startDateTime;
        this.endDateTime = endDateTime;
    }

    public static class EventBuilder {
        private String name;
        private int limitOfEnrollment;
        private LocalDateTime startDateTime;
        private LocalDateTime endDateTime;

        EventBuilder() {
        }

        public Event.EventBuilder name(final String name) {
            this.name = name;
            return this;
        }

        public Event.EventBuilder limitOfEnrollment(final int limitOfEnrollment) {
            this.limitOfEnrollment = limitOfEnrollment;
            return this;
        }

        public Event.EventBuilder startDateTime(final LocalDateTime startDateTime) {
            this.startDateTime = startDateTime;
            return this;
        }

        public Event.EventBuilder endDateTime(final LocalDateTime endDateTime) {
            this.endDateTime = endDateTime;
            return this;
        }

        public Event build() {
            return new Event(this.name, this.limitOfEnrollment, this.startDateTime, this.endDateTime);
        }

        public String toString() {
            return "Event.EventBuilder(name=" + this.name + ", limitOfEnrollment=" + this.limitOfEnrollment + ", startDateTime=" + this.startDateTime + ", endDateTime=" + this.endDateTime + ")";
        }
    }
}

2. EventService.java : 이벤트 객체 만들기

package com.example.demo;

import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

@Service
public class EventService {
    public List<Event> getEvents(){
        Event event = Event.builder()
                    .name("Spring")
                    .limitOfEnrollment(5)
                    .startDateTime(LocalDateTime.of(2019,1,10,10,10))
                    .endDateTime(LocalDateTime.of(2019,1,10,13,10,10))
                    .build();

        Event event2 = Event.builder()
            .name("Spring")
            .limitOfEnrollment(5)
            .startDateTime(LocalDateTime.of(2019,1,17,10,10))
            .endDateTime(LocalDateTime.of(2019,1,17,13,10,10))
            .build();

        return List.of(event,event);
    }
}

3. EventController.java : 이벤트 객체 페이지 전달

package com.example.demo;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;

@Controller
public class EventController {

    @Autowired
    EventService eventService;

    //@RequestMapping(value = "/events" ,method = RequestMethod.GET)
    @GetMapping("events")
    public String events(Model model){
        model.addAttribute("events" ,eventService.getEvents());
        return "events";
    }

}

 

 

4. Event.html : 이벤트 내용 표현

<!DOCTYPE html>
<html lang="en" xmlns:th="http://thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <style>
        table {
            width: 100%;
            border: 1px solid #444444;
            text-align: center;
        }
        th, td {
            border: 1px solid #444444;
        }
    </style>
</head>
<body>
    <h1>이벤트 목록</h1>
    <table>
        <tr>
            <th>#</th>
            <th>이름</th>
            <th>참가 인원</th>
            <th>시작</th>
            <th>종료</th>
            <th>index 내용</th>
        </tr>
        <tr th:each="event , index: ${events}">
            <td th:text="${index.index}">#</td>
            <td th:text="${event.name}">이벤트 이름</td>
            <td th:text="${event.limitOfEnrollment}">100</td>
            <td th:text="${event.startDateTime}">2019년 1월 10일 오전 10시</td>
            <td th:text="${event.endDateTime}">2019년 1월 10일 오전 12시</td>
            <td th:text="${index}">index</td>
        </tr>
    </table>
</body>
</html>

5. 결과화면

블로그 이미지

미나미나미

,