【问题标题】:Why jsp cannot reach its css file in SpringMVC?为什么jsp在Spring MVC中无法访问其css文件?
【发布时间】:2014-05-19 06:50:20
【问题描述】:

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

No mapping found for HTTP request with URI [/myproject/resources/css/welcome.css] in DispatcherServlet with name 'DispatcherServlet'

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 file:

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
public class WelcomeController {

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

这是我的项目结构:

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

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

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

【问题讨论】:

  • 试试href="${pageContext.request.contextPath}/resources/welcome.css" 让我们知道
  • 抱歉,您的变体不起作用。我的文件夹结构是src/main/webapp/resources/css/welcome.css
  • 然后尝试检查您提供路径的天气是否表明该文件未尝试(f.exits)如果这是真的那么文件正在获取路径
  • 不要把你的jsp文件放到WEB-INF里!它们应该在src/main/webapp 文件夹中。
  • @kromit 为什么?我读的正好相反。在 WEB-INF 文件夹中,.jsp 文件不能从客户端访问,而在其他文件夹中它们是。我的项目结构还可以,有很多教程都是一样的,我想可能是我的映射错了。

标签: spring jakarta-ee spring-mvc


【解决方案1】:

通过使用 Java 配置:

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

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

  3. 为公共资源定义一个处理程序:

    @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. 添加安全例外:

    @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>
    

【讨论】:

  • 这看起来比我想象的要复杂,为什么?看看mkyong.com/spring-mvc/…
  • 如果你使用 Spring 和 Java Config,它代表了一种可靠的方式来设置你的应用程序。
  • 试过了,没用。 welcome.css 文件未应用。
  • 你做错了。上面的解决方案在企业项目上得到了很好的测试(就像现在一样)。
【解决方案2】:

@vdenotaris 答案是正确的

由于我的pom.xml 文件设置,我的配置不起作用。 maven-war-plugin 有&lt;warSourceDirectory&gt;WebContent&lt;/warSourceDirectory&gt;,而我的/src/main/webapp/resources/css 从未被maven 检测到。一旦我将我的 jsp 文件移动到 /src/main/webapp/ 一切都很好。

所以,要么将所有内容(css 资源和其他内容)留在 WebContent\WEB-INF\ 文件夹中,要么将所有内容移至 /src/main/webapp

【讨论】:

    猜你喜欢
    • 2013-12-06
    • 1970-01-01
    • 1970-01-01
    • 2015-05-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-05-23
    相关资源
    最近更新 更多