在DispatcherServlet中找不到具有URI [/resources/css/styles.css]的HTTP请求的映射,名称为“SpringDispatcher”

时间:2015-09-12 19:09:18

标签: spring jsp spring-mvc

我正在使用Spring MVC开发简单的Web应用程序。你能否指出我访问它时为什么资源没有呈现在页面上?然而,整个页面正在呈现。

这是我的项目结构

enter image description here

我收到以下警告:

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

保存资源链接的JSP页面:

includes.jsp

<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ taglib prefix="fn" uri="http://java.sun.com/jsp/jstl/functions" %>
<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %>

<c:set var="base" value="${pageContext.request.contextPath}"/>

<link href="<c:url value="${base}/resources/css/pure/pure-min.css" />" rel="stylesheet">

<link href="<c:url value="${base}/resources/css/styles.css" />" rel="stylesheet">
<link href="<c:url value="${base}/resources/css/menu.css" />" rel="stylesheet">
<script src="${base}/resources/js/validateInput.js"></script>

必须呈现的JSP页面:

<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>

<html>
<head>
    <jsp:include page="/resources/includes.jsp"/>
</head>
<body>

<div id="main-container">
    <div class="site-content">

        <h1 class="fueling-header">Registration</h1>

        <form class="pure-form pure-form-aligned"
              action="/register"
              method="post"
              onsubmit="return validateUserInput();">



            </fieldset>
        </form>
    </div>
</div>
</body>
</html>

这是我的WebAppInitializer代码:

public class WebAppContextInitializer implements WebApplicationInitializer {

    @Override
    public void onStartup(ServletContext servletContext) throws ServletException {
        AnnotationConfigWebApplicationContext annotationConfigWebApplicationContext = new AnnotationConfigWebApplicationContext();
        annotationConfigWebApplicationContext.register(WebContextConfiguration.class);

        ServletRegistration.Dynamic dispatcher = servletContext.addServlet(
                "SpringDispatcher",new DispatcherServlet(annotationConfigWebApplicationContext)
        );
        dispatcher.setLoadOnStartup(1);
        dispatcher.addMapping("/");
    }
}

ContextConfiguration上课

@Configuration
@EnableWebMvc
@Import(ServiceContextConfiguration.class)
@ComponentScan("controllers")
public class WebContextConfiguration {

public void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler("/resources/**")
.addResourceLocations("/resources/");
}

    @Bean(name = "view_resolver")
    public InternalResourceViewResolver internalResourceViewResolver() {
        InternalResourceViewResolver internalResourceViewResolver = new InternalResourceViewResolver();

        internalResourceViewResolver.setPrefix("/WEB-INF/jsp/");
        internalResourceViewResolver.setSuffix(".jsp");

        return internalResourceViewResolver;
    }
}

我的简单控制器

@Controller
public class BasicController {
    @RequestMapping(value = "/greeting")
    public String sayBasic(Model model){
        model.addAttribute("greeting", "Hello world");
        return "register";
    }
}

2 个答案:

答案 0 :(得分:1)

问题是我在另一个控制器中有循环引用,即get方法和post被映射到相同的逻辑视图名称。我的配置是正确的。

对于那些遇到同样问题的人:删除所有请求映射并逐步添加它们,直到您看到问题发生的位置。

答案 1 :(得分:0)

删除$ {base},因为它添加了额外的/字符。