vendredi 9 octobre 2015

Thymeleaf + Spring MVC project error

As a newbie in web development, I have a problem with my Thymeleaf + Spring MVC based webb application.

HTTP Status 500 - Request processing failed; nested exception is org.thymeleaf.exceptions.TemplateInputException: Exception parsing document: template="index", line 11 - column 3

The problem is I get an HTTP 500 error from my Tomcat container when I try to add some stylesheet or script ref in head tag in my html file. Without that, It works fine.

index.html:

         <!DOCTYPE html>
    <html xmlns="http://ift.tt/lH0Osb"
          xmlns:th="http://ift.tt/wfNV60">
    <head th:lang="en">

        <title>webpage</title>
<!--the line below causes an error-->        
<link rel="stylesheet" type="text/css" th:href="@{style.css}"> 
    </head>
    <body>
    <div>
        <h1> Hello</h1>
        <a th:href="@{/staticPage}">Click</a>
    </div>
    </body>
    </html>

I have xml based Thymeleaf config - thymeleaf-config.xml:

    <?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://ift.tt/GArMu6"
       xmlns:xsi="http://ift.tt/ra1lAU"
       xmlns:p="http://ift.tt/1jdM0fE"
       xsi:schemaLocation="http://ift.tt/GArMu6
 http://ift.tt/GAf8ZW">

    <bean id="viewResolver"
          class="org.thymeleaf.spring4.view.ThymeleafViewResolver"
          p:templateEngine-ref="templateEngine"/>

    <bean id="templateEngine"
          class="org.thymeleaf.spring4.SpringTemplateEngine"
          p:templateResolver-ref="templateResolver"/>

    <bean id="templateResolver" class="org.thymeleaf.templateresolver.ServletContextTemplateResolver"
          p:prefix="/WEB-INF/templates/"
          p:suffix=".html"
          p:templateMode="HTML5" />

</beans>

web.xml:

<web-app id="WebApp_ID" version="2.4"
     xmlns="http://ift.tt/qzwahU"
     xmlns:xsi="http://ift.tt/ra1lAU"
     xsi:schemaLocation="http://ift.tt/qzwahU
http://ift.tt/16hRdKA">

<display-name>Spring Page Redirection</display-name>

<servlet>
    <servlet-name>springDispatcherServlet</servlet-name>
    <servlet-class>
        org.springframework.web.servlet.DispatcherServlet
    </servlet-class>
    <init-param>
        <param-name>contextClass</param-name>
        <param-value>org.springframework.web.context.support.AnnotationConfigWebApplicationContext</param-value>
    </init-param>
    <init-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>config.WebConfig</param-value>
    </init-param>
    <load-on-startup>1</load-on-startup>
</servlet>

<servlet-mapping>
    <servlet-name>springDispatcherServlet</servlet-name>
    <url-pattern>/</url-pattern>
</servlet-mapping>

pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://ift.tt/IH78KX"
         xmlns:xsi="http://ift.tt/ra1lAU"
         xsi:schemaLocation="http://ift.tt/IH78KX http://ift.tt/VE5zRx">
    <modelVersion>4.0.0</modelVersion>

    <groupId>myproject</groupId>
    <artifactId>myproject</artifactId>
    <version>1.0-SNAPSHOT</version>
    <packaging>war</packaging>


    <dependencies>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-core</artifactId>
            <version>4.2.1.RELEASE</version>
        </dependency>

        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version>4.2.1.RELEASE</version>
        </dependency>

        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-beans</artifactId>
            <version>4.2.1.RELEASE</version>
        </dependency>

        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-webmvc</artifactId>
            <version>4.2.1.RELEASE</version>
        </dependency>


        <dependency>
            <groupId>org.springframework.data</groupId>
            <artifactId>spring-data-jpa</artifactId>
            <version>1.9.0.RELEASE</version>
        </dependency>

        <dependency>
            <groupId>org.hibernate</groupId>
            <artifactId>hibernate-core</artifactId>
            <version>5.0.2.Final</version>
        </dependency>

        <dependency>
            <groupId>org.apache.maven</groupId>
            <artifactId>maven-plugin-api</artifactId>
            <version>3.3.3</version>
        </dependency>

        <dependency>
            <groupId>org.thymeleaf</groupId>
            <artifactId>thymeleaf</artifactId>
            <version>2.1.4.RELEASE</version>
        </dependency>


        <dependency>
            <groupId>org.thymeleaf</groupId>
            <artifactId>thymeleaf-spring4</artifactId>
            <version>2.1.4.RELEASE</version>
        </dependency>


    </dependencies>

    <build>
        <finalName>myproject</finalName>
    </build>

</project>

I also have Java config class WebConfig to add static resources

@Configuration
@EnableWebMvc
@ComponentScan(basePackages = "app")
@ImportResource("WEB-INF/thymeleaf-config.xml")
public class WebConfig extends WebMvcConfigurerAdapter{



    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry){
        registry.addResourceHandler("/content/**").addResourceLocations("/static/");
    }

    @Override
    public void configureDefaultServletHandling(DefaultServletHandlerConfigurer configurer){
        configurer.enable();
    }


}

My /templates directory and thymeleaf-config.xml file are located in webapp/WEB-INF/

As I said, an error occurs when I try to put something in head tag of my thymeleaf's index.html. Maybe I did something wrong with my configuration.




Aucun commentaire:

Enregistrer un commentaire