001package io.jstach.opt.spring.example;
002
003import java.util.List;
004
005import org.springframework.beans.factory.annotation.Autowired;
006import org.springframework.context.ApplicationContext;
007import org.springframework.context.annotation.Bean;
008import org.springframework.context.annotation.Configuration;
009import org.springframework.http.converter.HttpMessageConverter;
010import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
011
012import io.jstach.jstachio.JStachio;
013import io.jstach.opt.spring.web.JStachioHttpMessageConverter;
014import io.jstach.opt.spring.webmvc.JStachioModelViewConfigurer;
015import io.jstach.opt.spring.webmvc.ViewSetupHandlerInterceptor;
016
017/**
018 * Configures MVC using {@link JStachioHttpMessageConverter} to allow returning models
019 * which will be rendered using JStachio runtime.
020 *
021 * @see JStachioHttpMessageConverter
022 * @author agentgt
023 * @author dsyer
024 */
025@Configuration
026public class WebConfig implements WebMvcConfigurer {
027
028        private final JStachio jstachio;
029
030        /**
031         * Configures based on the jstachio found by spring
032         * @param jstachio the found jstachio
033         */
034        @Autowired
035        public WebConfig(JStachio jstachio) {
036                this.jstachio = jstachio;
037        }
038
039        @Override
040        public void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
041                converters.add(new JStachioHttpMessageConverter(jstachio));
042        }
043
044        /**
045         * Configures an interceptor for before rendering logic useful for adding additional
046         * data to JStache models.
047         * @param context searched for {@link JStachioModelViewConfigurer}s.
048         * @return interceptor that will automatically be added to the web context.
049         */
050        @Bean
051        @SuppressWarnings("exports")
052        public ViewSetupHandlerInterceptor viewSetupHandlerInterceptor(ApplicationContext context) {
053                return new ViewSetupHandlerInterceptor(context);
054        }
055
056}