Spring MVC 控制器问题,HTTP 状态 404



我正在学习Spring MVC,我在运行Web应用程序时遇到了很大的麻烦。我的控制器不工作,所以我无法映射我的请求。这是我的错误:

HTTP 状态 404 – 未找到

类型状态报告

说明 源服务器未找到目标资源的当前表示形式,或者不愿意透露目标资源存在。

当我在浏览器中输入时,会发生同样的错误:

http://localhost:8081/showForm

http://localhost:8081/mvcapp/showForm

http://localhost:8081/processForm

我的控制器:

@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>

网络.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>

文件树图像:https://i.stack.imgur.com/QSNlg.png

在你的bean 配置dispatcher-servlet.xml中, 尝试将InternalResourceViewResolverBean 的名为前缀的属性更正为以/开头的相对路径:

<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/view/"></property>
<property name="suffix" value=".jsp"></property>
</bean>

或者写成:

<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix">
<value>/WEB-INF/view/</value>
</property>
<property name="suffix">
<value>.jsp</value>
</property>
</bean>

参见mkYong(2010(: Spring MVC InternalResourceViewResolver 示例

当你访问 http://localhost:8081/showForm 时,它可以输入method吗,你可以在返回"helloworld-form"之前添加System.out.println("showForm"(;到位置属性,差异器属性或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

我发现了这个异常:

03-Dec-2019 17:25:41.190 信息 [RMI TCP 连接(3(-127.0.0.1] org.apache.catalina.core.ApplicationContext.log Marking servlet [调度程序] 不可用 03-Dec-2019 17:25:41.191 严重 [RMI TCP 连接(3(-127.0.0.1] org.apache.catalina.core.StandardContext.loadOnStartup Servlet Web 应用程序中的 [调度程序] [] 抛出了 load(( 异常 java.lang.ClassNotFoundException: org.springframework.web.servlet.DispatcherServlet

(...

因此,问题在于缺少调度程序类和其他库。

我修复了这个问题,将所有弹簧库从 maven 添加到库目录。 https://stackoverflow.com/a/24586000/12296902 非常感谢您的回复

相关内容

最新更新