Question

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

Was it helpful?

Solution 2

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

OTHER TIPS

Your Spring MVC dispatcher servlet must be registered at / right -

<servlet>
    <servlet-name>mvc-dispatcher</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
    <servlet-name>mvc-dispatcher</servlet-name>
    <url-pattern>/</url-pattern>
</servlet-mapping> 

if so add a <mvc:default-servlet-handler /> to your context also, this will dispatch the calls to static resources to the default servlet of the container instead of Spring trying to handle it

Further, refer to your resources using an absolute path like this:

<script src='${pageContext.request.contextPath}/WebContent/javascriptmvc/steal/steal.production.js'/>
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top