2012-07-02 5 views
0

Salut, je crée une application printemps mvc. Le contexte de printemps semble être la cartographie des méthodes de contrôleur à mauvaises URL.Printemps Mappages d'URL MVC

J'ai les contrôleurs suivants:

HelloWorldController

package com.springapp.controller; 

import org.springframework.stereotype.Controller; 
import org.springframework.web.bind.annotation.RequestMapping; 
import org.springframework.web.bind.annotation.RequestMethod; 
import org.springframework.web.servlet.ModelAndView; 

@Controller 
@RequestMapping("/hello") 
public class HelloWorldController { 

    @RequestMapping(method = RequestMethod.GET) 
    public ModelAndView helloWorld() { 

     String message = "Hello World, Spring 3.0!"; 
     return new ModelAndView("hello", "message", message); 
    } 
} 

ContactsController

package com.springapp.controller; 

import com.springapp.form.Contact; 
import com.springapp.service.ContactService; 

import java.util.Map; 

import org.springframework.beans.factory.annotation.Autowired; 
import org.springframework.stereotype.Controller; 
import org.springframework.ui.Model; 
import org.springframework.validation.BindingResult; 
import org.springframework.web.bind.annotation.ModelAttribute; 
import org.springframework.web.bind.annotation.PathVariable; 
import org.springframework.web.bind.annotation.RequestMapping; 
import org.springframework.web.bind.annotation.RequestMethod; 
import org.springframework.web.bind.annotation.SessionAttributes; 
import org.springframework.web.servlet.ModelAndView; 

@Controller 
@RequestMapping("/contacts") 
public class ContactsController { 

    @Autowired 
    private ContactService contactService; 

    @RequestMapping(method = RequestMethod.GET) 
    public String listContacts(Model map) { 

     map.addAttribute("contact", new Contact()); 
     map.addAttribute("contactList", contactService.listContacts()); 

     return "contact"; 
    } 

    @RequestMapping(value="{contactId}", method=RequestMethod.GET) 
    public String showContact(@PathVariable("contactId") Integer contactId) { 

     contactService.getContact(contactId); 
     return "redirect:/contacts"; 
    } 

    @RequestMapping(value = "/add", method = RequestMethod.POST) 
    public String addContact(@ModelAttribute("contact") Contact contact, BindingResult result) { 

     contactService.addContact(contact); 
     return "redirect:/contacts"; 
    } 

    @RequestMapping("/{contactId}/delete") 
    public String deleteContact(@PathVariable("contactId") Integer contactId) { 

     contactService.removeContact(contactId); 
     return "redirect:/contacts"; 
    } 
} 

Cependant le contexte du printemps est les mappant comme:

INFO: Mapped URL path [/contacts/new] onto handler 'contactsController' 
INFO: Mapped URL path [/contacts/new.*] onto handler 'contactsController' 
INFO: Mapped URL path [/contacts/new/] onto handler 'contactsController' 
INFO: Mapped URL path [/contacts/addContact] onto handler 'contactsController' 
INFO: Mapped URL path [/contacts/addContact.*] onto handler 'contactsController' 
INFO: Mapped URL path [/contacts/addContact/] onto handler 'contactsController' 
INFO: Mapped URL path [/contacts/delete/{contactId}] onto handler 'contactsController' 
INFO: Mapped URL path [/contacts/delete/{contactId}.*] onto handler 'contactsController' 
INFO: Mapped URL path [/contacts/delete/{contactId}/] onto handler 'contactsController' 
INFO: Mapped URL path [/hello] onto handler 'helloWorldController' 
INFO: Mapped URL path [/hello.*] onto handler 'helloWorldController' 
INFO: Mapped URL path [/hello/] onto handler 'helloWorldController' 

Où trouve-t-on ces modèles new et addContact? Le mappage /contacts est également manquant.

+0

La connexion à votre question est très étrange car elle ne correspond pas aux mappages dans votre 'ContactsController'. Etes-vous sûr de avoir publié du code et de vous connecter à partir de la même version? – davioooh

+0

@davioooh Incidemment, j'avais un mappage 'addContact' dans le ContactsController plus tôt, que j'ai mis à jour par la suite pour ajouter' add'. Comme vous pouvez le voir, j'ai aussi changé le mapping pour 'deleteContact' mais ça ne prend pas effet. –

+0

Utilisez-vous Eclipse pour développer/exécuter votre projet? – davioooh

Répondre

1

Votre problème peut dépendre des mappages que vous aviez dans une ancienne version de votre application. Essayez de mettre à jour la version déployée dans Tomcat.

Si vous utilisez Eclipse pour exécuter/déboguer votre projet, essayez de nettoyer/compiler votre projet et de déployer la nouvelle version dans Tomcat.

+0

Oui, j'utilise Eclipse et je le déploie sur Glassfish. –

+1

Essayez de supprimer la version déployée et publiez la nouvelle version. – davioooh

+0

Merci. J'ai nettoyé le projet et cela a fonctionné. –