1

I am working with spring 3 framework for developing my web application. I have to link my external javascript file with my jsp forms. For this i am using script tag as follows: <script type="text/javascript" src="test.js"></script>

My web.xml mapping is as follows:

`<servlet>
    <servlet-name>spring</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
    <servlet-name>spring</servlet-name>
    <url-pattern>/</url-pattern>
</servlet-mapping>  
<servlet-mapping>
    <servlet-name>spring</servlet-name>
    <url-pattern>*.js</url-pattern>
</servlet-mapping>`

But i am getting error like this:

Mar 13, 2014 4:54:01 PM org.springframework.web.servlet.DispatcherServlet noHandlerFound WARNING: No mapping found for HTTP request with URI [/Project/test.js] in >DispatcherServlet with name 'spring'

Here is my workspace structure. Project>>WebContent>>jsp

And in the jsp folder, i am having all the jsp's and test.js file.

I had tried - changing the web.xml configuration - tried getting context path with test.js

<script type="text/javascript" src="${pageContext.servletContext.contextPath}/test.js"></script>
  • tried pasting the test.js file in different paths
  • created a directory inside webcontent and pasted test.js file to check

    <script type="text/javascript" src="${pageContext.servletContext.contextPath}/jsp/test.js"></script>

I have referred many of related post, but i could not resolve it till now. Please somebody who had same issue help to resolve this.

EDIT Here i am adding my spring-servlet.xml for reference.

<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
    http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
    http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd">

<context:annotation-config />
<context:component-scan base-package="com.inet.test.spring3.controller" />  

<bean id="jspViewResolver"
    class="org.springframework.web.servlet.view.InternalResourceViewResolver">
    <property name="viewClass"
        value="org.springframework.web.servlet.view.JstlView" />
    <property name="prefix" value="/jsp/" />
    <property name="suffix" value=".jsp" />
</bean>     

Raj
  • 73
  • 2
  • 10
  • You dont want to specify it under ``. Specify it in html code – Deepak Ingole Mar 13 '14 at 12:15
  • if you are using spring specify it in applicationcontext file – V__ Mar 13 '14 at 12:15
  • can you update your question with your dispatcher-servlet.xml – V__ Mar 13 '14 at 12:16
  • @Pilot i wrote the – Raj Mar 13 '14 at 12:23
  • @VasudevPathak Are you mentioning spring-servlet.xml. Sorry, i am new to spring. Kindly clarify me. – Raj Mar 13 '14 at 12:26
  • yes, your mapping should be in spring-servlet.xml with resources mapping annotation – V__ Mar 13 '14 at 12:28
  • @VasudevPathak Thanks. I have edited the post to add spring-servlet.xml file. – Raj Mar 13 '14 at 12:36
  • use this to mapping . Map with js folder...i hope it will help you – V__ Mar 13 '14 at 12:38
  • @VasudevPathak I had tried that already. tag will work after spring 3.0.4 versions only. Anyway thanks for your help. [link](http://stackoverflow.com/questions/13645792/the-matching-wildcard-is-strict-but-no-declaration-can-be-found-for-element-re) – Raj Mar 13 '14 at 12:55

2 Answers2

0

I saw this in your web.xml

<servlet-mapping>
    <servlet-name>spring</servlet-name>
    <url-pattern>*.js</url-pattern>
</servlet-mapping>

So, whenever a url in your domain ends with .js, it will be mapped to your

org.springframework.web.servlet.DispatcherServlet

I was thinking, why do you need to map *.js? Is there any reason? Could you try removing that servlet-mapping tag and try again?

CodeBender
  • 267
  • 4
  • 14
  • I thought it would map all js files. Sorry if am wrong. And tried removing that from web.xml. Still its not working. I got same error. >Mar 14, 2014 9:53:02 AM >org.springframework.web.servlet.DispatcherServlet noHandlerFound >WARNING: No mapping found for HTTP request with URI >[/Project/test.js] in DispatcherServlet with name 'spring' – Raj Mar 14 '14 at 04:24
  • Oh strange. Did u recompile after removing the .js mapping? Your error message still saying that .js is going to DispatcherServlet and no mapping is found for test.js. – CodeBender Mar 14 '14 at 05:00
  • You really don't have to define servlet-mapping to include external java script file. – shakthydoss Mar 14 '14 at 11:08
0

You really don't have to define servlet-mapping to include external java script file.

Remove

<servlet-mapping>
         <servlet-name>spring</servlet-name>
        <url-pattern>/</url-pattern>
</servlet-mapping> 

Replace with

<servlet-mapping>
         <servlet-name>spring</servlet-name>
           <url-pattern>*.do</url-pattern>
</servlet-mapping> 

Configure your @RequestMapping("/mycontroller.html")

By giving / you configured the spring to handle all request coming to the application. In this case you did not have proper handler for .js files.

By giving *.do you configured the spring to handle only request that ends only with .do

shakthydoss
  • 2,551
  • 6
  • 27
  • 36
  • Thanks @Shakthydoss. I understand pretty clear now. Also changed "/" to *.do now and its working like charm. – Raj Mar 14 '14 at 11:47