Java Spring MVC控制器问题,HTTP状态404
我正在学习SpringMVC,在运行我的web应用程序时遇到了很大的麻烦。我的控制器不工作,因此我无法映射我的请求。这是我的错误: HTTP状态404–未找到 类型状态报告 说明源服务器找不到目标资源的当前表示形式,或者不愿意透露存在该表示形式 在浏览器中键入时也会发生相同的错误: 我的控制器:Java Spring MVC控制器问题,HTTP状态404,java,spring,spring-mvc,Java,Spring,Spring Mvc,我正在学习SpringMVC,在运行我的web应用程序时遇到了很大的麻烦。我的控制器不工作,因此我无法映射我的请求。这是我的错误: HTTP状态404–未找到 类型状态报告 说明源服务器找不到目标资源的当前表示形式,或者不愿意透露存在该表示形式 在浏览器中键入时也会发生相同的错误: 我的控制器: @Controller public class HelloWorldController { @RequestMapping("/showForm")
@Controller
public class HelloWorldController {
@RequestMapping("/showForm")
public String showForm() {
return "helloworld-form";
}
@RequestMapping("/processForm")
public String processForm() {
return "helloworld";
}
}
调度器servlet
<?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"
xmlns:mvc="http://www.springframework.org/schema/cache"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/cache http://www.springframework.org/schema/cache/spring-cache.xsd">
<mvc:annotation-driven/>
<context:component-scan base-package="mvcapp"/>
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="WEB-INF/view/"></property>
<property name="suffix" value=".jsp"></property>
</bean>
</beans>
web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
version="4.0">
<servlet>
<servlet-name>dispatcher</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>WEB-INF/dispatcher-servlet.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>dispatcher</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
调度员
org.springframework.web.servlet.DispatcherServlet
上下文配置位置
WEB-INF/dispatcher-servlet.xml
1.
调度员
/
文件树映像:在bean配置dispatcherservlet.xml中,
尝试将名为前缀的InternalResourceViewResolver bean的属性更正为以/
开头的相对路径:
或者写:
/WEB-INF/view/
.jsp
参见mkYong(2010):当您访问时,是否可以输入方法,您可以在返回“helloworld表单”之前添加System.out.println(“showForm”);以解决定位问题、dispather问题或jsp访问问题我扩展了tomcat输出,从内容添加了WEB-INF/classes/logging.properties:
org.apache.catalina.core.ContainerBase[catalina].level=INFO
org.apache.catalina.core.ContainerBase
[Catalina].handlers=java.util.logging.ConsoleHandler
我发现了一个例外:
2019年12月3日17:25:41.190信息[RMI TCP连接(3)-127.0.0.1]
org.apache.catalina.core.ApplicationContext.log标记servlet
[dispatcher]不可用2019年12月3日17:25:41.191严重[RMI TCP]
连接(3)-127.0.0.1]
org.apache.catalina.core.StandardContext.loadOnStartup Servlet
web应用程序[]中的[dispatcher]引发了load()异常
java.lang.ClassNotFoundException:
org.springframework.web.servlet.DispatcherServlet
(……)
所以,问题在于缺少dispatcher类和其他LIB
我修复了将所有spring库从maven添加到libs目录的问题。
非常感谢您的回复使用Spring Boot,而不是旧式配置。这将解决此问题(可能是您的模式不是/**
)。我看到您使用的是非标准端口8081
。您是否已将tomcat配置为侦听8081?(默认值为8080)我在intellij配置中仅将http端口从8080更改为8080(编辑配置…>>Tomcat服务器设置)因为我有错误“地址localhost:8080已在使用中”,这是否回答了您的问题?