【发布时间】:2020-03-27 14:43:22
【问题描述】:
我正在学习 Spring MVC,但在运行我的 Web 应用程序时遇到了很大的麻烦。我的控制器不工作,所以我无法映射我的请求。这是我的错误:
HTTP 状态 404 - 未找到
类型状态报告
描述 源服务器没有找到目标资源的当前表示或不愿意透露存在。
在浏览器中输入同样的错误:
http://localhost:8081/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>
【问题讨论】:
-
使用 Spring Boot,而不是旧配置。这将解决这个问题(这可能是你的模式不是
/**)。 -
我看到您使用的是非标准端口
8081。有没有配置tomcat听8081? (默认为 8080) -
我只更改了 intellij 配置中的 8080 的 http 端口(编辑配置... >> Tomcat 服务器设置),因为我有错误“地址 localhost:8080 已在使用中”
标签: java spring spring-mvc