【问题标题】:Spring MVC returns 404Spring MVC 返回 404
【发布时间】:2019-10-22 21:03:08
【问题描述】:

我正在尝试启动并运行带有 Tomcat 9.0.20 和 IntelliJ 2019.1 的 Hello World Spring MVC 4.3.18,以继续我的 Udemy 教程,不幸的是它使用了 Eclipse。如何通过控制器显示页面?

我检查了 web.xml、dispatcher-servlet.xml 和 applicationContext.xml 是否存在配置错误。 我检查了我的项目是否有错误,IntelliJ 说没有错误。 我从 GitHub 下载了一个正在运行的 IntelliJ 项目:https://github.com/vivlai/SpringMVC(将其更改为 java 12 和 tomcat 9)并成功运行。 我比较了所有文件、目录、配置,唯一的区别是使用的 spring 版本(4.3.10)和一些 xml 版本。

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">
    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>/WEB-INF/applicationContext.xml</param-value>
    </context-param>
    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>
    <servlet>
        <servlet-name>dispatcher</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <load-on-startup>1</load-on-startup>
    </servlet>
    <servlet-mapping>
        <servlet-name>dispatcher</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping>
</web-app>

调度程序-servlet.xml

<?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: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.xsd
       http://www.springframework.org/schema/mvc
       http://www.springframework.org/schema/mvc/spring-mvc.xsd
       http://www.springframework.org/schema/context
       http://www.springframework.org/schema/context/spring-context.xsd">

    <mvc:annotation-driven/>
    <context:component-scan base-package="spring.mvc"/>

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

</beans>

applicationContext.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
       http://www.springframework.org/schema/beans/spring-beans.xsd">

</beans>

HomeController.java

package spring.mvc;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

@Controller
public class HomeController {

    @RequestMapping("/")
    public String showPage() {
        return "index";
    }
}

所以部署成功,它注册了我的映射请求,并且 InternalResourceViewResolver 指向 WEB-INF 中的视图文件夹,它应该准备好一切了吗?

所以我希望在启动服务器时显示 index.jsp,但我得到了 404 附加说明:源服务器没有找到目标资源的当前表示或不愿意透露存在的表示。 任何帮助表示赞赏!

编辑:

【问题讨论】:

    标签: java spring spring-mvc intellij-idea tomcat9


    【解决方案1】:

    按照这个过程:

    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_3_1.xsd"
        version="3.1">
        <display-name>Project</display-name>
    
        <!-- Configure Spring MVC Dispatcher Servlet -->
        <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/applicationContext.xml</param-value>
            </init-param>
            <load-on-startup>1</load-on-startup>
        </servlet>
    
        <!-- Set up URL mapping for Spring MVC Dispatcher Servlet -->
        <servlet-mapping>
            <servlet-name>dispatcher</servlet-name>
            <url-pattern>/*</url-pattern>
        </servlet-mapping>
    
    </web-app>
    

    只创建一个applicationContext.xml文件(创建dispatcher-servlet.xml是不必要且错误的):

    <?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/mvc"
        xsi:schemaLocation="
            http://www.springframework.org/schema/beans
            http://www.springframework.org/schema/beans/spring-beans.xsd
            http://www.springframework.org/schema/context
            http://www.springframework.org/schema/context/spring-context.xsd
            http://www.springframework.org/schema/mvc
            http://www.springframework.org/schema/mvc/spring-mvc.xsd
        ">
    
        <mvc:annotation-driven/>
        <context:component-scan base-package="spring.mvc"/>
    
        <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
          <property name="prefix" value="/WEB-INF/views/"/>
          <property name="suffix" value=".jsp"/>
       </bean>
    
    </beans>
    

    这就够了。只需运行您的项目。它会起作用的。

    我对 Intellij IDE 不太了解:

    但我找到了一个网址:https://medium.com/@yuntianhe/create-a-web-project-with-maven-spring-mvc-b859503f74d7

    这将帮助您正确设置项目。

    【讨论】:

    • 您确定是错误的调度员吗?现在我收到一个警告并且 404 仍然存在jsp] 在 DispatcherServlet 中,名称为 'dispatcher' 在视图后添加斜杠无济于事。
    • 已更新。只是错过了内部视图解析器中的斜线
    • 我也注意到了这一点并更改了我的评论,警告和 404 仍然存在。
    • 您是否在视图中添加了页面“index.jsp”??
    • 好吧,本教程确实有效,但我仍然想知道我的错误。有什么不兼容的地方吗?
    猜你喜欢
    • 2015-08-16
    • 2019-03-10
    • 2015-05-06
    • 1970-01-01
    • 2018-12-08
    • 2014-11-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多