Mateus Vitali
Mateus Vitali

Reputation: 159

Spring MVC 4 with XHTML?

I read the guide of Spring MVC 4: http://docs.spring.io/spring-security/site/docs/4.0.x/guides/html5/form.html

And in this guide, he use html (pure) with thymeleaf:

<html xmlns:th="http://www.thymeleaf.org" xmlns:tiles="http://www.thymeleaf.org">
  <head>
    <title tiles:fragment="title">Messages : Create</title>
  </head>
  <body>
    <div tiles:fragment="content">
        <form name="f" th:action="@{/login}" method="post">               
            <fieldset>
                <legend>Please Login</legend>
                <div th:if="${param.error}" class="alert alert-error">    
                    Invalid username and password.
                </div>
                <div th:if="${param.logout}" class="alert alert-success"> 
                    You have been logged out.
                </div>
                <label for="username">Username</label>
                <input type="text" id="username" name="username"/>        
                <label for="password">Password</label>
                <input type="password" id="password" name="password"/>    
                <div class="form-actions">
                    <button type="submit" class="btn">Log in</button>
                </div>
            </fieldset>
        </form>
    </div>
  </body>
</html>

But if I try use this html code in my project, the browser is shown: "This XML file does not appear to have any style information associated with it. The document tree is shown below."

Why?

I use Spring MVC 4 with Java Config:

@Configuration
@EnableWebMvc
@ComponentScan(basePackages = "com.make3.mymed")
public class AppWebConfiguration extends WebMvcConfigurerAdapter {

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

    @Bean
    public InternalResourceViewResolver internalResourceViewResolver() {
        InternalResourceViewResolver resolver = new InternalResourceViewResolver();
        resolver.setPrefix("/WEB-INF/views/");
        resolver.setSuffix(".xhtml");
        return resolver;
    }

    @Override
    public void addViewControllers(ViewControllerRegistry registry) {
        registry.addViewController("/login").setViewName("login");
        registry.setOrder(Ordered.HIGHEST_PRECEDENCE);
    }
}

AbstractAnnotationConfigDispatcherServletInitializer:

public class SpringMVCServlet extends AbstractAnnotationConfigDispatcherServletInitializer {

    @Override
    protected Class<?>[] getRootConfigClasses() {
        return new Class[] { JPAConfiguration.class, SecurityConfiguration.class, AppWebConfiguration.class };
    }

    @Override
    protected Class<?>[] getServletConfigClasses() {
        return null;
    }

    @Override
    protected String[] getServletMappings() {
        return new String[] { "/" };
    }

}

Upvotes: 2

Views: 2906

Answers (1)

Alohci
Alohci

Reputation: 82976

You have served the page as XHTML, but your elements are not in the http://www.w3.org/1999/xhtml, so the browser does not recognise the elements as being the HTML elements of the same local name.

Since they are not the HTML elements they are not styled according to the HTML rules, and since you have provided no other styling, the only thing left for the browser to do is to apply the default styling for XML documents, which is a rendering of the document tree.

To fix, simply add the http://www.w3.org/1999/xhtml namespace as the default namespace to your <html> element. i.e.

<html xmlns="http://www.w3.org/1999/xhtml" 
      xmlns:th="http://www.thymeleaf.org"
      xmlns:tiles="http://www.thymeleaf.org">

Upvotes: 1

Related Questions