在Spring中为CSS找到java NoHandler
我正在学习SpringMVC,并试图获得一些经验。我编写了一个应用程序并将其部署到Tomcat服务器。在运行时,浏览器获取CSS的404代码,Tomcat“警告[http-nio-8080-exec-12]org.springframework.web.servlet.DispatcherServlet.NoHandler未找到GET/SpringC5BuildingSpringWebApplication/resources/style.CSS的映射” 你能给我一些可能出错的提示吗? 下面是我的代码:
App initializer
public class SpittrWebAppInitializer extends AbstractAnnotationConfigDispatcherServletInitializer{
@Override
protected String[] getServletMappings() {
return new String[] {"/"};
}
@Override
protected Class<?>[] getRootConfigClasses() {
return new Class<?>[] {RootConfig.class};
}
@Override
protected Class<?>[] getServletConfigClasses() {
return new Class<?>[] {WebConfig.class};
}
}
WebConfig
@Configuration
@EnableWebMvc
@ComponentScan(basePackageClasses = HomeController.class)
class WebConfig extends WebMvcConfigurationSupport{
@Bean
public ViewResolver viewResolver(){
InternalResourceViewResolver resolver =
new InternalResourceViewResolver();
resolver.setPrefix("/WEB-INF/views/");
resolver.setSuffix(".jsp");
resolver.setExposeContextBeansAsAttributes(true);
return resolver;
}
@Override
protected void addResourceHandlers(ResourceHandlerRegistry registry) {
registry.addResourceHandler("/resources/**")
.addResourceLocations("/resources/");
}
}
Controller
@Controller
public class HomeController {
@RequestMapping(value = "/", method = RequestMethod.GET)
public String home(){
return "home";
}
}
Webpage
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<%@ page session="false" %>
<html>
<head>
<title>Spitter</title>
<link rel="stylesheet"
href="<c:url value="/resources/style.css" />" >
</head>
<body>
<h1>Welcome to Spitter</h1>
<a href="<c:url value="/spittles" />">Spittles</a> |
<a href="<c:url value="/spitter/register" />">Register</a>
</body>
</html>
WebXML
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee
http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-4.3.xsd"
version="3.0">
<session-config>
<session-timeout>
30
</session-timeout>
</session-config>
</web-app>
CSS文件被插入到许多地方,服务器将从某个地方读取它。任何提示都会对您有所帮助,我们将不胜感激!谢谢你,祝你身体健康
# 1 楼答案
问题出在网络配置中。班 我需要更改继承的实体。从
class WebConfig extends WebMvcConfigurationSupport
到public class WebConfig implements WebMvcConfigurer