Masood Ahmad
Masood Ahmad

Reputation: 741

images, scripts and resources link path not loaded using spring and tomcat

Status: solved.

Here is my directory structure.

enter image description here

and other dir structure and structure of .war file on right side.

enter image description here


Edit 1 start

I just researched and saw my other projects. its not a problem of tomcat or jsp. I did similar in earlier projects. The only difference was that I included spring and maven this time and I had jsps, images, scripts outside the WEB-INF dir. but in the web dir

(but I may be wrong)

I do also get WARNING: No mapping found for HTTP request with URI [/ttmaven/resources/images/person.png] in DispatcherServlet with name 'springDispatcher'

I think it may be a problem of filters or url patterns. http://localhost:8084/ttmaven/resources/images/person.png passes the request to a servlet and that servlet does not handles it correctly?

Edit 1 end


I am using spring and controllers.

E.g localhost/appName/login gives me WEB-INF/view/jsp/login/login.jsp

prefix is /view/jsp.

I have really tried every possible combination to link up the images and .css files in my jsps. For example in login.jsp.

I always get 404 error.

Even for a direct link e.g ttmaven as my appname. http://localhost:8084/ttmaven/resources/images/person.png dont work

What is the proper way to link? and if I used spring security too, how to allow the resources/** for all to access.

My related questions

pageContext.request.contextPath and generic linking

generic linking, variables and paths in jsp

web.xml (I commented out spring security things to be sure that its not spring security permissions which is making problems)

<?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">

    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>
    <servlet>
        <servlet-name>springDispatcher</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <load-on-startup>1</load-on-startup>
    </servlet>
    <servlet-mapping>
        <servlet-name>springDispatcher</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping>
    <session-config>
        <session-timeout>
            30
        </session-timeout>
    </session-config>
    <welcome-file-list>
        <welcome-file>redirect.jsp</welcome-file>
    </welcome-file-list>



<!--    <filter>
        <filter-name>springSecurityFilterChain</filter-name>
        <filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class>
    </filter>

    <filter-mapping>
        <filter-name>springSecurityFilterChain</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>-->

    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>
<!--            /WEB-INF/spring-security.xml-->
            /WEB-INF/applicationContext.xml
        </param-value>
    </context-param>
</web-app>

applicationContext.xml mainly only has:

 <import resource="springDispatcher-servlet.xml" />

springDispatcher-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:mvc="http://www.springframework.org/schema/mvc"
  xmlns:context="http://www.springframework.org/schema/context"
  xmlns:tx="http://www.springframework.org/schema/tx"
  xsi:schemaLocation="
        http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd
        http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd
        http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.2.xsd"
>


    <context:component-scan base-package="web" >
    <context:include-filter type="annotation" expression="org.springframework.stereotype.Service"       />
        <context:include-filter type="annotation" expression="org.springframework.stereotype.Repository"    />
    </context:component-scan> 
  <mvc:annotation-driven />
<!--  <context:annotation-config />-->
    <bean class="org.springframework.web.servlet.mvc.support.ControllerClassNameHandlerMapping"/>

    <!--
    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="urlMapping" class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
        <property name="mappings">
            <props>
                <prop key="index.htm">indexController</prop>
            </props>
        </property>
    </bean>

    <bean id="viewResolver"
                class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<!--          class="org.springframework.web.servlet.view.InternalResourceViewResolver">-->

<!--             class="org.springframework.web.servlet.view.UrlBasedViewResolver" >-->
          <property name="prefix" value="/WEB-INF/view/jsp/" />
            <property name="suffix" value=".jsp" />
    </bean>        

    <!--
    The index controller.
    -->
    <bean name="indexController"
          class="org.springframework.web.servlet.mvc.ParameterizableViewController">
          <property name="viewName" value="index" />
    </bean>



     <!---
     ##########################################################################
    Hibernate

    -->
<!--    <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
        <property name="driverClassName" value="org.postgresql.Driver" />
        <property name="url" value="${jdbc.url}jdbc:postgresql://localhost:5432/postgres" />
        <property name="username" value="postgres" />
        <property name="password" value="abc" />
    </bean>

    <bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
        <property name="dataSource" ref="dataSource"/>
        <property name="packagesToScan" value="web.entity" />
        </property>
        <property name="hibernateProperties">
            <props>
                <prop key="hibernate.dialect">org.hibernate.dialect.PostgreSQLDialect</prop>
                <prop key="hibernate.show_sql">true</prop>
                <prop key="cache.provider_class">org.hibernate.cache.internal.NoCacheProvider</prop>
            </props>

        </property>
    </bean>

    <tx:annotation-driven transaction-manager="transactionManager"/>

    <bean id="transactionManager" class="org.springframework.orm.hibernate4.HibernateTransactionManager">
        <property name="sessionFactory" ref="sessionFactory"/>
    </bean>

    ###########################################################
    -->


<!--

    JPA based instead of hibernate

-->
        <bean class="org.springframework.orm.jpa.support.PersistenceAnnotationBeanPostProcessor"/>
<!-- 
        This configures the EntityManagerFactory object used for JPA/Spring managed persistent objects. 
     -->
    <bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
        <property name="persistenceXmlLocation" value="classpath*:META-INF/persistence.xml" />
        <property name="persistenceUnitName" value="persistence-unit-demo" /> 
        <property name="dataSource" ref="dataSource" />
                <property name="packagesToScan" value="web.entity" />
        <property name="jpaVendorAdapter">
            <bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter">
                                <property name="database" value="POSTGRESQL" />
<!--    giving errors       <property name="databasePlatorm" value="org.hibernate.dialect.PostgreSQLDialect"/>-->
<!--                                <property name="database" value="HSQL" />-->  
                <property name="showSql" value="true" />
                <property name="generateDdl" value="true" />                        
            </bean>
        </property>   
                <property name="jpaProperties">
                    <props>
<!--                        <prop key="hibernate.hbm2ddl.auto">create-drop</prop>-->
                            <prop key="hibernate.hbm2ddl.auto">update</prop>
                    </props>
                </property>                         
    </bean> 
    <bean id="jpaDialect" class="org.springframework.orm.jpa.vendor.HibernateJpaDialect" />
    <!-- Pulls database connection from the tomcat container's context database pool via JNDI -->
<!--    <jee:jndi-lookup id="dataSource" jndi-name="jdbc/mssqlserver" resource-ref="true"/>-->

        <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource" >
            <property name="driverClassName" value="org.postgresql.Driver" />
            <property name="url" value="jdbc:postgresql://localhost:5432/postgres" />
            <property name="username" value="postgres" />
            <property name="password" value="abc" />
        </bean>



    <!--
        Sets up our transaction manager. 
     -->
    <bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
        <property name="entityManagerFactory" ref="entityManagerFactory" />
        <property name="jpaDialect" ref="jpaDialect" />
        <property name="dataSource" ref="dataSource" />
<!-- giving errors               <property name="loadTimeWeaver">
                    <bean class="org.springframework.instrument.classloading.InstrumentationLoadTimeWeaver" />
                </property>-->
    </bean>

        <!--
        Defines our transaction manager for Transactional annotations.
     -->
    <tx:annotation-driven transaction-manager="transactionManager" />

    <bean id="sessionFactory" factory-bean="entityManagerFactory" factory-method="getSessionFactory" />




        <!--Internationalization ########################## -->

        <bean id="localeResolver"
        class="org.springframework.web.servlet.i18n.SessionLocaleResolver">
        <property name="defaultLocale" value="en" />
    </bean>

    <bean id="localeChangeInterceptor"
        class="org.springframework.web.servlet.i18n.LocaleChangeInterceptor">
        <property name="paramName" value="language" />
    </bean>

    <bean class="org.springframework.web.servlet.mvc.support.ControllerClassNameHandlerMapping" >
        <property name="interceptors">
           <list>
            <ref bean="localeChangeInterceptor" />
           </list>
        </property>
    </bean>



    <!-- Register the welcome.properties -->
    <bean id="messageSource"
        class="org.springframework.context.support.ResourceBundleMessageSource">
        <property name="basename" value="messages" />
    </bean>


</beans>

Upvotes: 0

Views: 7235

Answers (2)

Masood Ahmad
Masood Ahmad

Reputation: 741

Found the solution: just had to add <mvc:resources mapping="/resources/**" location="/resources/" /> in springDispatcher xml

Helpful link:

Not displaying images in Spring MVC http://www.coderanch.com/t/595112/Spring/find-css-images-js-static http://forum.springsource.org/showthread.php?97061-The-lt-mvc-resources-gt-does-not-work

Indirect help:

No mapping found for HTTP request with URI [/WEB-INF/pages/apiForm.jsp]

Upvotes: 3

Steven Benitez
Steven Benitez

Reputation: 11055

Resources that a browser will request directly, such as images, CSS, and JavaScript, cannot be under the /WEB-INF folder. That folder is not accessible through a web browser. Move your web resources up to the webapp folder. Then, link to them using ${pageContext.request.contextPath}/images/person.png.

Your structure should look like this:

/webapp
  /css
  /images
    /person.png
  /scripts
  /WEB-INF
    /view
      /jsp

And so forth and so on...

Upvotes: 4

Related Questions