springmvc 는 thymeleaf 와 jsp 두 가지 템 플 릿 엔진 을 동시에 설정 합 니 다.

15181 단어 기본 분류
문제 설명
제 의식 에 서 는 thymeleaf 를 주도 적 으로 사용 하 는 것 입 니 다. 왜냐하면 저 는 spring boot 가 추천 하 는 것 이 무엇 인지 알 고 싶 었 기 때 문 입 니 다.일반적인 springboot 프로젝트 에서 thymeleaf 의 의존 패 키 지 를 직접 도입 하면 보기 분석 을 할 수 있 습 니 다. 일반적인 spring 프로젝트 에 서 는 수 동 설정 이 필요 합 니 다. 그래서 이 문제 가 발생 했 습 니 다. 여러 템 플 릿 엔진 을 어떻게 통합 하 는 지 알 수 있 습 니 다.
해결 방안
  • thymeleaf 도입 의존
  •         
            <dependency>
                <groupId>org.thymeleafgroupId>
                <artifactId>thymeleafartifactId>
                <version>${thymeleaf.version}version>
            dependency>
    
            
            <dependency>
                <groupId>org.thymeleafgroupId>
                <artifactId>thymeleaf-spring3artifactId>
                <version>${thymeleaf.version}version>
            dependency>

    thymeleaf 버 전 2 와 3 모두 좋 습 니 다. 3 기능 이 좀 더 완벽 합 니 다.또한 tymeleaf - spring 3 or thymeleaf - sping 4 도 마음대로 사용 합 니 다.2. springmvc 보기 분석 을 설정 하 는 관건 은 이 단계 입 니 다. 이 단계 에 인터넷 버 전이 많 습 니 다.제 가 대충 분석 해 봤 는데 주로 다음 과 같은 두 가지 상황 이 있 습 니 다.
  • 첫 번 째 상황 jsp 페이지 와 html 페이지 는 각각 다른 보기 폴 더 아래 에 있 습 니 다
  • 두 번 째 상황 은 두 페이지 가 같은 보기 폴 더 에 동시에 존재 합 니 다
  • 첫 번 째 상황 설정 을 살 펴 보 겠 습 니 다. thymeleaf 공식 애완동물 상점 의 설정 주 소 는 thymeleaf 공식 설정 예 입 니 다.
    다음은 우리 의 예 이다.
        <bean id="templateResolver"
              class="org.thymeleaf.spring3.templateresolver.SpringResourceTemplateResolver">
            <property name="prefix" value="WEB-INF/" />
            <property name="suffix" value=".html" />
            <property name="templateMode" value="HTML5" />
            <property name="cacheable" value="false" />
            <property name="characterEncoding" value="UTF-8"/>
        bean>
    
        <bean id="templateEngine"
              class="org.thymeleaf.spring3.SpringTemplateEngine">
            <property name="templateResolver" ref="templateResolver" />
        bean>
        
        <bean class="org.springframework.web.servlet.view.ContentNegotiatingViewResolver">
    
            <property name="viewResolvers">
                <list>
                    
                    <bean class="org.thymeleaf.spring3.view.ThymeleafViewResolver">
                        <property name="characterEncoding" value="UTF-8"/>
                        <property name="templateEngine" ref="templateEngine" />
                        <property name="viewNames" value="html/*"/>
                        <property name="order" value="2" />
                    bean>
                    
                    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
                        <property name="prefix" value="/WEB-INF/"/>
                        <property name="suffix" value=".jsp"/>
                        <property name="viewNames" value="jsp/*"/>
                        <property name="order" value="1" />
                    bean>
    
                list>
            property>
        bean>

    관건: 다음 두 번 째 상황 설정:
    첫 번 째 설정 에서 연 장 된 것 입 니 다. prefix 의 값 이 같 습 니 다.
    <bean id="templateResolver"
              class="org.thymeleaf.spring3.templateresolver.SpringResourceTemplateResolver">
            <property name="prefix" value="WEB-INF/views/" />
            <property name="templateMode" value="HTML5" />
            <property name="cacheable" value="false" />
            <property name="characterEncoding" value="UTF-8"/>
        bean>
    
        <bean id="templateEngine"
              class="org.thymeleaf.spring3.SpringTemplateEngine">
            <property name="templateResolver" ref="templateResolver" />
        bean>
        
        <bean class="org.springframework.web.servlet.view.ContentNegotiatingViewResolver">
    
            <property name="viewResolvers">
                <list>
                    
                    <bean class="org.thymeleaf.spring3.view.ThymeleafViewResolver">
                        <property name="characterEncoding" value="UTF-8"/>
                        <property name="templateEngine" ref="templateEngine" />
                        <property name="viewNames" value="*.html" />
                        <property name="order" value="1" />
                    bean>
                    
                    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
                        <property name="prefix" value="/WEB-INF/views/"/>
                        <property name="viewNames" value="*.jsp"/>
                        <property name="order" value="2" />
                    bean>
    
                list>
            property>
        bean>

    다른 점 은 여기 있다. 여기까지 아직 끝나 지 않 았 습 니 다. controller 층 에서 어떻게 돌아 가 야 합 니까?
    첫 번 째 상황 return "jsp/abc" || return "thymeleaf/abc" 두 번 째 상황 return "abc.jsp" || return "abc.html"

    좋은 웹페이지 즐겨찾기