2013-02-25 15 views
10

Ho un controller Spring chiamato da JQuery.post(). Quando viene chiamato, il metodo del controller viene richiamato e restituito. Ma poi, sullo sfondo, Spring cambia l'URL e chiama il guadagno del server. Il server risponde con un 404.Spring Controller 404 retuned dopo il metodo POST Invocato

Penso che questo sia in risposta a Spring che tenta di trovare una vista dopo che il metodo POST è stato elaborato.

Come interrompere il funzionamento del controller Spring.

Ecco il mio controller Primavera:

import org.springframework.stereotype.Controller; 
import org.springframework.web.bind.annotation.ModelAttribute; 
import org.springframework.web.bind.annotation.PathVariable; 
import org.springframework.web.bind.annotation.RequestBody; 
import org.springframework.web.bind.annotation.RequestMapping; 
import org.springframework.web.bind.annotation.RequestMethod; 
import org.springframework.web.bind.annotation.ResponseBody; 

import java.util.ArrayList; 
import java.util.List; 

@Controller 
@RequestMapping("/person") 
public class DataController { 

    private List<Person> people = new ArrayList<Person>(); 

    @RequestMapping(value="put", method = RequestMethod.POST) 
    public void addPerson(@ModelAttribute("person") Person person){ 
    System.out.println(">>>>>>> person: " + person); 
    System.out.println(">>>>>>>>> " + person.getFirstName()); 
    people.add(person); 
    } 
} 

Ecco la mia domanda file di contesto 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/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd 
     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"> 

    <context:component-scan base-package="uk.co.jeeni" /> 

    <mvc:annotation-driven /> 

</beans> 

Ecco il mio file web.xml:

<?xml version="1.0" encoding="ISO-8859-1" ?> 
<web-app 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_4.xsd" 
     version="2.4"> 

    <servlet> 
     <servlet-name>dispatcherServlet</servlet-name> 
     <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> 
     <init-param> 
      <param-name>contextConfigLocation</param-name> 
      <param-value>classpath*:applicationContext-web.xml</param-value> 
     </init-param> 
     <load-on-startup>1</load-on-startup> 
    </servlet> 

    <servlet-mapping> 
     <servlet-name>dispatcherServlet</servlet-name> 
     <url-pattern>/data/*</url-pattern> 
    </servlet-mapping> 
</web-app> 

Qui è la mia Chiamata JQuery dal mio file HTML:

function attachSendDataEvent(){ 
    $("#sendData").click(function(){ 

     var data = "firstName=" + $("#firstName").val() + "&" + 
       "lastName=" + $("#lastName").val() + "&" + 
       "address=" + $("#address").val() + "&" + 
       "postcode=" + $("#postcode").val(); 

     $.post("data/person/put", 
       data, 
       dataSentOK 
     ); 
    }); 

    return false; 
} 

La funzione dataSentOK fa appena un alert("DONE").

Così, quando il metodo di JQuery l'URL invocato è:

http://localhost:8080/jquery/data/person/put 

e sul lato server del System.out.println(...) metodo di stampare i dati come previsto.

Tuttavia nel Firebug, il server invia indietro un 404.

Così ho acceso la registrazione con la Primavera e ha ottenuto questo:

[01] DispatcherServlet [DEBUG] DispatcherServlet with name 'dispatcherServlet' processing POST request for [/jquery/data/person/put] 
[02] AbstractHandlerMethodMapping [DEBUG] Looking up handler method for path /person/put 
[03] AbstractHandlerMethodMapping [DEBUG] Returning handler method [public void uk.co.jeeni.DataController.addPerson(uk.co.jeeni.Person)] 
[04] AbstractBeanFactory [DEBUG] Returning cached instance of singleton bean 'dataController' 
[05] DispatcherServlet [DEBUG] Rendering view [org.springframework.web.servlet.view.InternalResourceView: name 'person/put'; URL [person/put]] in DispatcherServlet with name 'dispatcherServlet' 
[06] AbstractView [DEBUG] Added model object 'org.springframework.validation.BindingResult.person' of type [org.springframework.validation.BeanPropertyBindingResult] to request in view with name 'person/put' 
[07] AbstractView [DEBUG] Added model object 'person' of type [uk.co.jeeni.Person] to request in view with name 'person/put' 
[08] InternalResourceView [DEBUG] Forwarding to resource [person/put] in InternalResourceView 'person/put' 
[09] DispatcherServlet [DEBUG] DispatcherServlet with name 'dispatcherServlet' processing POST request for [/jquery/data/person/person/put] 
[10] AbstractHandlerMethodMapping [DEBUG] Looking up handler method for path /person/person/put 
[11] AbstractHandlerMethodMapping [DEBUG] Did not find handler method for [/person/person/put] 
[12] DispatcherServlet [ WARN] No mapping found for HTTP request with URI [/jquery/data/person/person/put] in DispatcherServlet with name 'dispatcherServlet' 
[13] FrameworkServlet [DEBUG] Successfully completed request 
[14] FrameworkServlet [DEBUG] Successfully completed request 

In risposta alla richiesta URL POST (/ jquery/dati/persona/put), il metodo corretto è stato trovato e invocato (righe da 1 a 7), ma in seguito Spring towards a InternalResourceView alla riga 8, che modifica l'URL su /jquery/data/person/person/put e non è possibile trovarlo.

Come posso impedire a Spring di cercare una vista. Tutto quello che voglio che faccia è tornare pulito e fatto.

Grazie per il vostro aiuto.

risposta

6

Credo che se si dispone di un tipo di reso nullo o vuoto, Spring tenterà di risolvere la vista in base all'URL della richiesta. Penso che la forma corretta qui sarebbe semplicemente restituire una pagina OK, poiché questo non sembra essere JSON o qualcosa del genere. Oppure, taggalo con @ResponseBody e restituisci una stringa vuota.

+1

Grazie per la risposta. La risposta era aggiungere @ResponseBody. La primavera sembra gestire bene i ritorni nulli. –

+2

@AdamDavies pubblicarlo come risposta e contrassegnarlo come accettato –

11

Risolto.

Il problema era come suggerito da #CodeChimp, tranne che volevo ancora un tipo di reso vuoto.

ho aggiunto il @ResponseBody al metodo addPerson e tutto ha funzionato bene:

@RequestMapping(value="put", method = RequestMethod.POST) 
**@ResponseBody** 
public void addPerson(@ModelAttribute("person") Person person){ 
    System.out.println(">>>>>>> person: " + person); 
    System.out.println(">>>>>>>>> " + person.getFirstName()); 
    people.add(person); 
} 

L'indizio è venuto da http://static.springsource.org/spring/docs/3.2.x/spring-framework-reference/html/mvc.html#mvc-ann-responsebody.Anche se la documentazione non è chiara cosa succede con un reso nullo. Ho appena provato e ha funzionato.

+0

Immagino che un annullamento nullo o nullo funzionerà come una stringa vuota. Spring non avrebbe semplicemente messo nulla nel corpo della risposta. Trovo che le persone di Spring siano abbastanza intelligenti e tendono a pensare a tutti i possibili angoli nelle loro soluzioni. – CodeChimp

Problemi correlati