Problem with the context in Spring MVC project and URL's


I'm a Java developer but it's the first time that I have to create a web project from 0 and choose the technologies to use. I've launched Spring MVC because it's what I've used the most but now that I'm doing and configuring the project in NetBeans I've run into a lot of headaches, here I show one so I hope you can help me.

My project is called AdminConsole and when I created it I put the resources (.css .js .img, etc) within the web folder. From the jsp I refer to those resources so "href = 'img / image.jpg'" I even have Spring MVC configured by annotations so that I always enter each request by a controller, and each request I make it like this "form action = '/ processForm '"and it worked for me.

One day with absolutely nothing to do, I arrive at work and no longer detects the context. ( I had already passed but always created a new project ).

Now the way for it to work is by adding the context in each URL so that it can call resources like css or img as for spring requests. example $ {contextPath} /img/image.jpg or $ {contextPath} / procesForm.

The browser shows the error that the path for those files link etc. can not be found. when before there was no problem.

Could you please help me? Below I show the most relevant files of my project.

This is the folder structure that I have:

This is my web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="3.0" xmlns="" xmlns:xsi="" xsi:schemaLocation="">


<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="" 
       xsi:schemaLocation="                      ">
        Most controllers will use the ControllerClassNameHandlerMapping above, but
        for the index controller we are using ParameterizableViewController, so we must
        define an explicit mapping for it.
    <bean id="viewResolver"
          p:suffix=".jsp" />
    <!-- Especificamos el o los paquetes donde estan los controllers -->
    <context:component-scan base-package="com.controller" />
    <mvc:annotation-driven /> <!-- Especificamos que los controllers funcionaran con anotaciones -->
    <mvc:default-servlet-handler />   <!--Especificamos que Spring detecte el servlet por default para la carga inicial -->
    <bean id="messageSource" class="">
        <property name="basename" value="/WEB-INF/messages" /> <!-- Ubucacion de los archivos .properties con los labels -->
        <property name="defaultEncoding" value="UTF-8" />
    <bean id="localeResolver" class="org.springframework.web.servlet.i18n.CookieLocaleResolver">
        <property name="defaultLocale" value="en" /> <!-- Idioma por Default -->      
        <bean class="org.springframework.web.servlet.i18n.LocaleChangeInterceptor">
            <property name="paramName" value="locale" /> <!-- Variable para interceptar el cambio de idioma -->



@RequestMapping(value = "/", method = RequestMethod.GET)
public ModelAndView login(){ //Metodo que se ejecuta antes de cargar el Login            
    ModelAndView model = new ModelAndView();            

    return model;


    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta name="description" content="Creative - Bootstrap 3 Responsive Admin Template">
    <meta name="author" content="GeeksLabs">
    <meta name="keyword" content="Creative, Dashboard, Admin, Template, Theme, Bootstrap, Responsive, Retina, Minimal">
    <link rel="shortcut icon" href="img/favicon.ico">
    <!-- Bootstrap CSS -->    
    <link href="css/bootstrap.min.css" rel="stylesheet">
    <!-- bootstrap theme -->
    <link href="css/bootstrap-theme.css" rel="stylesheet">
    <!--external css-->
    <!-- font icon -->
    <link href="css/elegant-icons-style.css" rel="stylesheet" />
    <link href="css/font-awesome.css" rel="stylesheet" />
    <!-- Custom styles -->
    <link href="css/style.css" rel="stylesheet">
    <link href="css/style-responsive.css" rel="stylesheet" /> 
    <script src="js/jquery-1.8.3.min.js" type="text/javascript"></script>
    <!-- HTML5 shim and Respond.js IE8 support of HTML5 -->
    <!--[if lt IE 9]>
    <script src="js/html5shiv.js"></script>
    <script src="js/respond.min.js"></script>
    <title>Admin Console</title>

I think the error must be that I have wrong configured the web.xml since I have it so that it captures all the requests and passes them through a controller, but I do not know why, without moving anything, it works and without more, I have already Several projects that I create new every time I make this mistake.

I will be eternally grateful for your help.

asked by Rick Nuñez 31.05.2016 в 17:46

1 answer


For the URL that you have commented ( link ) it seems that you have modified your web server to serve your application from the root omitting the name of the application. This can affect your links if you have not been building them correctly. Precisely to avoid these problems, it is good practice to always use contextPath in the construction of your urls or failing

<c:url value="/url_relativa"/>

Likewise, I would advise that if you use spring-mvc, let him serve the static content more efficiently, including in your dispatcher-servlet.xml

 <mvc:resources mapping="/css/**" location="/css/" />

I hope it serves you

answered by 06.07.2016 в 12:24