在Tomcat 6上的Spring MVC 3中,我似乎无法使用/ x / y / z形式的RequestMappings工作。 / x / y似乎工作正常,这就是文档中的所有示例所示。
例如,为什么这样做
@RequestMapping(value="/browse/{categoryName}");
但这不起作用:
@RequestMapping(value="/browse/category/{categoryName}");
浏览http://localhost:8080/myapp/browse/category/books会从Tomcat生成HTTP 404。该方法如下所示:
@Controller
public class BrowseController {
@RequestMapping(value = "/browse/category/{categoryName}", method = RequestMethod.GET)
public String showCategory(@PathVariable("categoryName") String categoryName, Model model) {
model.addAttribute("categoryName", categoryName);
return "Browse";
}
}
我在Netbeans 6.9的Tomcat输出窗口中看到此消息:
Nov 14, 2010 2:02:03 PM org.springframework.web.servlet.DispatcherServlet noHandlerFound
WARNING: No mapping found for HTTP request with URI [/myapp/browse/category/model] in DispatcherServlet with name 'dispatcher'
编辑:从日志中添加了更多跟踪信息。请忽略时间戳,因为此问题已编辑两天。
部署应用时:
Nov 15, 2010 9:33:28 PM org.springframework.context.support.AbstractApplicationContext prepareRefresh
INFO: Refreshing WebApplicationContext for namespace 'dispatcher-servlet': startup date [Mon Nov 15 21:33:28 EST 2010]; parent: Root WebApplicationContext
Nov 15, 2010 9:33:28 PM org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
INFO: Loading XML bean definitions from ServletContext resource [/WEB-INF/dispatcher-servlet.xml]
Nov 15, 2010 9:33:29 PM org.springframework.beans.factory.support.DefaultListableBeanFactory preInstantiateSingletons
INFO: Pre-instantiating singletons in org.springframework.beans.factory.support.DefaultListableBeanFactory@143c423: defining beans [browseController,homeController,showController,org.springframework.context.annotation.internalConfigurationAnnotationProcessor,org.springframework.context.annotation.internalAutowiredAnnotationProcessor,org.springframework.context.annotation.internalRequiredAnnotationProcessor,org.springframework.context.annotation.internalCommonAnnotationProcessor,org.springframework.context.annotation.internalPersistenceAnnotationProcessor,org.springframework.web.servlet.mvc.support.ControllerClassNameHandlerMapping#0,viewResolver]; parent: org.springframework.beans.factory.support.DefaultListableBeanFactory@1ea763a
Nov 15, 2010 9:33:29 PM org.springframework.web.servlet.handler.AbstractUrlHandlerMapping registerHandler
INFO: Mapped URL path [/browse] onto handler 'browseController'
Nov 15, 2010 9:33:29 PM org.springframework.web.servlet.handler.AbstractUrlHandlerMapping registerHandler
INFO: Mapped URL path [/browse/*] onto handler 'browseController'
Nov 15, 2010 9:33:29 PM org.springframework.web.servlet.handler.AbstractUrlHandlerMapping registerHandler
INFO: Mapped URL path [/home] onto handler 'homeController'
Nov 15, 2010 9:33:29 PM org.springframework.web.servlet.handler.AbstractUrlHandlerMapping registerHandler
INFO: Mapped URL path [/home/*] onto handler 'homeController'
Nov 15, 2010 9:33:29 PM org.springframework.web.servlet.handler.AbstractUrlHandlerMapping registerHandler
INFO: Mapped URL path [/show] onto handler 'showController'
Nov 15, 2010 9:33:29 PM org.springframework.web.servlet.handler.AbstractUrlHandlerMapping registerHandler
INFO: Mapped URL path [/show/*] onto handler 'showController'
Nov 15, 2010 9:33:29 PM org.springframework.web.servlet.FrameworkServlet initServletBean
INFO: FrameworkServlet 'dispatcher': initialization completed in 501 ms
当我拨打http://localhost:8080/myapp/browse/category/model时,
Nov 15, 2010 9:34:28 PM org.springframework.web.servlet.DispatcherServlet noHandlerFound
WARNING: No mapping found for HTTP request with URI [/myapp/browse/category/model] in DispatcherServlet with name 'dispatcher'
这是我的web.xml调度程序配置:
<servlet>
<servlet-name>dispatcher</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>2</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>dispatcher</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
为什么我的URI模式有效,但另一个没有?
答案 0 :(得分:1)
您可能想要为浏览控制器添加请求映射。试试这个
@Controller
@RequestMapping("/browse")
public class BrowseController
{
@RequestMapping(value = "/category/{categoryName}", method = RequestMethod.GET)
public String showCategory(@PathVariable("categoryName") String categoryName, Model model)
{
model.addAttribute("categoryName", categoryName);
return "Browse";
}
}
答案 1 :(得分:1)
答案 2 :(得分:0)
您的URL模式应该是servlet映射中的/ *。