gpt4 book ai didi

java - Spring MVC Controller 问题,HTTP 状态 404

转载 作者:行者123 更新时间:2023-12-01 19:43:49 25 4
gpt4 key购买 nike

我正在学习 Spring MVC,但在运行我的 Web 应用程序时遇到了很大的麻烦。我的 Controller 无法工作,因此我无法映射我的请求。这是我的错误:

HTTP Status 404 – Not Found

Type Status Report

Description The origin server did not find a current representation for the target resource or is not willing to disclose that one exists.

当我在浏览器中输入时出现同样的错误:

http://localhost:8081/showForm

http://localhost:8081/mvcapp/showForm

http://localhost:8081/processForm

我的 Controller :

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

文件 TreeMap 像:/image/QSNlg.png

最佳答案

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

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

关于java - Spring MVC Controller 问题,HTTP 状态 404,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59148195/

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