title: SetViewNames, spring 4 and thymeleaf and java
body: <p>I try to develop an aplication that just has to send a email with an image. 
  I saw some solutions that refer configuring the following property</p>
  
  <pre><code>&lt;property name="viewNames" value="templates/*, pages/*" /&gt;
  </code></pre>
  
  <p>but I dont know how I have to configure in java way, thus this way doesnt works:
  thymeleaf.setViewNames("templates/*"), 
  However, it is appearing an error message:</p>
  
  <pre><code>2015-11-01 18:30:31.481 ERROR 1003 --- [nio-8080-exec-1] org.thymeleaf.TemplateEngine             : [THYMELEAF][http-nio-8080-exec-1] Exception processing template "error": Error resolving template "error", template might not exist or might not be accessible by any of the configured Template Resolvers
  2015-11-01 18:30:31.483 ERROR 1003 --- [nio-8080-exec-1] o.a.c.c.C.[.[.[/].[dispatcherServlet]    : Servlet.service() for servlet dispatcherServlet threw exception
  
  org.thymeleaf.exceptions.TemplateInputException: Error resolving template "error", template might not exist or might not be accessible by any of the configured Template Resolvers
      at org.thymeleaf.TemplateRepository.getTemplate(TemplateRepository.java:246)
      at org.thymeleaf.TemplateEngine.process(TemplateEngine.java:1104)
      at org.thymeleaf.TemplateEngine.process(TemplateEngine.java:1060)
      at org.thymeleaf.TemplateEngine.process(TemplateEngine.java:1011)
      at org.thymeleaf.spring4.view.ThymeleafView.renderFragment(ThymeleafView.java:335)
      at org.thymeleaf.spring4.view.ThymeleafView.render(ThymeleafView.java:190)
      at org.springframework.web.servlet.DispatcherServlet.render(DispatcherServlet.java:1244)
      at org.springframework.web.servlet.DispatcherServlet.processDispatchResult(DispatcherServlet.java:1027)
      at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:971)
      at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:893)
      at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:967)
      at org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:858)
      at javax.servlet.http.HttpServlet.service(HttpServlet.java:622)
      at org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:843)
      at javax.servlet.http.HttpServlet.service(HttpServlet.java:729)
      at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:291)
      at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
      at org.apache.catalina.core.ApplicationDispatcher.invoke(ApplicationDispatcher.java:721)
      at org.apache.catalina.core.ApplicationDispatcher.processRequest(ApplicationDispatcher.java:468)
      at org.apache.catalina.core.ApplicationDispatcher.doForward(ApplicationDispatcher.java:391)
      at org.apache.catalina.core.ApplicationDispatcher.forward(ApplicationDispatcher.java:318)
      at org.apache.catalina.core.StandardHostValve.custom(StandardHostValve.java:439)
      at org.apache.catalina.core.StandardHostValve.status(StandardHostValve.java:305)
      at org.apache.catalina.core.StandardHostValve.throwable(StandardHostValve.java:399)
      at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:180)
      at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:79)
      at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:88)
      at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:518)
      at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1091)
      at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:673)
      at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1526)
      at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.run(NioEndpoint.java:1482)
      at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
      at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
      at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
      at java.lang.Thread.run(Thread.java:745)
  
  2015-11-01 18:30:31.484 ERROR 1003 --- [nio-8080-exec-1] o.a.c.c.C.[Tomcat].[localhost]           : Exception Processing ErrorPage[errorCode=0, location=/error]
  
  org.springframework.web.util.NestedServletException: Request processing failed; nested exception is org.thymeleaf.exceptions.TemplateInputException: Error resolving template "error", template might not exist or might not be accessible by any of the configured Template Resolvers
      at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:979)
      at org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:858)
      at javax.servlet.http.HttpServlet.service(HttpServlet.java:622)
      at org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:843)
      at javax.servlet.http.HttpServlet.service(HttpServlet.java:729)
      at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:291)
      at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
      at org.apache.catalina.core.ApplicationDispatcher.invoke(ApplicationDispatcher.java:721)
      at org.apache.catalina.core.ApplicationDispatcher.processRequest(ApplicationDispatcher.java:468)
      at org.apache.catalina.core.ApplicationDispatcher.doForward(ApplicationDispatcher.java:391)
      at org.apache.catalina.core.ApplicationDispatcher.forward(ApplicationDispatcher.java:318)
      at org.apache.catalina.core.StandardHostValve.custom(StandardHostValve.java:439)
  </code></pre>
  
  <p>This is the code:</p>
  
  <p>the configuration </p>
  
  <pre><code>@Bean 
  public ServletContextTemplateResolver templateResolver(){
      ServletContextTemplateResolver resolver=new ServletContextTemplateResolver();
      resolver.setPrefix("/WEB-INF/");
      resolver.setSuffix(".html");
      resolver.setCharacterEncoding("UTF-8");
      resolver.setTemplateMode("HTML5");
      return resolver;
  }
  
  @Bean
  public ThymeleafViewResolver thymeleafView(){
      ThymeleafViewResolver thymeleaf = new ThymeleafViewResolver();
  
      thymeleaf.setTemplateEngine(templateEngine());
      thymeleaf.setCharacterEncoding("UTF-8");
      thymeleaf.setOrder(1);
      return thymeleaf;
  }
  
  @Bean
  public SpringTemplateEngine templateEngine() {
      SpringTemplateEngine engine = new SpringTemplateEngine();
      engine.setTemplateResolver(templateResolver());
      return engine;
  }
  
  @Bean
  public SpringResourceTemplateResolver SourceTemplateResolver(){
      SpringResourceTemplateResolver sourceTemplateResolver = new SpringResourceTemplateResolver();
  
      sourceTemplateResolver.setSuffix(".html");
      sourceTemplateResolver.setTemplateMode("HTML5");
      return sourceTemplateResolver;
  }
  </code></pre>
  
  <p>The view:</p>
  
  <pre><code>&lt;!DOCTYPE html&gt;
  &lt;html xmlns:th="http://www.thymeleaf.org"&gt;
  &lt;head&gt;
      &lt;title th:remove="all"&gt;Template for HTML email with inline image&lt;/title&gt;
      &lt;meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/&gt;
  &lt;/head&gt;
  &lt;body&gt;
  &lt;p&gt; Hello, everybody!&lt;/p&gt;
  &lt;p&gt; You have been successfully subscribed to the &lt;b&gt;Fake newsletter&lt;/b&gt; on&lt;/p&gt;
  &lt;p&gt;Your hobbies are:&lt;/p&gt;
  &lt;p&gt; You can find &lt;b&gt;your inlined image&lt;/b&gt; just below this text.&lt;/p&gt;
  
  &lt;p&gt;&lt;img src="LogoLehrer.jpg" th:src="'cid:' + ${imageResourceName}"/&gt;&lt;/p&gt;
  
  &lt;p&gt; Regards, &lt;br/&gt; &lt;em&gt;The Thymeleaf Team&lt;/em&gt;&lt;/p&gt;
  &lt;/body&gt;
  &lt;/html&gt;
  </code></pre>
  
  <p>and the controller:</p>
  
  <pre><code>@RequestMapping("/sendmail")
  public void sendMail(final Locale locale, HttpServletRequest request,
                       HttpServletResponse response,
                       @RequestParam("imageResourceName") final MultipartFile image)
          throws MessagingException, IOException {
  
      mailSender.send("lehrerxx@gmail.com", "prueba spring final", 
      "I have luck", "Lehrer", locale,request,
      response, image, image.getBytes(), image.getName());
  }
  </code></pre>
  
  <p>And the method which sends an email:</p>
  
  <pre><code>public void send(String to, String subject, String body,
       String recipientName, String recipientEmail,
       Locale locale,HttpServletRequest request,
       HttpServletResponse response, final MultipartFile image,
       final byte[] imageBytes, final String imageResourceName) throws MessagingException {
  
      // final Context ctx = new Context(locale);
      final WebContext ctx = new WebContext(request, response, servletContext, locale);
      ctx.setVariable("name", recipientName);
      ctx.setVariable("mail", recipientEmail);
      ctx.setVariable("imageResourceName", imageResourceName); 
      final String htmlContent = this.templateEngine.process("email_Template", ctx);
  
  
      MimeMessage message = javaMailSender.createMimeMessage();
      MimeMessageHelper helper;
  
      helper = new MimeMessageHelper(message,true);
      helper.setSubject(subject);
      helper.setTo(to);
      helper.setText(htmlContent, true); 
  
      final InputStreamSource imageSource = new ByteArrayResource(imageBytes);
      helper.addInline(image.getName(), imageSource, image.getContentType());
      this.javaMailSender.send(message);
  
  }
  </code></pre>