我一直在寻找stackoverflow和其他站点来解决我的问题,但没有找到有效的答案。
我试图用CSS加载页面,但会抛出
获取http://localhost:8080/resources/css/myCss.css净值:: ERR_ABORTED 404
我添加了资源处理程序,但是它没有解决问题。
AppConfig.java
@Configuration
@EnableWebMvc
@ComponentScan(basePackages = "com.hawwaru")
public class AppConfig extends WebMvcConfigurationSupport {
@Autowired
private Environment env;
private Logger logger = Logger.getLogger(getClass().getName());
@Bean
public ViewResolver viewResolver() {
InternalResourceViewResolver viewResolver = new
InternalResourceViewResolver();
viewResolver.setPrefix("/WEB-INF/");
viewResolver.setSuffix(".jsp");
return viewResolver;
}
private static final String[] CLASSPATH_RESOURCE_LOCATIONS = {
"/resources/",
"classpath:/META-INF/resources/", "classpath:/resources/",
"classpath:/static/", "classpath:/public/" };
@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
registry
.addResourceHandler("/resources/**")
.addResourceLocations(CLASSPATH_RESOURCE_LOCATIONS);
}
}
AppController.java
@Controller
public class AppController {
@GetMapping("/")
public String showHome(Model theModel) {
return "moviepage";
}
}
调度程序
public class SpringMvcDispatcherInitializer extends AbstractAnnotationConfigDispatcherServletInitializer {
@Override
protected Class<?>[] getRootConfigClasses() {
return null;
}
@Override
protected Class<?>[] getServletConfigClasses() {
return new Class[] {AppConfig.class};
}
@Override
protected String[] getServletMappings() {
return new String[] {"/"};
}
moviepage.jsp
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<!DOCTYPE html>
<html lang="en" xmlns="http://www.w3.org/1999/xhtml">
<head>
<link rel="stylesheet" type="text/css" href="<c:url value="${pageContext.request.contextPath}/resources/css/myCss.css"/>">
<meta charset="utf-8" name="viewport" content="width=device-width, initial-scale=1" />
</head>
<body>
...
</body>
</html>
这也是我的项目树结构: project tree