gpt4 book ai didi

java - Spring MVC URI模板问题

转载 作者:行者123 更新时间:2023-11-30 09:52:36 25 4
gpt4 key购买 nike

在 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 模式有效而另一个没有?

最佳答案

您可能想要为浏览 Controller 添加一个请求映射。试试这个

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

关于java - Spring MVC URI模板问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4178701/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com