我正在使用Spring-MVC4开发一个Web应用程序 我将DispatcherServlet配置为处理所有'/'请求,如下所示(web.xml):
<servlet>
<servlet-name>WebAppConfig</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>
com.xxx.config
</param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>WebAppConfig</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
我的目录结构如下图所示: project directory structure
我的jsp文件如下:
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<link type="text/css" rel="stylesheet"
href="/resources/css/Style.css" media="screen" />
<title>Insert title here</title>
</head>
<body>
<c:import url="/resources/common/menu.jsp" />
<h1>Home/Welcome page update</h1>
<p>
${message}
</p>
</body>
</html>
最后我的webapp配置是:
@Configuration
@ComponentScan(value = { "com.xxx" })
@EnableWebMvc
@EnableTransactionManagement
@PropertySource("classpath:/app.properties")
public class WebAppConfig extends WebMvcConfigurerAdapter {
@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
registry.addResourceHandler("/resources/**").addResourceLocations("/resources/");
}
@Override
public void configureDefaultServletHandling(DefaultServletHandlerConfigurer configurer) {
configurer.enable();
}
}
当我将http请求发送到服务器时,我可以在/ resources / common / menu.jsp中加载jsp文件,但我无法处理css文件! 当我发送一个http请求来获取像这样http://localhost:8080/resources/css/Style.css的css文件时,我得到404 - Not Found错误 有解决方案吗?
答案 0 :(得分:0)
当我用
调用css文件时,问题出现在jsp文件中<link type="text/css" rel="stylesheet"
href="/resources/css/Style.css" media="screen" />
正确的href是:href="<c:url value='/resources/css/Style.css' />"