스프링 프레임워크는 자바 플랫폼을 위한 오픈소스 애플리케이션 프레임워크
동적인 웹 사이트를 개발하기 위한 여러가지 서비스를 제공

스프링 프로젝트 만들기

프로젝트 옵션
Project : 빌드 툴 (프로젝트에 필요한 의존성을 관리하는 툴) 선택
과거에는 Maven
- xml 방식으로 작성
- depency 태그로 라이브러리 복붙
- 매번 정확한 버전을 입력해줘야 함
최근에는 Gradle
- 주소 복붙으로 가능
- 매번 정확한 버전을 입력해줄 필요 없음
- 더 직관적

Spring Boot : 사용할 버전 선택
- SNAPshot : 개발중인 버전
- M2 : 베타버전 ( 버그테스트 중)
Group : 회사의 identity
artifact : 프로젝트 이름
Packaging : 스프링부트는 웬만하면 jar로 배포
Dependencies : 프로젝트 생성과 동시에 사용할 라이브러리
- spring web : 별도 설치 없이 톰캣, 서블릿 사용 가능
- thymeleaf : jsp 대신 사용할 라이브러리
generate로 프로젝트 생성
사용할 workplace에서 프로젝트 zip 풀기
① import 하기

② html 파일 불러올 수 있는 환경 만들기
eclipse marketplace 들어가기

순서대로 눌러주기

③ application.properties 사용해서 환경설정하기
src/main/resources의 application.properties에서 환경설정 가능함
port 수정

server.port=9090
spring.http.encoding.charset=UTF-8
spring.http.encoding.enabled=true
spring.http.encoding.force=true
spring.thymeleaf.cache=false
// 새로고침으로 타임리프 변경사항 웹에서 확인 가능
④ build.gradle 이용하면 원하는 라이브러리 추가 생략 가능

⑤ 타임리프 템플릿 추가

① 스프링 시작점
💡 예시
<<Controller.java>>
@GetMapping("/hello-mvc")
public String helloMvc(@RequestParam(value = "name", required = false, defaultValue = "required value") String name, Model model) {
model.addAttribute("name", name);
return "hello-template";
}
<<hello-template.html>>
<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<p th:text="'hello ' + ${name}">Hello empty</p>
</body>
</html>
🙆♀️ 알아두기
💡 예시
<HomeController.java>
@Controller
public class HomeContoller {
// localhost:9090으로 호출하면 home() 호출
@GetMapping("/")
public String home() {
return "home";
}
}
<src/main/resources/templates/home.html>
<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
home 출력 테스트
</body>
</html>
👍 결과
localhost:9090으로 호출하면 home.html 파일 출력
① ViewResolver (뷰 리졸버)
② DispatcherServlet
💡 예시
@Controller
public class MemberController {
// member url mapping
@RequestMapping("member")
public String getMember(Model model) {
MemberDTO member = new MemberDTO( 1, "자바학생", "01012345678");
model.addAttribute("member",member);
return "thymeleaf/member";
// templates 파일 밑에 thymeleaf 밑에 member.html
}
}