2017-06-14 2 views
0

1.Below est mon appel ajax à index.jspImpossible de frapper le printemps contrôleur mvc ajax par appel

<script type="text/javascript"> 
     $(document).ready(function(){ 
      $('#buttonDemo1').click(function(){ 
       $.ajax({ 
        method: "GET", 
        url: '${pageContext.request.contextPath}/ajax/demo1', 
        success: function(result){ 
         $('#result1').html(result); 
        }, 
          error: function(response){ 
           console.log("error"); 
          } 
       }); 
      }); 
     }); 
    </script> 

2.Below est le code du contrôleur

@Controller 
    @RequestMapping("/ajax") 
    public class AjaxController { 
    @RequestMapping(method = RequestMethod.GET) 
    public String index() { 
    return "ajax/index"; 
    } 

    @RequestMapping(value="/demo1", method = RequestMethod.GET) 
    @ResponseBody 
    public String demo1() { 
     return "Demo 1"; 
    } 
    } 

3. Ci-dessous est le code de répartiteur

<?xml version='1.0' encoding='UTF-8' ?> 
    <!-- was: <?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:p="http://www.springframework.org/schema/p" 
    xmlns:aop="http://www.springframework.org/schema/aop" 
    xmlns:tx="http://www.springframework.org/schema/tx" 
    xmlns:context="http://www.springframework.org/schema/context" 
    xsi:schemaLocation="http://www.springframework.org/schema/beans 
    http://www.springframework.org/schema/beans/spring-beans-4.0.xsd 
    http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd 
    http://www.springframework.org/schema/tx 
    http://www.springframework.org/schema/tx/spring-tx-4.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-4.0.xsd"> 

<context:component-scan base-package="demo.controller" /> 
<mvc:annotation-driven/> 

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

<!-- 
Most controllers will use the ControllerClassNameHandlerMapping above, but 
for the index controller we are using ParameterizableViewController, so we must 
define an explicit mapping for it. 
--> 
<bean id="urlMapping" class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping"> 
    <property name="mappings"> 
     <props> 
      <prop key="index.htm">indexController</prop> 
     </props> 
    </property> 
</bean> 

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

<!-- 
The index controller. 
--> 
<bean name="indexController" 
     class="org.springframework.web.servlet.mvc.ParameterizableViewController" 
     p:viewName="index" /> 

  1. Ci-dessous est web.xml

    <?xml version="1.0" encoding="UTF-8"?> 
        <web-app version="3.1" xmlns="http://xmlns.jcp.org/xml/ns/javaee" 
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
        xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee 
        http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"> 
        <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>2</load-on-startup> 
    </servlet> 
    <servlet-mapping> 
    <servlet-name>dispatcher</servlet-name> 
    <url-pattern>*.htm</url-pattern> 
    </servlet-mapping> 
    <session-config> 
    <session-timeout> 
        30 
    </session-timeout> 
    </session-config> 
    <welcome-file-list> 
    <welcome-file>redirect.jsp</welcome-file> 
    </welcome-file-list> 
    </web-app> 
    

Je ne sais pas ce qui ne va pas, à chaque fois que je frappe le chemin de contrôleur ajax je reçois "GET http://localhost:8080/ajax/demo1 404 (non trouvé)" erreur. S'il vous plaît, aidez-moi là où je me trompe. J'ai essayé quelques solutions mais elles n'ont pas résolu le problème.

+0

Voyez-vous votre instancier contrôleur dans la sortie du journal? –

+0

@TheHeadRush oui je peux voir. Voici les journaux: – Rishabh

+0

Info: Mappé "{[/ ajax], méthodes = [GET], params = [], en-têtes = [], consumes = [], outputs = [], custom = []}" sur public java.lang.String demo.controller.AjaxController.index() Info: Mappé "{[/ ajax/demo1], méthodes = [GET], params = [], en-têtes = [], consume = [], produit = [], custom = []} "sur public java.lang.String demo.controller.AjaxController.demo1() Info: Mappé URL chemin [/ ajax] sur le gestionnaire 'ajaxController' Info: Mapped URL path [/ ajax/*] sur le gestionnaire 'ajaxController' – Rishabh

Répondre