2012-07-02 234 views
0

嗨,我正在創建一個春天mvc應用程序。春天的上下文似乎將控制器方法映射到錯誤的URL。Spring MVC URL映射

我已經以下控制器:

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"; 
    } 
} 

然而彈簧上下文映射它們爲:

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' 

它在哪裏得到這些newaddContact模式?此外,映射/contacts丟失。

+0

你的問題中的日誌很奇怪,因爲它不符合你的'ContactsController'中的映射。你確定你發佈的代碼和日誌來自同一版本嗎? – davioooh

+0

@davioooh順便說一句,我在前面的ContactsController中有一個'addContact'映射,後來我更新爲'add'。正如你所看到的,我也改變了'deleteContact'的映射,但它沒有生效。 –

+0

你使用Eclipse來開發/運行你的項目嗎? – davioooh

回答

1

您的問題可能取決於您在舊版應用程序中的映射。 嘗試更新Tomcat中的部署版本。

如果您使用Eclipse來運行/調試項目,請嘗試清理/編譯您的項目,然後在Tomcat中部署新版本。

+0

是的,我使用Eclipse並在Glassfish上部署。 –

+1

嘗試刪除已部署的版本併發布新版本。 – davioooh

+0

謝謝。我清理了這個項目並且工作。 –