1

I am developing simple web application using Spring MVC. Could you please point me out why the resources are not rendered on the page when I am accessing it? The whole page is being rendered however.

Here my project structure

enter image description here

I am receiving the following warning:

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

JSP page that holds the links to the resources:

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 page that has to be rendered:

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

Here is my WebAppInitializer code:

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 class

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

And my simple controller

@Controller
public class BasicController {
    @RequestMapping(value = "/greeting")
    public String sayBasic(Model model){
        model.addAttribute("greeting", "Hello world");
        return "register";
    }
}
BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
mr.M
  • 851
  • 6
  • 23
  • 41

2 Answers2

1

The problem was that I had the cyclic reference in another controller i.e. the get method and post were mapped to the same logical view name. My configuration was correct.

For those who will meet the same problem: remove all request mappings and add them step by step till you will see where the problem occurred.

mr.M
  • 851
  • 6
  • 23
  • 41
0

Remove ${base} as it is adding additional / Character.

Vasu
  • 21,832
  • 11
  • 51
  • 67