0

I ahve a spring application where an ajax call after login dosen't work.

I made the ajax function as below in CHrome dev tools snippets.

var today = new Date();
var month=today.getMonth();
var name="testuser";

$(document).ready(function(){
  $.ajax({
   type: "GET",
   url: "ajax/getStmt",
   data: "name="+name+"&month="+month,
   success: function(msg){
     alert( "Data Saved: " + msg );
   },
   error: function(msg){
       alert("there was an error \n"+msg);
   }
 }); 
 });

controller:

package com.cardholder.controller.ajax;
@Controller
public class StatementAjax {
@Autowired
public UserDao userDao;

//initialise logger, dao
@RequestMapping(value = "/ajax/getMonths", method = RequestMethod.GET)
public String getMonths(){
    //get user from session
    //get user token from session

    /*UserStatement availableStmts = new UserStatement();
    availableStmts.setAvailableStatements(userDao.getUserStatementMonths(user.getUser_token()));*/
    return "months";
}
@RequestMapping(value = "/ajax/getStmt", method = RequestMethod.GET)
public String getStatement(@RequestParam(required = true) String name, @RequestParam(required = true) String month){
    System.out.println(name+", "+month);
    //get user from session
    //get user token from session
    /*UserStatement availableStmts = new UserStatement();
    availableStmts.setAvailableStatements(userDao.getUserStatementMonths(user.getUser_token()));
    ArrayList<UserStatement> s = (ArrayList<UserStatement>) userDao.getUserSelectedStatement(user.getUser_token(), 07, 2015);*/

    return "statements";
}

}

Spring xml.

<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:tx="http://www.springframework.org/schema/tx" xmlns:p="http://www.springframework.org/schema/p"
xmlns:jdbc="http://www.springframework.org/schema/jdbc" xmlns:security="http://www.springframework.org/schema/security"
xsi:schemaLocation="http://www.springframework.org/schema/beans 
http://www.springframework.org/schema/beans/spring-beans-4.1.xsd
http://www.springframework.org/schema/context  
http://www.springframework.org/schema/context/spring-context-4.1.xsd 
 http://www.springframework.org/schema/jdbc 
 http://www.springframework.org/schema/jdbc/spring-jdbc-4.1.xsd
http://www.springframework.org/schema/security
http://www.springframework.org/schema/security/spring-security-3.2.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-4.1.xsd">
<context:annotation-config />
<context:component-scan base-package="com.cardholder,com.cardholder.orm,com.cardholder.controller.ajax" />
<tx:annotation-driven transaction-manager="transactionManager"></tx:annotation-driven>
<bean id="secureServiceClient" class="com.cardholder.service.SecureServiceClient">
     <property name="serviceUrl" value="http://xxxx:8080/xApi/api/"></property>
     <property name="serviceUserName" value="xxxx"></property>
    <property name="servicePassword" value="xxxx"></property> 
</bean>
<bean id="httpHeders" class="org.springframework.http.HttpHeaders">
</bean>
<bean id="restTemplate" class="org.springframework.web.client.RestTemplate">

<property name="errorHandler">
    <bean class= "com.cardholder.service.error.ServiceClientErrorHandler" />
</property>
</bean>

<bean id="resourceLoader" class="org.springframework.core.io.DefaultResourceLoader"/>
<bean id="pdfRenderer" class="com.cardholder.controller.menunav.util.Util"/>        
<bean id="serviceClientInterceptor" class="com.cardholder.service.interceptors.ServiceClientInterceptor">
    <property name="serviceUserName" value="xxx"></property>
    <property name="servicePassword" value="xxx"></property> 
</bean>
<bean id="encoder"
    class="org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder">
    <constructor-arg name="strength" value="16" />
</bean>
<bean id="idGenerator" class="org.apache.catalina.util.SessionIdGenerator"></bean>
<bean id="sessionValidator" class="com.cardholder.session.SessionValidator"></bean>
<bean class="org.springframework.orm.hibernate4.HibernateTransactionManager" id="transactionManager">
    <property name="sessionFactory" ref="sessionFactory" />
</bean>
<bean id="sessionFactory"
    class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
    <property name="dataSource" ref="dataSource"></property>
    <property name="hibernateProperties">
        <props>
            <prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
            <prop key="hibernate.show_sql">true</prop>
        </props>
    </property>
    <property name="packagesToScan" value="com.cardholder"></property>
</bean>
<bean id="dataSource"
    class="org.springframework.jdbc.datasource.DriverManagerDataSource">
    <property name="driverClassName" value="com.mysql.jdbc.Driver" />
    <property name="url" value="jdbc:mysql://xxxx:3306/portal" />
    <property name="username" value="xxxx" />
    <property name="password" value="xxxx" />
</bean>

Below is the link to a screenshot from devtools.

https://i.stack.imgur.com/EXry7.png

As far as i know the request dosent even enter the application context even if the URL its trying to access is right.

i have a debug point in the dispatcher servlet's do dispatch method which should have been the first place to be reached before any interceptors, controllers etc. i dont see any thread pausing there.

EDITS: adding Web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
xmlns="http://java.sun.com/xml/ns/javaee" 
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" 
id="WebApp_ID" version="3.0"
metadata-complete="true">
 <display-name>NCHP</display-name>

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

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

   <listener>
      <listener-class>
         org.springframework.web.context.ContextLoaderListener
      </listener-class>
   </listener>
 <filter>  
    <filter-name>encodingFilter</filter-name>  
    <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>  
    <init-param>  
       <param-name>encoding</param-name>  
       <param-value>UTF-8</param-value>  
    </init-param>  
    <init-param>  
       <param-name>forceEncoding</param-name>  
       <param-value>true</param-value>  
    </init-param>  
</filter>  
<filter-mapping>  
    <filter-name>encodingFilter</filter-name>  
    <url-pattern>/*</url-pattern>  
</filter-mapping>
   <welcome-file-list>  
     <welcome-file>land.htm</welcome-file> 
 </welcome-file-list> 
</web-app>
DJR
  • 89
  • 2
  • 3
  • 7
  • Look in your log files - Spring logs the endpoints that have been created. Ensuring Spring is wired up the way you intended is a good first step in troubleshooting 404 errors. – Paul Aug 10 '15 at 18:22
  • where can i find my spring logs? sorry im new to spring. i used to work on struts 1& 2 earlier. – DJR Aug 10 '15 at 18:28
  • How are you deploying your app? Are you using Spring Boot or Tomcat or Jetty or something else? – Paul Aug 10 '15 at 18:32
  • Im not using spring boot or maven. This app is deployed in a local tomcat 7 – DJR Aug 10 '15 at 18:34
  • Since it doesn't sound like you've set up your own logger to capture Spring's output I suggest looking in Tomcat's log files: catalina.out, catalina.yyyy-mm-dd.log, localhost.yyyy.mm.dd.log. I don't remember where they'll end up but there should be something in one of those files. – Paul Aug 10 '15 at 18:39
  • Please paste your web.xml in the question. – Akshay Aug 10 '15 at 20:27
  • @AkshaySinghal added – DJR Aug 10 '15 at 20:44
  • I think, view resolver's are missing from the configuration. Please add an view resolver. Also limit the package scan to base package com.cardholder. – Niranjan Aug 11 '15 at 06:57

1 Answers1

0

Thanks for posting your web.xml.

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

Based on the above config Spring's dispatcher will be consulted, only if the incoming URL has some path info. e.g. login.htm, something.xx. However, if the url has no path info, like /getStmt, the dispatcher will not receive the request.

You can either change your ajax end point to some thing like /getStmt.ajax. Or add /as a url pattern in the web xml (this could have other implications, based on how other resources are served in your project).

If you are currently serving pages through xxxx.htm convention, I would recommend using xxxx.ajax convention for ajax requests.

You might also want to look at this answer : No mapping found for HTTP request with URI Spring MVC

Hope this helps.

Community
  • 1
  • 1
Akshay
  • 3,158
  • 24
  • 28