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

0

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="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd">
    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>/WEB-INF/applicationContext.xml</param-value>
    </context-param>
    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>
    <servlet>
        <servlet-name>dispatcher</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <load-on-startup>2</load-on-startup>
    </servlet>
    <servlet-mapping>
        <servlet-name>dispatcher</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping>
    <session-config>
        <session-timeout>
            30
        </session-timeout>
    </session-config>
    <welcome-file-list>
        <welcome-file>/</welcome-file>        
    </welcome-file-list>
</web-app>

dispatcher-servlet.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans" 
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
       xmlns:p="http://www.springframework.org/schema/p" 
       xmlns:aop="http://www.springframework.org/schema/aop" 
       xmlns:tx="http://www.springframework.org/schema/tx"        
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:util="http://www.springframework.org/schema/util"   
       xmlns:mvc="http://www.springframework.org/schema/mvc"
       xsi:schemaLocation="http://www.springframework.org/schema/beans 
        http://www.springframework.org/schema/beans/spring-beans-4.0.xsd         
        http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd        
        http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd        
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd
        http://www.springframework.org/schema/util http://www.springframework.org/schema/context/spring-util-4.0.xsd
        http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd">
            
    <!--
        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"
          class="org.springframework.web.servlet.view.InternalResourceViewResolver"
          p:prefix="/WEB-INF/pages/"
          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 -->
          
    <!-- INTERNACIONALIZACION -->
    <bean id="messageSource" class="org.springframework.context.support.ReloadableResourceBundleMessageSource">
        <property name="basename" value="/WEB-INF/messages" /> <!-- Ubucacion de los archivos .properties con los labels -->
        <property name="defaultEncoding" value="UTF-8" />
    </bean>
    <bean id="localeResolver" class="org.springframework.web.servlet.i18n.CookieLocaleResolver">
        <property name="defaultLocale" value="en" /> <!-- Idioma por Default -->      
    </bean>
    <mvc:interceptors>
        <bean class="org.springframework.web.servlet.i18n.LocaleChangeInterceptor">
            <property name="paramName" value="locale" /> <!-- Variable para interceptar el cambio de idioma -->
        </bean>
    </mvc:interceptors>

</beans>

Controller:

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

    return model;
}

JSP:

<head>
    <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>
    <![endif]-->
    <title>Admin Console</title>
</head>

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
source

1 answer

1

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