URL 映射问题 - Spring Web MVC

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11692837/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 05:16:46  来源:igfitidea点击:

URL Mapping issue - Spring web MVC

spring

提问by magiclko

I'm a newbie with Spring and web MVC module. Basically, i have the following :

我是 Spring 和 web MVC 模块的新手。基本上,我有以下几点:

web.xml

网页.xml

<servlet>
    <servlet-name>abc-dispatcher</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <load-on-startup>1</load-on-startup>
</servlet>

<servlet-mapping>
    <servlet-name>abc-dispatcher</servlet-name>
    <url-pattern>/user/*</url-pattern>
</servlet-mapping>

abc-dispatcher-servlet.xml

abc-dispatcher-servlet.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
   xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
   xmlns:context="http://www.springframework.org/schema/context"
   xsi:schemaLocation="http://www.springframework.org/schema/beans
   http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
   http://www.springframework.org/schema/context
   http://www.springframework.org/schema/context/spring-context-3.0.xsd">


<context:component-scan base-package="myPkg" />


<bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
    <property name="viewClass" value="org.springframework.web.servlet.view.JstlView"/>
    <property name="prefix" value="/WEB-INF/pages/"></property>
    <property name="suffix" value=".jsp"></property>        
</bean>

And i have a controller, related parts are :

我有一个控制器,相关部分是:

@Controller
public class ABCController {

@RequestMapping("/user/welcome")
public String printWelcome(ModelMap model) {

    //code

}

Now whenever i try to access http://localhost:8080/myapp/user/welcome

现在每当我尝试访问 http://localhost:8080/myapp/user/welcome

it gives me 404.

它给了我 404。

Logs say that "mapped url '/user/welcome' onto handler 'ABCController' but it failed to map URI [/MYAPP/user/welcome] in DispatcherServlet with name 'abc-dispatcher' .

日志说“将 url '/user/welcome' 映射到处理程序 'ABCController' 上,但它未能将 URI [/ MYAPP/user/welcome]映射到 DispatcherServlet 中,名称为 'abc-dispatcher' 。

Totally confused. I have checked all the threads where we specify a mapping twice, but that's not the case here. I must be missing something!

完全糊涂了。我已经检查了我们两次指定映射的所有线程,但情况并非如此。我肯定错过了什么!

Thanks for the help!

谢谢您的帮助!

采纳答案by JB Nizet

The URL should be http://localhost:8080/myapp/user/user/welcome. Indeed, unless the alwaysUseFullPath property of the handler is set to true, the servlet-mapping is prepended to the request mapping URL to form the full path.

网址应该是http://localhost:8080/myapp/user/user/welcome。实际上,除非处理程序的 alwaysUseFullPath 属性设置为 true,否则 servlet-mapping 将被添加到请求映射 URL 以形成完整路径。

See http://static.springsource.org/spring/docs/3.1.x/spring-framework-reference/htmlsingle/spring-framework-reference.html#mvc-handlermappingfor details:

有关详细信息,请参阅http://static.springsource.org/spring/docs/3.1.x/spring-framework-reference/htmlsingle/spring-framework-reference.html#mvc-handlermapping

alwaysUseFullPath

If true , Spring uses the full path within the current Servlet context to find an appropriate handler. If false (the default), the path within the current Servlet mapping is used. For example, if a Servlet is mapped using /testing/* and the alwaysUseFullPath property is set to true, /testing/viewPage.html is used, whereas if the property is set to false, /viewPage.html is used.

始终使用完整路径

如果为 true ,Spring 将使用当前 Servlet 上下文中的完整路径来查找适当的处理程序。如果为 false(默认值),则使用当前 Servlet 映射中的路径。例如,如果使用 /testing/* 映射 Servlet,并且 alwaysUseFullPath 属性设置为 true,则使用 /testing/viewPage.html,而如果该属性设置为 false,则使用 /viewPage.html。

回答by Victor Bashurov

It's' been added context:component-scanelement into the sample context file snippet but there is no <annotation-driven/>element that says spring framework to look for controllers annotated with @Controller

它已被添加context:component-scan到示例上下文文件片段中,但没有<annotation-driven/>元素表明 spring 框架要查找带有注释的控制器@Controller

回答by redochka

For me, the problem was that I was using the deprecated DefaultAnnotationHandlerMapping, and even if setting the alwaysUseFullPathto true, it didn't take effect, however replacing DefaultAnnotationHandlerMapping in benefit of RequestMappingHandlerMappingwith the alwaysUseFullPathset to true solved the problem.

对我来说,问题是我使用了已弃用的DefaultAnnotationHandlerMapping,即使将alwaysUseFullPath设置为 true,它也没有生效,但是将有利于RequestMappingHandlerMapping 的DefaultAnnotationHandlerMapping 替换为设置为 true的alwaysUseFullPath解决了问题。

<bean id="handlerMapping" class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping">
    <property name="alwaysUseFullPath" value="true"></property>
</bean>