我正在尝试使用java配置的Spring,并且在静态资源中找不到404,我可以确认的文件夹是被发布到服务器上。资源似乎根本没有得到解决。因此,即使在调用localhost:8080/SpringMVC/resources/js/jquery-1.4.2.min.js时,也没有找到404。我正在使用Spring4.0.1
部署结构
SpringMVC/
resources/
css/
js/
jquery-1.4.2.min.js
WEB-INF/
classes/
lib/
views/
services.jsp
web.xml我的守则:
com/me/config/WebApplicationConfig.java
@Configuration
@EnableWebMvc
@ComponentScan(basePackages = {"com.me.services"})
public class WebApplicationConfig extends WebMvcConfigurerAdapter {
private static final String VIEW_RESOLVER_SUFFIX = ".jsp";
private static final String VIEW_RESOLVER_PREFIX = "/WEB-INF/views/";
@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
registry.addResourceHandler("/resources/**").addResourceLocations("/resources/");
}
@Bean
public ViewResolver setupViewResolver() {
InternalResourceViewResolver resolver = new InternalResourceViewResolver();
resolver.setPrefix(VIEW_RESOLVER_PREFIX);
resolver.setSuffix(VIEW_RESOLVER_SUFFIX );
resolver.setViewClass(JstlView.class);
return resolver;
}
}com/me/config/Initializer.java
public class Initializer implements WebApplicationInitializer {
private static final String DISPATCHER_SERVLET_MAPPING = "/";
private static final String DISPATCHER_SERVLET_NAME = "dispatcher";
public void onStartup(ServletContext servletContext) throws ServletException {
AnnotationConfigWebApplicationContext rootContext = new AnnotationConfigWebApplicationContext();
rootContext.register(WebApplicationConfig.class);
rootContext.setServletContext(servletContext);
Dynamic servlet = servletContext.addServlet(DISPATCHER_SERVLET_NAME, new DispatcherServlet(rootContext));
servlet.addMapping(DISPATCHER_SERVLET_MAPPING );
servlet.setLoadOnStartup(1);
}
}com/me/controller/ServicesController.java
@Controller
public class ServicesController {
@RequestMapping(value = "/", method = RequestMethod.GET)
public String services(ModelMap model) {
return "services";
}
}WEB/views/services.jsp
<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<html>
<head>
<script src="<c:url value="/resources/js/jquery-1.4.2.min.js" />"></script>
</head>
<body>
<h2>Services</h2>
</body>
</html>调用localhost时获得请求响应:8080/SpringMVC/:
<html>
<head>
<script src="/SpringMVC/resources/js/jquery-1.4.2.min.js"></script>
</head>
<body>
<h2>Services</h2>
</body>
</html>在此之后,404没有出现在:
localhost:8080/SpringMVC/resources/js/jquery-1.4.2.min.js发布于 2014-02-25 11:45:34
我想在你的services.jsp里,你能试试单引号吗:
<script src="<c:url value='/resources/js/jquery-1.4.2.min.js' />"></script>在<script>中使用2组双引号似乎是个问题。
https://stackoverflow.com/questions/22013074
复制相似问题