gpt4 book ai didi

java - 404-在 Spring MVC 中找不到页面

转载 作者:行者123 更新时间:2023-12-01 13:35:40 25 4
gpt4 key购买 nike

您好,我正在开发 Spring MVC 4.0。当我尝试通过 URL http://localhost:8080/bookstore-web-0.0.1-SNAPSHOT/bookstore/authentication/login 访问登录页面时,显示 404 错误。日志显示以下警告

WARN : org.springframework.web.servlet.PageNotFound - No mapping found for HTTP request with URI [/bookstore-web-0.0.1-SNAPSHOT/bookstore/authentication/login] in DispatcherServlet with name 'appServlet']]

这里 bookstore-web-0.0.1-SNAPSHOT 是我的 .war 文件。我在 web.xml 中做了以下映射

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

<!-- The definition of the Root Spring Container shared by all Servlets and Filters -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/spring/root-context.xml, /WEB-INF/spring/appServlet/security-config.xml</param-value>
</context-param>

<!-- Creates the Spring Container shared by all Servlets and Filters -->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>

<!-- Processes application requests -->
<servlet>
<servlet-name>appServlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/spring/appServlet/servlet-context.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>

<filter>
<filter-name>springSecurityFilterChain</filter-name>
<filter-class>
org.springframework.web.filter.DelegatingFilterProxy
</filter-class>
</filter>

<filter-mapping>
<filter-name>springSecurityFilterChain</filter-name>
<url-pattern>/bookstore/*</url-pattern>
<dispatcher>REQUEST</dispatcher>
<dispatcher>FORWARD</dispatcher>
</filter-mapping>

<!-- Map all /resources requests to the Resource Servlet for handling -->
<!-- <servlet-mapping>
<servlet-name>Resources Servlet</servlet-name>
<url-pattern>/resources/*</url-pattern>
</servlet-mapping> -->

<servlet-mapping>
<servlet-name>appServlet</servlet-name>
<url-pattern>/bookstore/*</url-pattern>
</servlet-mapping>

Controller 是:

package com.abhendra.bookstore;
@Controller
public class AuthenticationController {

private static final Logger logger = LoggerFactory.getLogger(AuthenticationController.class);

/**
* Simply selects the home view to render by returning its name.
*/
@RequestMapping(value = "/authentication/login", method = RequestMethod.GET)
public String home(Locale locale, Model model) {
logger.info("Welcome home! The client locale is {}.", locale);


System.out.println("In Controller!!!!!!!!!!!!!!!!!!!!!!");
return "login";
}

}

我还在我的 root-context.xml 文件中完成了组件扫描条目。像:

<context:component-scan base-package="com.abhendra.bookstore, com.abhendra.core" />
<context:annotation-config />

我不明白我在哪里做错了。

提前致谢。

最佳答案

仅仅扫描@Controller注解的类是不够的,需要指定

<mvc:annotation-driven>

这样 Spring 创建的 bean 会实际将您的 @COntroller bean 的处理程序方法映射到 URL 路径。

在由 DispatcherServlet 加载的上下文中执行此操作,即。 servlet-context.xml

<小时/>

请注意,如果您已指定

<context:component-scan .../>

你不需要

<context:annotation-config />

关于java - 404-在 Spring MVC 中找不到页面,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21293227/

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