spring之为什么 jsp 无法在 Spring MVC 中访问它的 css 文件

lovecherry 阅读:132 2025-02-15 21:57:57 评论:0

我有一个 SpringMVC 项目。在 tomcat7 中运行项目后,我收到以下消息:

在名为“DispatcherServlet”的 DispatcherServlet 中未找到具有 URI [/myproject/resources/css/welcome.css] 的 HTTP 请求的映射

没有应用 CSS:

我已经尝试过可用的解决方案,但没有一个有效,也没有一个真正解释了发生了什么。

我在 /WEB-INF/jsp 中有一个简单的 welcome.jsp。在这里:

<html> 
<head> 
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> 
<link type="text/css" 
    href="${pageContext.request.contextPath}/resources/css/welcome.css" 
    rel="stylesheet"> 
<title>Welcome!</title> 
</head> 
<body> 
    <div id="font-HarabaraHand"> 
        <p class="centrify">Poem Collection</p> 
    </div> 
    <div id="font-Goodfoot"> 
        <p class="centrify2">What would you like to do?</p> 
    </div> 
 
</body> 
</html> 

我的welcome.css 文件位于src/main/webapp/resources/css/。 我的 Spring 配置是基于 java 的,DispatcherServlet 类在 AppInit.java 文件中:

public class AppInit implements WebApplicationInitializer { 
    // this method is automatically invoked on application startup 
    public void onStartup(ServletContext container) throws ServletException { 
        WebApplicationContext webcntx = getWebCntx(); 
 
        container.addListener(new ContextLoaderListener(webcntx)); 
 
        ServletRegistration.Dynamic dispatcher = container.addServlet( 
                "DispatcherServlet", new DispatcherServlet(webcntx)); 
 
        dispatcher.setLoadOnStartup(1); 
        dispatcher.addMapping("/"); 
    } 
 
    private AnnotationConfigWebApplicationContext getWebCntx() { 
        AnnotationConfigWebApplicationContext context = new AnnotationConfigWebApplicationContext(); 
        context.register(WebAppConfiguration.class); 
        return context; 
    } 
} 

最后在 WebAppConfig.java 文件中:

@Bean 
    public InternalResourceViewResolver getInternalResourceViewResolver() { 
        InternalResourceViewResolver resolver = new InternalResourceViewResolver(); 
        resolver.setPrefix("/WEB-INF/jsp/"); 
        resolver.setSuffix(".jsp"); 
        resolver.setViewClass(JstlView.class); 
        return resolver; 
    } 
 
@Override 
public void addResourceHandlers(ResourceHandlerRegistry registry) { 
    super.addResourceHandlers(registry); 
    if (!registry.hasMappingForPattern("/resources/**")) { 
        registry.addResourceHandler("/resources/**").addResourceLocations( 
                "/resources/").setCachePeriod(CACHE_PERIOD);; 
 
    } 
} 

我的欢迎页面 Controller :

@Controller 
public class WelcomeController { 
 
    @RequestMapping(value="/") 
    public ModelAndView welcomePage() { 
        return new ModelAndView("welcome"); 
    } 
} 

这是我的项目结构:

<强> 1。为什么无法访问我的 welcome.css 文件?

<强> 2。如何更正我的映射配置?

我非常感谢有关 Spring url 映射配置的解释或链接。

请您参考如下方法:

通过使用 Java 配置:

  1. 创建如下子目录结构:src/main/webapp/public/css

  2. 将您的 css 文件放入 src/main/webapp/public/css(例如 src/main/webapp/public/css/welcome.css)

  3. 定义公共(public)资源的处理程序:

    @Override 
    public void addResourceHandlers(ResourceHandlerRegistry registry) { 
        super.addResourceHandlers(registry); 
        if (!registry.hasMappingForPattern("/public/**")) { 
            registry.addResourceHandler("/public/**") 
                    .addResourceLocations("/public/") 
                    .setCachePeriod(CACHE_PERIOD); // You have to define a cache period. 
        } 
    } 
    
  4. 添加安全异常(exception):

    @Override 
    public void configure(WebSecurity web) throws Exception { 
        web.ignoring().antMatchers("/public/**"); 
    } 
    
  5. 链接你的 css 文件(通过定义相对路径):

    <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %> 
    <%@ taglib prefix="fn" uri="http://java.sun.com/jsp/jstl/functions" %> 
    ... 
    <head> 
        <c:set var="url">${pageContext.request.requestURL}</c:set> 
        <base href="${fn:substring(url, 0, fn:length(url) - fn:length(pageContext.request.requestURI))}${pageContext.request.contextPath}/" /> 
        <link rel="stylesheet" href="public/css/welcome.css" /> 
    </head> 
    


标签:Spring
声明

1.本站遵循行业规范,任何转载的稿件都会明确标注作者和来源;2.本站的原创文章,请转载时务必注明文章作者和来源,不尊重原创的行为我们将追究责任;3.作者投稿可能会经我们编辑修改或补充。

关注我们

一个IT知识分享的公众号