gpt4 book ai didi

spring - URL 映射问题 - Spring Web MVC

转载 作者:IT老高 更新时间:2023-10-28 13:53:48 24 4
gpt4 key购买 nike

我是 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/

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