Spring 带有 XHTML 的 MVC 4?
Spring MVC 4 with XHTML?
我阅读了 Spring MVC 4 的指南:
http://docs.spring.io/spring-security/site/docs/4.0.x/guides/html5/form.html
在本指南中,他使用 html(纯)和百里香叶:
<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>
但是如果我尝试在我的项目中使用此 html 代码,浏览器将显示:
"This XML file does not appear to have any style information associated with it. The document tree is shown below."
为什么?
我使用 Spring MVC 4 和 Java 配置:
@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[] { "/" };
}
}
您已将页面作为 XHTML 提供,但您的元素不在 http://www.w3.org/1999/xhtml 中,因此浏览器无法将这些元素识别为 HTML 元素相同的本地名称。
因为它们不是 HTML 元素,所以它们没有根据 HTML 规则设置样式,并且由于您没有提供其他样式,浏览器唯一要做的就是应用 XML 文档的默认样式,这是文档树的渲染。
要修复,只需将 http://www.w3.org/1999/xhtml 命名空间作为默认命名空间添加到您的 <html>
元素。即
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:th="http://www.thymeleaf.org"
xmlns:tiles="http://www.thymeleaf.org">
我阅读了 Spring MVC 4 的指南: http://docs.spring.io/spring-security/site/docs/4.0.x/guides/html5/form.html
在本指南中,他使用 html(纯)和百里香叶:
<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>
但是如果我尝试在我的项目中使用此 html 代码,浏览器将显示: "This XML file does not appear to have any style information associated with it. The document tree is shown below."
为什么?
我使用 Spring MVC 4 和 Java 配置:
@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[] { "/" };
}
}
您已将页面作为 XHTML 提供,但您的元素不在 http://www.w3.org/1999/xhtml 中,因此浏览器无法将这些元素识别为 HTML 元素相同的本地名称。
因为它们不是 HTML 元素,所以它们没有根据 HTML 规则设置样式,并且由于您没有提供其他样式,浏览器唯一要做的就是应用 XML 文档的默认样式,这是文档树的渲染。
要修复,只需将 http://www.w3.org/1999/xhtml 命名空间作为默认命名空间添加到您的 <html>
元素。即
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:th="http://www.thymeleaf.org"
xmlns:tiles="http://www.thymeleaf.org">