Spring Boot 응용 설정

SpringBoot 의 자동 조립 은 보기 해석 기 를 설치 하 였 습 니까?
SpringBoot 가 자동 으로 설 치 된 WebMvcAutoConfiguration 클래스 에서 ViewResolver(보기 해상도)에 대한 클래스 를 설치 한 것 을 볼 수 있 습 니 다.SpringBoot 는 Internal ResourceView Resolver 류 를 자동 으로 설치 하고 외부 자원 설정 을 통 해 이 보기 해상 도 를 설정 하 는 것 을 볼 수 있 습 니 다this.mvcProperties.getView().getPrefix()따라서 우 리 는application.properties파일 에서 이 보기 해상 도 를 JSP 를 분석 하 는 데 사용 할 수 있 습 니 다.

 @Bean
 @ConditionalOnMissingBean
 public InternalResourceViewResolver defaultViewResolver() {
  InternalResourceViewResolver resolver = new InternalResourceViewResolver();
  resolver.setPrefix(this.mvcProperties.getView().getPrefix());
  resolver.setSuffix(this.mvcProperties.getView().getSuffix());
  return resolver;
 }
SpringBoot JSP 사용
SpringBoot 는 자동 으로 설치 할 때 기본적으로 JSP 의 보기 해석 기 Internal ResourceView Resolver 를 설치 합 니 다.그래서 우 리 는 설정 만 하고 사용 하면 됩 니 다.SpringBoot 에서 JSP 를 사용 하 는 것 은 좀 번 거 롭 습 니 다.제 개인 적 인 이해 에 잘못된 부분 이 있 을 수도 있 습 니 다.더 좋 은 설정 방법 을 아 는 친구 가 있 으 면 메 시 지 를 남 겨 주세요.
첫 번 째 단계:사용자 정의 웹 앱 디 렉 터 리 를 만 듭 니 다.다음 과 같 습 니 다.

두 번 째 단계:이 폴 더 를 프로젝트 의 WEB 모듈 로 설정 합 니 다.

STEP 3:JSP 의존 도 가 져 오기

 <dependency>
  <groupId>org.apache.tomcat.embed</groupId>
  <artifactId>tomcat-embed-jasper</artifactId>
  <scope>provided</scope>
 </dependency>
 <dependency>
  <groupId>javax.servlet</groupId>
  <artifactId>jstl</artifactId>
 </dependency>
STEP 4:SpringBoot 의 속성 파일application.propertiesJSP 의 경로 설정

spring.mvc.view.prefix=/
spring.mvc.view.suffix=.jsp
다섯 번 째 단계:Maven 의 pom.xml 파일 포장 방식 을 war 로 변경 합 니 다.

<packaging>war</packaging>

SpringBoot 에서 Thymeleaf 사용 하기
SpringBoot 는 공식 적 으로 thymeleaf 를 선택 한 보기 해석 기로 추천 하기 때문에 SpringBoot 는 Thymeleaf 에 대한 지원 이 매우 좋 습 니 다.SpringBoot 가 Thymeleaf 역할 의 기본 보기 해석 기 를 어떻게 사용 하 는 지 보 여 줍 니 다.
STEP 1:Thymeleaf 의 의존 도 를 가 져 옵 니 다.

 <dependency>
  <groupId>org.springframework.boot</groupId>
  <artifactId>spring-boot-starter-thymeleaf</artifactId>
 </dependency>
두 번 째 단계:Thymeleaf 템 플 릿 폴 더 를 만 들 고 Resources 디 렉 터 리 에 templates 디 렉 터 리 를 만 듭 니 다.

이 폴 더 의 이름 은 제 가 마음대로 명명 한 것 이 아 닙 니 다.SpringBoot 가 Thymeleaf 보기 해석 기 를 자동 으로 설치 할 때 미리 정 의 했 습 니 다.정의 원본 코드 를 살 펴 보 겠 습 니 다.

@ConfigurationProperties(prefix = "spring.thymeleaf")
public class ThymeleafProperties {

 private static final Charset DEFAULT_ENCODING = StandardCharsets.UTF_8;

 public static final String DEFAULT_PREFIX = "classpath:/templates/";

 public static final String DEFAULT_SUFFIX = ".html";
 }
SpringBoot 에서 Freemark 사용 하기
STEP 1:Maven 의존 도입

<dependency>
   <groupId>org.springframework.boot</groupId>
   <artifactId>spring-boot-starter-freemarker</artifactId>
  </dependency>
두 번 째 단계:Freemark 템 플 릿 폴 더 를 만 들 고 Resources 디 렉 터 리 에 templates 디 렉 터 리 를 만 듭 니 다.

@ConfigurationProperties(prefix = "spring.freemarker")
public class FreeMarkerProperties extends AbstractTemplateViewResolverProperties {

 public static final String DEFAULT_TEMPLATE_LOADER_PATH = "classpath:/templates/";

 public static final String DEFAULT_PREFIX = "";

 public static final String DEFAULT_SUFFIX = ".ftl";
 }
SpringBoot 가 Freemarker 보기 해석 기 를 자동 으로 설치 하 는 것 을 볼 수 있 습 니 다.기본적으로 템 플 릿 파일 을 classpath:/templates/경로 에 두 는 것 입 니 다.SpringBoot 설정 파일 에서 도 자체 적 으로 설정 할 수 있 습 니 다.
알림:프 리 마크 보기 해석 기 를 쓸 때 첫 번 째 JSP 내부 자원 해석 기 를 삭제 하지 않 았 기 때문에 공존 합 니 다.그래서 SpringBoot 가 설치 할 때 우선 순 위 를 설정 한 것 을 알 수 있 습 니 다.아래 그림 에서 그들의 우선 순위 순 서 를 볼 수 있다.Freemarker>Thymeleaf>InternalResourceViewResolver`

총결산
이상 은 이 글 의 전체 내용 입 니 다.본 논문 의 내용 이 여러분 의 학습 이나 업무 에 어느 정도 참고 학습 가치 가 있 기 를 바 랍 니 다.궁금 한 점 이 있 으 시 면 댓 글 을 남 겨 주 셔 서 저희 에 대한 지지 에 감 사 드 립 니 다.

좋은 웹페이지 즐겨찾기