Java Spring MVC URL映射
嗨,我正在创建一个spring mvc应用程序。spring上下文似乎将控制器方法映射到错误的URL 我有以下控制器: HelloWorldControllerJava Spring MVC URL映射,java,spring,spring-mvc,controller,Java,Spring,Spring Mvc,Controller,嗨,我正在创建一个spring mvc应用程序。spring上下文似乎将控制器方法映射到错误的URL 我有以下控制器: HelloWorldController package com.springapp.controller; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import org.springfram
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";
}
}
接触器控制器
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'
从哪里获得这些
新的和addContact
模式?此外,映射/contacts
丢失。您的问题可能取决于您在旧版本应用程序中的映射。
尝试在Tomcat中更新部署的版本
如果您使用Eclipse运行/调试您的项目,请尝试清理/编译您的项目,而不是在Tomcat中部署新版本。您问题中的日志非常奇怪,因为它与您的ContactsController中的映射不匹配。你确定你发布的代码和日志来自同一版本吗?@davioooh顺便说一句,我之前在ContactsController中有一个addContact
映射,后来我将其更新为add
。如您所见,我还更改了deleteContact
的映射,但它没有生效。您是否使用Eclipse开发/运行项目?是的,我正在使用Eclipse并在Glassfish上部署。请尝试删除部署的版本并发布新版本。