0

I'm working on a project where I need to submit a JSOn object using AJAX to Spring controller. But I'm getting 404 on submission. Please, can somebody tell me what the problem is:

My AJAX Request :

    $.ajax({
        url: 'NewTestApp/chkDetails/',
        type : 'POST',
        data : 'pwd='+ p,       
        timeout: 15000,
        async : false,
        dataType: 'json',
                success: function (data, textStatus, jqXHR) {
                    displayThings(data);
                },
                error: function (jqXHR, textStatus, errorThrown) {
                    $('#error').show();
            }
        });
    }
}

My Controller :

@RequestMapping(value = "/chkDetails", method = RequestMethod.POST)
@ResponseBody
public JSONObject getDetails(@RequestParam(value = "pwd")Object sPassword) throws IOException, ParseException
{
    JSONObject obj = (JSONObject) JSONValue.parse(sPassword.toString());
    JSONObject retObj;

    if(obj.isEmpty())
    {
        System.out.println("hihi");
    }       
    retObj = chk.chkStrength(obj);
    return retObj;  
}

My web.xml :

<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>*.html</url-pattern>
</servlet-mapping>
<context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>/WEB-INF/spring-servlet.xml</param-value>
</context-param>

and My spring-servlet.xml :

<context:component-scan
    base-package="main.pwd.controller" />


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

<bean id="urlMapping" class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
    <property name="urlMap">           
        <map>
            <entry key="/checkDetails.html">
                <ref bean="PasswordServiceController"/>
            </entry>
        </map>
    </property>

</bean>
  <bean id="PasswordServiceController" class="main.pwd.controller.PasswordServiceController"> </bean>

I'm very new to spring and this is my first project. Pretty sure I've done something wrong with the mapping.

If directory structure helps :

NewTestApp | WebContent | |-WEB-INF | | | |-web.xml | |-spring-servlet.xml | |-jsp | | | |- checkDetails.jsp |-index.jsp

index.jsp is able to call checkDetails.jsp. Also, there is no problem with AJAX JSON submission, the application is working perfectly when m not implementing it as a Spring MVC.

Do I need to add another mapping for it ?

  • I want to make it clear ... the mapping in spring-servlet.xml is done to make spring recognize the checkDetails.jsp page... On application invocation "index.jsp" is displayed, which contains an href to "checkDetails.jsp" ... IMy application is REST based. M doing a POST request using java. so my application should call ... http://localhost:8080/NewTestApp/chkDetails .... As suggested by @Biju .... I tried including another mapping but it is not working. – Hitesh Patwari Jul 17 '12 at 16:36

1 Answers1

1

Your servlet mapping for dispatcher servlet is *.html, so your requests also should be to /NewTestApp/chkDetails.html for it to be handled by your Spring controller. If you want it to be handled by /NewTestApp/chkDetails then the dispatcher servlet mapping should be /

Biju Kunjummen
  • 49,138
  • 14
  • 112
  • 125
  • Ok ... the problem is ... If change the mapping to "/" only .., ity does not recognize my JS & CSS files. I tried adding another mapping in web.xml ... but still the same issue. check org.springframework.web.servlet.DispatcherServlet 1 check / – Hitesh Patwari Jul 17 '12 at 16:03
  • Ok, but does the json work if your put your servlet mapping as `/`, if it does then the static content is a different issue right - you can probably ask another question on it as it could be the way you have mapped your static content - you can put a `` to handle the static content - assuming that your static content is in a folder called static – Biju Kunjummen Jul 17 '12 at 16:31
  • No it is not working ... :( ... Icopied necessary function from the JS file in the jsp and tried it ... still 404 – Hitesh Patwari Jul 17 '12 at 17:30