template might not exist or might not be accessible by any of the configured Template Resolvers

classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

template might not exist or might not be accessible by any of the configured Template Resolvers

zubair
i am trying to validate a form field;

package hello;

import javax.validation.Valid;

import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

@Controller
public class WebController{
   
    @RequestMapping(value="/",method=RequestMethod.GET)
    public String showForm(Person person){
        return "form";
    }

    @RequestMapping(value="/",method=RequestMethod.POST)
    public String enterAge(@Valid Person person, BindingResult bindingResult, RedirectAttributes redirectAttributes){
        if (bindingResult.hasErrors()){
            redirectAttributes.addFlashAttribute("error",bindingResult.getFieldError().getDefaultMessage());
            return "redirect:/";
        }
        return "success";
    }
}
--------------------------------------------------------------

package hello;

import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.SpringApplication;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.thymeleaf.spring3.SpringTemplateEngine;
import org.thymeleaf.spring3.view.ThymeleafViewResolver;
import org.thymeleaf.templateresolver.ServletContextTemplateResolver;

@Configuration
@ComponentScan
@EnableAutoConfiguration
public class Application{

    @Bean
    ServletContextTemplateResolver templateResolver(){
        ServletContextTemplateResolver resolver=new ServletContextTemplateResolver();
        resolver.setSuffix(".html");
        resolver.setPrefix("/resources/templates/");
        resolver.setTemplateMode("HTML5");
        return resolver;
    }

    @Bean
    SpringTemplateEngine engine(){
         SpringTemplateEngine engine=new SpringTemplateEngine();
         engine.setTemplateResolver(templateResolver());
         return engine;
    }

    @Bean
    ThymeleafViewResolver viewResolver(){
        ThymeleafViewResolver viewResolver=new ThymeleafViewResolver();
        viewResolver.setTemplateEngine(engine());
        return viewResolver;
    }

        public static void main(String [] args){
                SpringApplication.run(Application.class, args);
        }
}
----------------------------------------------------------------------------------------------

<!DOCTYPE HTML>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8"/>
    <title>Varify your age!!</title>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
</head>
<body>
    <form action="#" th:action="@{/}" th:object="${person}" method="POST">
                                                                                            <input type="submit" value="Submit" />       
How old are you?<input type="text" th:field="*{age}" /><div id="errors" th:text="${error}"></div>
     </form> 
</body>
</html>
-----------------------------------------------------------------------------------------

buildscript {
    repositories {
        maven { url "http://repo.spring.io/libs-snapshot" }
        mavenLocal()
    }
    dependencies{
        classpath("org.springframework.boot:spring-boot-gradle-plugin:0.5.0.M4")
    }
}

apply plugin: 'java'
apply plugin: 'eclipse'
apply plugin: 'idea'
apply plugin: 'spring-boot'

jar {
    baseName = 'gs-serving-web-content'
    version =  '0.1.0'
}

repositories {
    mavenCentral()
    maven { url "http://repo.spring.io/libs-milestone" }
}

dependencies {
    compile("org.springframework.boot:spring-boot-starter-web:0.5.0.M5")
    compile("org.thymeleaf:thymeleaf-spring3:2.0.17")
    compile("org.hibernate:hibernate-validator:4.3.1.Final")
    testCompile("junit:junit:4.11")
}

task wrapper(type: Wrapper) {
    gradleVersion = '1.8'
}
-------------------------------------------------------------------------------

i liked the simplicity of thymeleaf templating from frontend designer perspective, but above code is raising error which suggests me that controller or templateResolver is not able to find form.html to render
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: template might not exist or might not be accessible by any of the configured Template Resolvers

danielfernandez
Administrator
You are using the ServletContextTemplateResolver and using "/resources/templates/" as prefix. Are your templates really in "/resources/templates/"? Maybe it's "/WEB-INF/resources/templates/" (which is more common)?
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: template might not exist or might not be accessible by any of the configured Template Resolvers

joarobles
As it was 2013, can you remember the prefix you finally used? It would help me a lot, thanks
Loading...