Search code examples
javascriptspring-mvcspring-securitytomcat7javascriptmvc

Cannot access web application resources in Tomcat 7


I am working on making a sample web application using SpringMVC and JavascriptMVC, deployed Tomcat 7. I have incorporated Spring security and Spring MVC in my application i got help from this article by Mkyong http://www.mkyong.com/spring-security/spring-security-form-login-using-database/ Now my application runs fine when i deploy it on tomcat, but now i want to add Client Side MVC i.e JavascriptMVC in my application for that i want to add scripting resources like js files in my application.

Here's the flow of my application, user launches application using

"//localhost:8080/SpringMVC(application name)/welcome (/welcome is redirected by controller to hello.jsp page)"

this shows login page, once user credentials are write he gets to hello.jsp page. In this page i have added my JacascriptMVC code and referenced a js file in tag, and the application runs fine in my browser locally but when i deploy it on tomcat it says resources not accessable 404 error and only shows basic html, and can't access js and css files.

I have tried alot of ways to access the resource, like

<script src='./WebContent/javascriptmvc/steal/steal.production.js'/>

<script src='WebContent/javascriptmvc/steal/steal.production.js'/>

<script src='/WebContent/javascriptmvc/steal/steal.production.js'/>
but all gave same error.

my application directory structure of war file

-SpringMVC
  -WEB-INF
    -pages
    -hello.jsp
    -login.jsp 
  -META-INF
  -WebContent
    -javscriptmvc
      -steal
         -steal.production.js

Any Help would be highly appreciated thanks. Fahad


Solution

  • I got the problem solved thanks for responding guys.... I added this code in my servlet xml file,

    <?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:util="http://www.springframework.org/schema/util"   
           xmlns:context="http://www.springframework.org/schema/context"
           xmlns:oxm="http://www.springframework.org/schema/oxm"
           xsi:schemaLocation="http://www.springframework.org/schema/oxm    
        http://www.springframework.org/schema/oxm/spring-oxm-3.0.xsd
        http://www.springframework.org/schema/mvc
        http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd
        http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
        http://www.springframework.org/schema/util
        http://www.springframework.org/schema/util/spring-util-3.0.xsd
        http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context-3.0.xsd">
    <context:component-scan base-package="com.mkyong.common.controller" />
    <mvc:annotation-driven />
    <mvc:resources mapping="/resources/**" location="/resources/" />
    

    then in the jsp file i added this to get js files loaded

    <script type='text/javascript' src="${pageContext.request.contextPath}/resources/javascriptmvc/steal/steal.production.js"></script>

    also i changed the directory strcuture somewhat

    -webapps
        -WEB-INF
            -web.xml
            -mvc-dispatcher0servlet.xml
            -pages
                -hello.jsp
        -resources
            -javscriptmvc
                -steal
                    -steal.production.js