Spring Framework/Spring
컨트롤러, form action경로 설정
devstep
2022. 3. 2. 21:00
디렉토리 구성 및 세팅
src/main/resources
└ static
└ templates
- static : 정적 파일을 관리하는 곳으로 css, fonts, images, js 폴더를 생성하고 넣는다.
- templates : 동적 파일을 관리하는 곳으로 html 파일을 넣는다.
application.properties 관련설정
#템플릿 시작 위치 참조
spring.thymeleaf.prefix=classpath:templates/
#템플릿의 확장자 이름 참조
spring.thymeleaf.suffix=.html
#해당위치에 파일이 있는지 없는지 체크
spring.thymeleaf.check-template-location=true
#캐시를 남기지 않는다.
spring.thymeleaf.cache=false
- spring.thymeleaf.prefix=classpath:templates/ (템플릿 시작 위치 참조)
- spring.thymeleaf.suffix=.html (템플릿의 확장자 이름 참조)
- spring.thymeleaf.check-template-location=true (해당위치에 파일이 있는지 없는지 체크)
- spring.thymeleaf.cache=false(캐시를 남기지 않는다.)
- prefix+ 경로 + suffix가 실제 경로가 된다.
- classpath: templates/ + controller return 이름 + .html이 된다는 뜻이다.
- templates 디렉토리에 user디렉토리 속에 list.html 파일이 있다면
- return "user/list" 으로 템플릿으로 넘어간다는 뜻이다.
controller의 Mappping하는 url은 절대경로
ModelAndView 에서는 슬래쉬(/) 빼야 한다.
이유는 spring boot 내에서 template prefix 에끝에 / 가 있어서 2중으로 붙으면 문제가 되어서입니다./ 안붙이면 됩니다.
form에서 사용하는 url은 절대경로
- form 에서 url 지정하는건 보통 절대경로로 지정하는게 좋다.
<!-- 상대경로 사용, [현재 URL이 속한 계층 경로 + /save] --> <form action="save" method="post">
- 는 상대경로이고, 현재 경로인 해당 페이지의 브라우져 상 uri에서 상대경로가 추가된다. (second)
- 예) http://localhost/first 라는 페이지에서 로 선언된 링크를 누르면 http://localhost/first/second 로 이동하려고 할 것이고, second 로 된 링크를 누르면 http://localhost/second 로 이동.