0

I have a problem with mine mappings. I have a template resolver which has the following configuration:

    @Bean(name="templateResolver")
    public ServletContextTemplateResolver getServletContextTemplateResolver() {
        ServletContextTemplateResolver templateResolver = new ServletContextTemplateResolver();
        templateResolver.setPrefix("/WEB-INF/views/");
        templateResolver.setSuffix(".html");
        templateResolver.setTemplateMode("HTML5");
        templateResolver.setCharacterEncoding("UTF-8");
        templateResolver.setCacheable(false);
        return templateResolver;
    }

    @Bean(name="templateEngine")
    public SpringTemplateEngine getTemplateEngine() {
        SpringTemplateEngine templateEngine = new SpringTemplateEngine();
        templateEngine.setTemplateResolver(getServletContextTemplateResolver());
        return templateEngine;
    }

    @Bean(name="viewResolver")
    public ThymeleafViewResolver getThymeleafViewResolver() {
        ThymeleafViewResolver viewResolver = new ThymeleafViewResolver();
        viewResolver.setTemplateEngine(getTemplateEngine());
        viewResolver.setOrder(1);
        viewResolver.setCharacterEncoding("UTF-8");
        return viewResolver;
    }

And I have a structure of my project:

-WEB-INF
--views
---home.html
---registration.html

I'm using thymeleaf and spring 4.0. For home, I have added the following requestMapping in my controller:

@RequestMapping(value = "/", method = RequestMethod.GET)
public String start(Model model) {
    return "home";
}

I don't have request mapping for registration.html.

In my home.html I have following link (thymeleaf anchor tag):

        <a href="registration.html" th:href="@{/registration.html}" th:text="#{navigation.registration}">Registration</a>

I have binded my app in tomcat under /myapp and when I enter on /myapp I get the home.html page (I have a request mapping for that), but when I enter on /myapp/registration.html i get 404.

What I must do, to serve such static html files without adding every page to the controller? Is it possible? I tried with static resources but it doesn't work.

I also tried to move views directory level up so I had structure:

-WEB-INF
-views
--home.html
--registration.html

and I was able to enter on /myapp/views/registration.html, but I couldn't get into /myapp/registration.html

Ma Kro
  • 1,232
  • 4
  • 20
  • 34

2 Answers2

0

To access page with out controller (Like Layout does), Use th:substituteby="path/to/registration" . and try for it. It'll render the registration.html based on the request.

sitakant
  • 1,766
  • 2
  • 18
  • 38
  • what do you mean? This parameter is added to a div or body, I want to go to completly new page. I don't want to stay on home page, and swap the content. Can you explain what do you mean? – Ma Kro Jan 02 '15 at 14:48
  • I mean to , In Home page itself We can display the registration.html. If you want to display registeration.html in different page, then you must have to give a server request as you are working in Client-Server Architecture. else display your registration.html in home.html and hide the home.html content using css or js. – sitakant Jan 03 '15 at 08:39
0

What is your intention to get the registration.html on the root of myapp? The solution of yours is the normal behaviour of mapping those requests. If you want to serve the static Content you could use the following Operation for example:
with xml:

<mvc:resources mapping="/views/**" location="/views/" />

with Annotations and a additional Class in your Package:

@Configuration
@EnableWebMvc
public class WebConfig extends WebMvcConfigurerAdapter {
    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler("/views/**").addResourceLocations("/views/");
    }
}

Then you could make also use of your mentioned Controller Class. The static Pages are accessible under /views/yourpage.html. That is the comparable solution like yours with /views/registration.html but a little more convenient way to serve static resources in spring.

sven.kwiotek
  • 1,459
  • 15
  • 22