TIL

230313 [Spring, thymeleaf] (Controller / Mapping)

하차모 2023. 3. 13. 17:51

1. 프로젝트 생성 : new - Spring Starter Project

  src/main/java -> controller, vo 등

  src/main/resources -> html 등 template 안에

 

2. controller 생성 : 기본 폴더 안에 .controller 폴더 생성

  @Controller : 해당 클래스가 컨트롤러 역할을 하는 클래스임을 지정

  @RequestMapping("/"), @GetMapping("/"), @PostMapping("/") : " "안에 지정한 경로로 시작되는 요청 처리

  request는 클래스 레벨에서 get과 post는 메소드 레벨에서 사용

  post는 form태그 post 메소드로 넘길 때 사용

 

3. 데이터 넘겨 받기

@GetMapping("/boardWrite")
public String boardWrite() {
    return "board_write"; //리턴 값과 같은 이름의 html 파일로 이동한다. (확장자명은 작성하지 않는다.)
}

// html파일에서 input 태그로 데이터를 넘겨 받을 때,
//name 속성과 똑같은 이름으로 매개변수를 지정하면 자동으로 들고 온다.
@PostMapping("/regBoard")
public String regBoard(String title, String content, int age) {
    System.out.println(title);
    System.out.println(content);
    System.out.println(age);
    return "board_info";
}

 

매개변수를 하나씩 지정하지 않고 vo를 만들어 다같이 들고오는 방법이 있다. (DTO와 같이)

package com.study.test.vo;

public class BoardVO {
	private String title;
	private String content;
	private int age;
	
	public String getTitle() {
		return title;
	}
	public void setTitle(String title) {
		this.title = title;
	}
	public String getContent() {
		return content;
	}
	public void setContent(String content) {
		this.content = content;
	}
	public int getAge() {
		return age;
	}
	public void setAge(int age) {
		this.age = age;
	}
	
	@Override
	public String toString() {
		return "BoardVO [title=" + title + ", content=" + content + ", age=" + age + "]";
	}
}
// BoardVO boardVO : 커맨드 객체
//클래스를 만들어 변수 이름을 input태그의 name 속성과 똑같이 만들어 주면 자동으로 들고 온다.
@PostMapping("/regBoard2")
public String regBoard2(BoardVO boardVO) {
    return "board_info";
}

 

4. thymeleaf

html 파일에서 데이터를 넘겨 받아 화면에 뿌려주기 위해 jsp 대신 thymeleaf를 사용할 수 있다.

<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<form action="/member/showInfo" method="post">
	<input type="hidden" name="id" th:value="${memberVO.id}">
	<input type="hidden" name="pw" th:value="${memberVO.pw}">
	<input type="hidden" name="name" th:value="${memberVO.name}">
	<input type="hidden" name="email" th:value="${memberVO.email}">
	<input type="hidden" name="bday" th:value="${memberVO.bday}">
	<th:block th:each="e : ${memberVO.tel}">
		<input type="hidden" name="tel" th:value="${e}">
	</th:block>
	

	<span th:text="${memberVO.name}"></span> 회원님 가입을 환영합니다.<br>
	[[${memberVO.name}]] 회원님 가입을 환영합니다.<br>
	관심 레슨 : 
	<input type="checkbox" name="lesson" value="수영">수영
	<input type="checkbox" name="lesson" value="헬스">헬스
	<input type="checkbox" name="lesson" value="요가">요가
	<input type="checkbox" name="lesson" value="필라테스">필라테스<br>
	레슨에 참여한 적 있나요 
	<input type="radio" name="expLesson" value="예">예
	<input type="radio" name="expLesson" value="아니오">아니오<br>
	추천인명 : <input type="text" name="recommendName"><br>
	자기소개 : <textarea rows="5" cols="50" name="intro"></textarea><br>
	<input type="submit" value="입력 완료">
</form>
</body>
</html>