簡體   English   中英

Spring MVC URL映射

[英]Spring MVC URL Mappings

嗨,我正在創建一個Spring MVC應用程序。 spring上下文似乎正在將控制器方法映射到錯誤的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);
    }
}

聯系人控制器

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

但是spring上下文將它們映射為:

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丟失。

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

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM