title: Issues with Spring Boot and Thymeleaf
body: <p>Here are my configuration classes::</p>
  
  <p>In the templateResolver() method without ServletContext parameter i get compile error so add it as a parameter and give it to ServletContextTemplateResolver(servletContext);</p>
  
  <pre><code>@Configuration
  @EnableWebMvc
  @ComponentScan(basePackages= {"com.packtpub.springsecurity"})
  public class ThymeleafConfig {
  
      @Bean
      public ServletContextTemplateResolver templateResolver(ServletContext servletContext) {
          ServletContextTemplateResolver resolver = new ServletContextTemplateResolver(servletContext);
          resolver.setPrefix("/WEB-INF/templates/");
          resolver.setSuffix(".html");
          resolver.setTemplateMode("HTML5");
          resolver.setCacheable(false);
          resolver.setOrder(1);
          return resolver;
      }
  
      @Bean
      public SpringTemplateEngine templateEngine(final ServletContextTemplateResolver templateResolver) {
          SpringTemplateEngine engine = new SpringTemplateEngine();
          engine.setTemplateResolver(templateResolver);
          return engine;
      }
  
      @Bean
      public ThymeleafViewResolver thymeleafViewResolver(final SpringTemplateEngine templateEngine) {
          ThymeleafViewResolver resolver = new ThymeleafViewResolver();
          resolver.setTemplateEngine(templateEngine);
          return resolver;
      }
  
  }
  </code></pre>
  
  <p>When i run my application i get following error:::</p>
  
  <pre><code>***************************
  APPLICATION FAILED TO START
  ***************************
  
  Description:
  
  Parameter 0 of method templateResolver in com.packtpub.springsecurity.web.configuration.ThymeleafConfig required a bean of type 'javax.servlet.ServletContext' that could not be found.
  
  The injection point has the following annotations:
      - @org.springframework.beans.factory.annotation.Autowired(required=true)
  
  
  Action:
  
  Consider defining a bean of type 'javax.servlet.ServletContext' in your configuration.
  </code></pre>
  
  <p>What am i doing wrong?
  thanks</p>
  
  <p>Other config files are</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt;
  &lt;project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
      xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"&gt;
      &lt;modelVersion&gt;4.0.0&lt;/modelVersion&gt;
  
      &lt;groupId&gt;chapter2&lt;/groupId&gt;
      &lt;artifactId&gt;chapter2&lt;/artifactId&gt;
      &lt;version&gt;0.0.1-SNAPSHOT&lt;/version&gt;
      &lt;packaging&gt;jar&lt;/packaging&gt;
  
      &lt;name&gt;chapter2&lt;/name&gt;
      &lt;description&gt;chapter 2 test&lt;/description&gt;
  
      &lt;parent&gt;
          &lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;
          &lt;artifactId&gt;spring-boot-starter-parent&lt;/artifactId&gt;
          &lt;version&gt;2.0.6.RELEASE&lt;/version&gt;
          &lt;relativePath/&gt; &lt;!-- lookup parent from repository --&gt;
      &lt;/parent&gt;
  
      &lt;properties&gt;
          &lt;project.build.sourceEncoding&gt;UTF-8&lt;/project.build.sourceEncoding&gt;
          &lt;project.reporting.outputEncoding&gt;UTF-8&lt;/project.reporting.outputEncoding&gt;
          &lt;java.version&gt;1.8&lt;/java.version&gt;
      &lt;/properties&gt;
  
      &lt;dependencies&gt;
          &lt;dependency&gt;
              &lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;
              &lt;artifactId&gt;spring-boot-starter-data-jpa&lt;/artifactId&gt;
          &lt;/dependency&gt;
          &lt;dependency&gt;
              &lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;
              &lt;artifactId&gt;spring-boot-starter-web&lt;/artifactId&gt;
          &lt;/dependency&gt;
  
          &lt;dependency&gt;
              &lt;groupId&gt;mysql&lt;/groupId&gt;
              &lt;artifactId&gt;mysql-connector-java&lt;/artifactId&gt;
              &lt;scope&gt;runtime&lt;/scope&gt;
          &lt;/dependency&gt;
          &lt;dependency&gt;
              &lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;
              &lt;artifactId&gt;spring-boot-starter-test&lt;/artifactId&gt;
              &lt;scope&gt;test&lt;/scope&gt;
          &lt;/dependency&gt;
  
  
  
          &lt;!-- Thymeleaf --&gt;  
          &lt;dependency&gt;
              &lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;
              &lt;artifactId&gt;spring-boot-starter-thymeleaf&lt;/artifactId&gt;
          &lt;/dependency&gt;
  
  
  
  
  
          &lt;!-- Spring dependencies START--&gt;
          &lt;dependency&gt;
             &lt;groupId&gt;org.springframework.security&lt;/groupId&gt;
             &lt;artifactId&gt;spring-security-web&lt;/artifactId&gt;
             &lt;/dependency&gt;
          &lt;dependency&gt;
             &lt;groupId&gt;org.springframework.security&lt;/groupId&gt;
             &lt;artifactId&gt;spring-security-config&lt;/artifactId&gt;
             &lt;/dependency&gt;
          &lt;dependency&gt;
             &lt;groupId&gt;org.springframework.security&lt;/groupId&gt;
             &lt;artifactId&gt;spring-security-crypto&lt;/artifactId&gt;
             &lt;/dependency&gt;
          &lt;dependency&gt;
             &lt;groupId&gt;org.springframework&lt;/groupId&gt;
             &lt;artifactId&gt;spring-webmvc&lt;/artifactId&gt;
             &lt;/dependency&gt;
          &lt;dependency&gt;
             &lt;groupId&gt;org.springframework&lt;/groupId&gt;
             &lt;artifactId&gt;spring-jdbc&lt;/artifactId&gt;
             &lt;/dependency&gt;
          &lt;!-- Servlet and JSP related dependencies --&gt;
          &lt;dependency&gt;
             &lt;groupId&gt;javax.servlet&lt;/groupId&gt;
             &lt;artifactId&gt;javax.servlet-api&lt;/artifactId&gt;
             &lt;scope&gt;provided&lt;/scope&gt;
          &lt;/dependency&gt;
          &lt;dependency&gt;
             &lt;groupId&gt;javax.servlet.jsp&lt;/groupId&gt;
             &lt;artifactId&gt;javax.servlet.jsp-api&lt;/artifactId&gt;
             &lt;version&gt;2.3.1&lt;/version&gt;
             &lt;scope&gt;provided&lt;/scope&gt;
          &lt;/dependency&gt;
          &lt;dependency&gt;
             &lt;groupId&gt;javax.servlet.jsp.jstl&lt;/groupId&gt;
             &lt;artifactId&gt;javax.servlet.jsp.jstl-api&lt;/artifactId&gt;
             &lt;version&gt;1.2.1&lt;/version&gt;
          &lt;/dependency&gt;
          &lt;dependency&gt;
             &lt;groupId&gt;taglibs&lt;/groupId&gt;
             &lt;artifactId&gt;standard&lt;/artifactId&gt;
             &lt;version&gt;1.1.2&lt;/version&gt;
          &lt;/dependency&gt;
          &lt;!-- For datasource configuration --&gt;
          &lt;dependency&gt;
             &lt;groupId&gt;org.apache.commons&lt;/groupId&gt;
             &lt;artifactId&gt;commons-dbcp2&lt;/artifactId&gt;
             &lt;/dependency&gt;
          &lt;!-- We will be using MySQL as our database server --&gt;
          &lt;dependency&gt;
             &lt;groupId&gt;mysql&lt;/groupId&gt;
             &lt;artifactId&gt;mysql-connector-java&lt;/artifactId&gt;
             &lt;version&gt;6.0.6&lt;/version&gt;
          &lt;/dependency&gt;   
          &lt;!-- Spring dependencies END --&gt;    
  
  
  
          &lt;dependency&gt;
      &lt;groupId&gt;org.json&lt;/groupId&gt;
      &lt;artifactId&gt;json&lt;/artifactId&gt;
      &lt;version&gt;20170516&lt;/version&gt;
  &lt;/dependency&gt;
  
      &lt;/dependencies&gt;
  
      &lt;build&gt;
          &lt;plugins&gt;
              &lt;plugin&gt;
                  &lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;
                  &lt;artifactId&gt;spring-boot-maven-plugin&lt;/artifactId&gt;
              &lt;/plugin&gt;
          &lt;/plugins&gt;
      &lt;/build&gt;
  
  
  &lt;/project&gt;
  
  
  
  @Configuration
  //@Import({SecurityConfig.class, DataSourceConfig.class})
  @ComponentScan(basePackages =
          {
                  "com.packtpub.springsecurity.dataaccess",
                  "com.packtpub.springsecurity.domain",
                  "com.packtpub.springsecurity.service"
          }
  )
  @PropertySource(value = {"classpath:application.properties"})
  public class JavaConfig {
  
      /**
       * Note: If you want to use @PropertySource, you must create a static
       * PropertySourcesPlaceholderConfigurer with the @Bean as seen here.
       * @return PropertySourcesPlaceholderConfigurer
       * @throws java.io.IOException
       */
      @Bean
      public static PropertySourcesPlaceholderConfigurer propertySourcesPlaceholderConfigurer() throws IOException {
          PropertySourcesPlaceholderConfigurer propertySourcesPlaceholderConfigurer = new PropertySourcesPlaceholderConfigurer();
          propertySourcesPlaceholderConfigurer.setIgnoreUnresolvablePlaceholders(Boolean.TRUE);
          propertySourcesPlaceholderConfigurer.setProperties(yamlPropertiesFactoryBean().getObject());
          return propertySourcesPlaceholderConfigurer;
      }
  
      @Bean
      public static YamlPropertiesFactoryBean yamlPropertiesFactoryBean() {
          YamlPropertiesFactoryBean yaml = new YamlPropertiesFactoryBean();
          yaml.setResources(new ClassPathResource("application.yml"));
          return yaml;
      }
  } // The end...
  
  
  
  @Order(1)
  public class SecurityWebAppInitializer
          extends AbstractSecurityWebApplicationInitializer {
  
      /**
       * Don't initialize the filter directly, the Spring WebApplicationInitializer
       * parent will take care of the initialization.
       */
      public SecurityWebAppInitializer() {
          super();
      }
  
  } // The end...
  
  
  
  
  public class WebAppInitializer
          extends AbstractAnnotationConfigDispatcherServletInitializer {
  
      @Override
      protected Class&lt;?&gt;[] getRootConfigClasses() {
          return new Class[] { JavaConfig.class, SecurityConfig.class, DataSourceConfig.class };
      }
  
      @Override
      protected Class&lt;?&gt;[] getServletConfigClasses() {
          return new Class[] { WebMvcConfig.class };
      }
  
      @Override
      protected String[] getServletMappings() {
          return new String[] { "/*" };
      }
  
      @Override
      public void onStartup(final ServletContext servletContext)
              throws ServletException {
  
          // Register DispatcherServlet
          super.onStartup(servletContext);
  
          // Register H2 Admin console:
          ServletRegistration.Dynamic h2WebServlet = servletContext.addServlet("h2WebServlet",
                  "org.h2.server.web.WebServlet");
          h2WebServlet.addMapping("/admin/h2/*");
          h2WebServlet.setInitParameter("webAllowOthers", "true");
  
      }
  
  } // The End...
  
  
  
  
  @Configuration
  @EnableWebMvc
  @Import({ThymeleafConfig.class})
  @ComponentScan(basePackages = {
          "com.packtpub.springsecurity.web.controllers",
          "com.packtpub.springsecurity.web.model"
  })
  public class WebMvcConfig extends WebMvcConfigurerAdapter
  {
  
      @Autowired
      private ThymeleafViewResolver thymeleafViewResolver;
  
      /**
       * We mention this in the book, but this helps to ensure that the intercept-url patterns prevent access to our
       * controllers. For example, once security has been applied for administrators try commenting out the modifications
       * to the super class and requesting &lt;a
       * href="http://localhost:800/calendar/events/.html"&gt;http://localhost:800/calendar/events/.html&lt;/a&gt;. You will
       * observe that security is bypassed since it did not match the pattern we provided. In later chapters, we discuss
       * how to secure the service tier which helps mitigate bypassing of the URL based security too.
       */
      // FIXME: FInd out what this is and why it is here.
      @Bean
      public RequestMappingHandlerMapping requestMappingHandlerMapping() {
          RequestMappingHandlerMapping result = new RequestMappingHandlerMapping();
          result.setUseSuffixPatternMatch(false);
          result.setUseTrailingSlashMatch(false);
          return result;
      }
  
      @Override
      public void addResourceHandlers(final ResourceHandlerRegistry registry) {
          super.addResourceHandlers(registry);
          registry.addResourceHandler("/resources/**")
                  .addResourceLocations("/resources/")
                  .setCachePeriod(31_556_926)
          ;
      }
  
      @Override
      public void configureContentNegotiation(final ContentNegotiationConfigurer configurer) {
          configurer
                  .ignoreAcceptHeader(false)
                  .favorPathExtension(true) // .html / .json / .ms
                  .defaultContentType(MediaType.TEXT_HTML) // text/html
                  .mediaTypes(
                          new HashMap&lt;String, MediaType&gt;(){
                              {
                                  put("html", MediaType.TEXT_HTML);
                                  put("xml", MediaType.APPLICATION_XML);
                                  put("json", MediaType.APPLICATION_JSON);
                              }
                          })
          ;
      }
  
      @Override
      public void configureMessageConverters(List&lt;HttpMessageConverter&lt;?&gt;&gt; converters) {
          converters.add(new MappingJackson2HttpMessageConverter());
      }
  
      /*@Bean
      public ContentNegotiatingViewResolver contentNegotiatingViewResolver() {
  
          ContentNegotiatingViewResolver result = new ContentNegotiatingViewResolver();
          Map&lt;String, String&gt; mediaTypes = new HashMap&lt;&gt;();
          mediaTypes.put("json", MediaType.APPLICATION_JSON_VALUE);
  //        result.setMediaTypes(mediaTypes);
  
          result.setDefaultViews(Collections.singletonList(jacksonView()));
  
          return result;
      }*/
  
      @Bean
      public MappingJackson2JsonView jacksonView() {
          MappingJackson2JsonView jacksonView = new MappingJackson2JsonView();
          jacksonView.setExtractValueFromSingleKeyModel(true);
  
          Set&lt;String&gt; modelKeys = new HashSet&lt;String&gt;();
          modelKeys.add("events");
          modelKeys.add("event");
          jacksonView.setModelKeys(modelKeys);
  
          return jacksonView;
      }
  
      @Override
      public void configureViewResolvers(final ViewResolverRegistry registry) {
          registry.viewResolver(thymeleafViewResolver);
      }
  
  
      // i18N support
      @Bean
      public ReloadableResourceBundleMessageSource messageSource() {
          ReloadableResourceBundleMessageSource resource = new ReloadableResourceBundleMessageSource();
          resource.setBasenames("/WEB-INF/locales/messages");
          resource.setDefaultEncoding("UTF-8");
          resource.setFallbackToSystemLocale(Boolean.TRUE);
          return resource;
      }
  
      @Override
      public void configureDefaultServletHandling(DefaultServletHandlerConfigurer configurer){
          configurer.enable();
      }
  
  
  }
  </code></pre>
  
  <p><strong>UPDATED</strong></p>
  
  <p>I have deleted the following dependencies from the POM according to @Adina in the comment below but still get the errors</p>
  
  <pre><code>&lt;!-- dependency&gt;
             &lt;groupId&gt;javax.servlet&lt;/groupId&gt;
             &lt;artifactId&gt;javax.servlet-api&lt;/artifactId&gt;
             &lt;scope&gt;provided&lt;/scope&gt;
          &lt;/dependency&gt;
          &lt;dependency&gt;
             &lt;groupId&gt;javax.servlet.jsp&lt;/groupId&gt;
             &lt;artifactId&gt;javax.servlet.jsp-api&lt;/artifactId&gt;
             &lt;version&gt;2.3.1&lt;/version&gt;
             &lt;scope&gt;provided&lt;/scope&gt;
          &lt;/dependency&gt;
          &lt;dependency&gt;
             &lt;groupId&gt;javax.servlet.jsp.jstl&lt;/groupId&gt;
             &lt;artifactId&gt;javax.servlet.jsp.jstl-api&lt;/artifactId&gt;
             &lt;version&gt;1.2.1&lt;/version&gt;
          &lt;/dependency&gt;
          &lt;dependency&gt;
             &lt;groupId&gt;taglibs&lt;/groupId&gt;
             &lt;artifactId&gt;standard&lt;/artifactId&gt;
             &lt;version&gt;1.1.2&lt;/version&gt;
          &lt;/dependency--&gt;
  </code></pre>
  
  <p>What else can i do again</p>