0

Still trying to wrap my head around using the Spring Framework so I apologize in advance for my shiny newness.

I am trying to get Ajax to work and am having less than stellar results.

Here is the controller code, which is just something I'm trying to get to work so it may seem trivial:

@RequestMapping("/rest/login")    
public @ResponseBody CallManager logIn() throws ServletException {
    callManager.removeStationId(); 
    return callManager;
}

Here is the relevant web.xml

<servlet>
  <servlet-name>myservlet</servlet-name>
  <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
  <load-on-startup>1</load-on-startup>
</servlet>

<servlet-mapping>
  <servlet-name>myservlet</servlet-name>
  <url-pattern>*.htm</url-pattern>
</servlet-mapping>

<servlet-mapping>
  <servlet-name>myservlet</servlet-name>
  <url-pattern>/rest/*</url-pattern>
</servlet-mapping>

And here is the myservlet-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"
        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.xsd
        http://www.springframework.org/schema/mvc
        http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd">


    <import resource="database-c3p0.xml" />
    <context:annotation-config/>

    <mvc:annotation-driven /> 

    <!-- the application context definition for the springapp DispatcherServlet -->
    <bean id="productManager" class="org.dat.service.ProductManagerImpl">
    </bean>

    <bean id="priceIncrease" class="org.dat.service.PriceIncrease">
    </bean>

    <bean id="stationID" class="org.dat.domain.StationId">
    <property name="datStation" value=""/>
    </bean>

    <bean id="messageSource"   class="org.springframework.context.support.ResourceBundleMessageSource">
        <property name="basename" value="messages"/>
    </bean>

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

</beans>

This is the error I am getting:

org.springframework.web.servlet.PageNotFound - No mapping found for HTTP request with URI [/myservlet/rest/login] in DispatcherServlet with name 'myservlet'

Any help you can provide getting Ajax properly running is greatly appreciated. Please let me know if you need to see anything else. Thank you.

user1269651
  • 184
  • 8
  • Yeah, I guess that would be helpful! D'oh! – user1269651 May 29 '13 at 17:57
  • I am getting this error in the trace and a 404 on the page:org.springframework.web.servlet.PageNotFound - No mapping found for HTTP request with URI [/myservlet/rest/login] in DispatcherServlet with name 'myservlet' – user1269651 May 29 '13 at 17:58
  • Are you using eclipse for your development . If yes then try the following path /your project name in eclipse/rest/login – user1889970 May 29 '13 at 18:04

2 Answers2

1

If that is you entire configuration, I think what you are missing is

<context:component-scan />

in your spring configuration.

Without that, your controller classes (I am assuming that your controller classes are annotated with @Controller) won't get detected. This is what the documentation says

Scans the classpath for annotated components that will be auto-registered as Spring beans. By default, the Spring-provided @Component, @Repository, @Service, and @Controller stereotypes will be detected.

Also, once you have specified this in your configuration, it also provides the behavior of context:annotation-config by default. So, you can remove the

<context:annotation-config />

line from the configuration.

You can get a summary from here.

Community
  • 1
  • 1
Bhashit Parikh
  • 3,121
  • 23
  • 24
1

Your mapping does not match the request.

Your mapping for DispatcherServlet is /rest/*, so the call to /myservlet/rest/login would be handled by DispatcherServlet correctly, however beyond that it would not find a match, the reason is that DispatcherServlet would strip out its mapping from the rest of the mapping match. So in this case it would look for a match for /login and would not find it in any of the controllers.

Changing to @RequestMapping("/login") should work or another approach would be to change the DispatcherServlet's mapping to /

Biju Kunjummen
  • 49,138
  • 14
  • 112
  • 125