gpt4 book ai didi

使用 PathVariable 的 Spring RequestMapping 结果为 404

转载 作者:可可西里 更新时间:2023-11-01 16:11:30 26 4
gpt4 key购买 nike

我有一个 spring 应用程序,但无法解决问题。当我使用 PathVariable 时,我的 RequestMapping 中的值被添加到 URI,并且 DispatchHandler 变得困惑。

例如,当我请求/Dashboard/project/1131/

我得到 HTTP 状态 404 -/Dashboard/project/1131/WEB-INF/jsp/projectDetail.jsp

项目/1131 由于某种原因被添加到路径中

当我请求 /Dashboard/projects 时,Spring 找到我的 .jsp 并将其呈现。/project/{projectId} 不起作用 - 我遇到了上述行为。

这是我的 Controller

@Controller
public class ProjectController {

protected final Log logger = LogFactory.getLog(getClass());

@RequestMapping(value = "projects", method=RequestMethod.GET)
public String projects() {
return "/projects";
}

@RequestMapping(value="/project/{projectId}", method=RequestMethod.GET)
public String project(@PathVariable("projectId") String projectId, ModelMap map) {
map.put("projectId", projectId);
logger.info("Project Id: " + projectId);
return "/projectDetail";
}

}

我的配置

网络.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
version="3.0">

<!-- Webapp name -->
<display-name>Dashboard</display-name>

<!-- default file name -->
<welcome-file-list>
<welcome-file>home</welcome-file>
</welcome-file-list>

<!-- spring listener (all spring jars need to be in WEB-INF/lib -->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>

<!-- set servlet to DispatcherService -->
<servlet>
<servlet-name>dashboard</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>

<!-- hand all requests to dispatcher service -->
<servlet-mapping>
<servlet-name>dashboard</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>

</web-app>

应用配置

@Configuration
public class AppConfig {

@Bean
ViewResolver viewResolver() {
InternalResourceViewResolver resolver = new InternalResourceViewResolver();
resolver.setPrefix("WEB-INF/jsp/");
resolver.setSuffix(".jsp");
return resolver;
}

}

仪表板-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:mvc="http://www.springframework.org/schema/mvc"
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/mvc
http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd">

<!-- the packages to scan for annotations -->
<context:component-scan base-package="com.dennisstevens.dashboard.controller" />
<context:component-scan base-package="com.dennisstevens.dashboard" />
<context:component-scan base-package="com.dennisstevens.dashboard.domain" />

<!-- Tell Spring that MVC components are @Annotation Driven -->
<mvc:annotation-driven />

<!-- Tell Spring Dispatch Handler to look in WebContent/resources/ for resources -->
<mvc:resources mapping="/resources/**" location="/resources/" />

<!-- Tell Spring to scan for config annotations -->
<context:annotation-config/>

<!-- TODO: Figure out how to load this in AppConfig -->
<bean id="messageSource"
class="org.springframework.context.support.ResourceBundleMessageSource">
<property name="basename" value="messages" />
</bean>

</beans>

这一定很简单 - 但我已经阅读了文档并在互联网上搜索了几个小时。从我所看到的看来,我所做的一切都是正确的。

最佳答案

我认为在 resolver.setPrefix("/WEB-INF/jsp/"); 中有一个前导斜杠应该可以为您修复它。

关于使用 PathVariable 的 Spring RequestMapping 结果为 404,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13638042/

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