gpt4 book ai didi

java - 为什么spring警告消息是“org.springframework.web.servlet.PageNotFound”?

转载 作者:行者123 更新时间:2023-12-02 08:31:45 26 4
gpt4 key购买 nike

我尝试 spring 3 mvc 这个包是org.spring.test 和代码是

@Controller
@RequestMapping("/welcome")
public class WelcomeController {

private Logger logger = org.slf4j.LoggerFactory.getLogger(WelcomeController.class);


@RequestMapping(method = RequestMethod.GET)
public void welcome() {
logger.info("Welcome!");
}
@RequestMapping("test1")
public void test1() {
logger.info("test1!");
}
@RequestMapping("test2")
public void test2() {
logger.info("test2!");
}
}

web.xml 是:

<servlet>
<servlet-name>Spring MVC Dispatcher Servlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>
/WEB-INF/spring/*.xml
</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>

<servlet-mapping>
<servlet-name>Spring MVC Dispatcher Servlet</servlet-name>
<url-pattern>/*</url-pattern>
</servlet-mapping>

最后一个 mvc.xml 是

<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">
<context:component-scan base-package="org.spring.test" />
<!-- Configures support for @Controllers -->
<mvc:annotation-driven />

<!-- Resolves view names to protected .jsp resources within the /WEB-INF/views directory -->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/views/"/>
<property name="suffix" value=".jsp"/>
</bean>

当我使用“mvn jetty:run”命令并写入http://localhost:8080/springdemo/welcome时是

 "HTTP ERROR 404
Problem accessing /springdemo/WEB-INF/views/welcome.jsp. Reason:
NOT_FOUND"

控制台消息是:

INFO : org.spring.test.WelcomeController - Welcome!
WARN : org.springframework.web.servlet.PageNotFound - No mapping found for HTTP request with URI [/springdemo/WEB-INF/views/welcome.jsp] in DispatcherServlet with name 'Spring MVC Dispatcher Servlet'

为什么?

最佳答案

您似乎缺少/WEB-INF/views/test.jsp。这是真的吗?

您正在请求/test URI,它未映射到您的 Controller 类(我认为它映射到/welcome)。 Spring 使用InternalResourceViewResolver 来满足您的请求。它需要前缀+“测试”+后缀。这就是您最终得到/WEB-INF/views/test.jsp 的方式。

确保您有该 JSP 或更改您的 Controller 映射。

更新:好吧,你改变了问题。但是你有/springdemo/WEB-INF/views/welcome.jsp 吗?

关于java - 为什么spring警告消息是“org.springframework.web.servlet.PageNotFound”?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3192399/

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