0

I have created a Spring DWR integration referring from here.

When I run the project I get a pop up "NOT FOUND" in the browser the response for dwrService.js is not found. Engine.js and util.js are loaded correctly.

Request: `http://localhost:8080/HelloWorldSpring3/dwr/interface/dwrService.js`
Response: "404 Not Found"

The message in the console is

an 10, 2014 4:12:31 PM org.springframework.web.servlet.DispatcherServlet noHandlerFound
WARNING: No mapping found for HTTP request with URI [/HelloWorldSpring3/forms/dwr/interface/dwrService.js] in DispatcherServlet with name 'dispatcher'
Jan 10, 2014 4:14:24 PM org.springframework.web.servlet.DispatcherServlet noHandlerFound
WARNING: No mapping found for HTTP request with URI [/HelloWorldSpring3/forms/dwr/dwr/interface/dwrService.js] in DispatcherServlet with name 'dispatcher'

Why the request for dwrService.js is going for the dispatcher servlet?

Below are my config files and controller and jsp

web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_5.xsd">

    <display-name>Comet</display-name>

    <servlet>
      <servlet-name>dispatcher</servlet-name>
      <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
      <init-param> 
      <param-name>contextConfigLocation</param-name> 
      <param-value>/WEB-INF/applicationContext.xml</param-value> 
        </init-param> 
      <load-on-startup>1</load-on-startup>
    </servlet>

     <!-- Mapping for DWR -->
    <servlet-mapping>
      <servlet-name>dispatcher</servlet-name>
      <url-pattern>/forms/dwr/*</url-pattern>
    </servlet-mapping>       

    <!-- Mapping for MVC -->
    <servlet-mapping>
      <servlet-name>dispatcher</servlet-name>
      <url-pattern>/forms/*</url-pattern>
    </servlet-mapping>

    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>

    <welcome-file-list>
        <welcome-file>index.jsp</welcome-file>
    </welcome-file-list>    
</web-app>

dwr-Context.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:context="http://www.springframework.org/schema/context"
    xmlns:p="http://www.springframework.org/schema/p" 
    xmlns:dwr="http://www.directwebremoting.org/schema/spring-dwr"
    xsi:schemaLocation="
        http://www.springframework.org/schema/beans 
        http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
        http://www.directwebremoting.org/schema/spring-dwr
        http://www.directwebremoting.org/schema/spring-dwr-3.0.xsd">

    <dwr:annotation-config />

    <dwr:annotation-scan base-package="net.roseindia.controllers" scanDataTransferObject="true" scanRemoteProxy="true" />

    <dwr:url-mapping />

    <dwr:controller id="dwrController" debug="true">
    <dwr:config-param name="activeReverseAjaxEnabled" value="true"/>
    </dwr:controller>
</beans>

applicationContext.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: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-3.0.xsd
            http://www.springframework.org/schema/context
            http://www.springframework.org/schema/context/spring-context-3.0.xsd
            http://www.springframework.org/schema/mvc 
            http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd">


    <context:annotation-config />

    <context:component-scan base-package="net.roseindia.controllers" />

    <mvc:annotation-driven /> 

    <bean class="org.springframework.web.servlet.mvc.SimpleControllerHandlerAdapter" />

    <import resource="file:**/WebContent/WEB-INF/dwr-context.xml" />

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

        <property name="prefix">
            <value>/WEB-INF/views/</value>
        </property>
        <property name="suffix">
            <value>.jsp</value>
        </property>
    </bean>     
</beans>

JSP

<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <title>Spring MVC - DWR Integration Tutorial</title>
    <script type='text/javascript' src="${pageContext.request.contextPath}/dwr/engine.js"></script>
    <script type='text/javascript' src="${pageContext.request.contextPath}/dwr/util.js"></script>
    <script type="text/javascript" src="${pageContext.request.contextPath}/dwr/interface/dwrService.js"></script>


</head>
<body>

<h3>Spring MVC - DWR Integration Tutorial</h3>
<h4>AJAX version</h4>

<script type="text/javascript">
    // Retrieves the matching value
    // Delegates to the dwrService
    function add() {
        // Retrieve value of text inputs
        var operand1 = dwr.util.getValue("inputNumber1");
        var operand2 = dwr.util.getValue("inputNumber2");

        // Pass two numbers, a callback function, and error function
        dwrService.add(operand1, operand2, {
            callback : handleAddSuccess,
            errorHandler : handleAddError
        });
    }

    // data contains the returned value
    function handleAddSuccess(data) {
        // Assigns data to result id
        dwr.util.setValue("sum", data);
    }

    function handleAddError() {
        // Show a popup message
        alert("We can't add those values!");
    }
</script>

Demo 1
<div style="border: 1px solid #ccc; width: 250px;">
    Add Two Numbers: <br/>
    <input id="inputNumber1" type="text" size="5"> +
    <input id="inputNumber2" type="text" size="5">
    <input type="submit" value="Add" onclick="add()" /> <br/>
    Sum: <span id="sum">(Result will be shown here)</span>
</div>
</body>
</html>

What am i doing wrong here?

Aditya
  • 2,876
  • 4
  • 34
  • 30
underdog
  • 4,447
  • 9
  • 44
  • 89

1 Answers1

0

Why the request for dwrService.js is going for the dispatcher servlet?

Because that's exactly how you have it mapped. Notice <servlet-name>dispatcher</servlet-name>. It means requests matching "/forms/dwr" are going to be sent to a servlet named "dispatcher".

    <!-- Mapping for DWR -->
    <servlet-mapping>
      <servlet-name>dispatcher</servlet-name>
      <url-pattern>/forms/dwr/*</url-pattern>
    </servlet-mapping>    

I'm not a DWR expert, but I believe that with the way you're looking to set it up, the requests need to go to the DispatcherServlet first anyway. Spring will then forward them to Spring-managed DWR beans.

Are you sure that <import resource="file:**/WebContent/WEB-INF/dwr-context.xml" /> is working? I'd crank up the Spring logging to DEBUG to see if any beans from this file are even being created.

That "/WebContent" in your import statement looks suspect to me. Should /WebContent/WEB-INF/dwr-context.xml be /WEB-INF/dwr-context.xml?

What OS are you using? There are some inconsistencies in capitalization in your question. You mentioned dwr-Context.xml but the case doesn't match what's in your XML. This would only be a factor if you're on a Unix-type platform.

EDIT:

Just a question in general do the request for importing of all the css & js files in the header part of the UI also goes through the dispatcher servlet mapping?

It depends on the URL structure. If your image and CSS files are under "/forms", then the requests will hit the Spring dispatcher. You'll need to specifically configure the dispatcher in order to serve your resources. Spring provides the [<mvc:resources/>] directive (How to access static resources when mapping a global front controller servlet on /*) for this purpose.

However, since you have the Spring dispatcher mapped to "/forms/" and not to "/" anything that's not under "/forms/*" will be served by your container's default servlet. For example, "/css/main.css" wouldn't hit Spring's dispatcher with your current configuration.

Either approach is valid but my personal preference is to map DispatcherServlet to a path like /ajax or /ui and let the servlet container serve my static content from folders like /images or /css.

Community
  • 1
  • 1
John R
  • 2,066
  • 1
  • 11
  • 17
  • Thanks for the reply John don't have access to the code right now will update in a day. Just a question in general do the request for importing of all the css & js files in the header part of the UI also goes through the dispatcher servlet mapping? – underdog Jan 11 '14 at 05:19