spring - URL 映射问题 - Spring Web MVC

标签 spring

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

web.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

<?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>

我有一个 Controller ,相关部分是:

@Controller
public class ABCController {

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

    //code

}

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

它给了我 404。

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

完全糊涂了。我已经检查了我们两次指定映射的所有线程,但这里不是这种情况。我一定是错过了什么!

感谢您的帮助!

最佳答案

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

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.

关于spring - URL 映射问题 - Spring Web MVC,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11692837/

相关文章:

spring - 如何覆盖Spring Cloud Ribbon中的ribbon.serverListRefreshInterval默认值?

java - @Configuration @AutoConfigureAfter 与 Kotlin 的解释

spring - 如何在Spring Webflow中的最终状态externalredirect中设置一些参数?

spring - 什么是 <spring :hasBindErrors>?

spring - 警告 @Configuration 类上的非静态 ConfigurationClassPostProcessor 声明

spring web ResponseEntity 无法序列化

linux - 内存数据库中带有嵌入式 HSQL 的 Spring DataSource 和 Linux 上的 hibernate create-drop ordering

java - Spring Rest 返回带有特定 http 响应代码的 JSON 响应

java - 管理异常

java - 如何将 @SpringBootTest(webEnvironment) 与 @DataJpaTest 一起使用?