java - 基本的 Spring 4 MVC - Hello World - 找不到错误 404 文件

标签 java spring jsp spring-mvc

我正在尝试访问 localhost:8080/HelloWeb/helloWorld,但一直收到找不到文件的错误。我知道我没有正确地绘制 map ,但作为初学者,我无法真正确定位置。请帮忙。

我在 WEB-INF/jsp/helloWorld.jsp 中有以下 View

<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>JSP Page</title>
    </head>
    <body>
        <h1>Spring MVC Web Service</h1>
        <h3>Name: ${name}</h3>
    </body>
</html>

以下是我的web.xml配置文件:

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="3.1" 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">
    <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>2</load-on-startup>
    </servlet>
    <servlet-mapping>
        <servlet-name>dispatcher</servlet-name>
        <url-pattern>*.htm</url-pattern>
    </servlet-mapping>    
    <session-config>
        <session-timeout>
            30
        </session-timeout>
    </session-config>
    <welcome-file-list>
        <welcome-file>redirect.jsp</welcome-file>
    </welcome-file-list>
</web-app>

以下是我的dispatcher-servlet.xml文件:

<?xml version='1.0' encoding='UTF-8' ?>
<!-- was: <?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:p="http://www.springframework.org/schema/p"
       xmlns:aop="http://www.springframework.org/schema/aop"
       xmlns:tx="http://www.springframework.org/schema/tx"
       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-4.0.xsd
       http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd
       http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd
       http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd
       http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd">

    <bean class="org.springframework.web.servlet.mvc.support.ControllerClassNameHandlerMapping"/>

    <!--
    Most controllers will use the ControllerClassNameHandlerMapping above, but
    for the index controller we are using ParameterizableViewController, so we must
    define an explicit mapping for it.
    -->
    <bean id="urlMapping" class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
        <property name="mappings">
            <props>
                <prop key="index.htm">indexController</prop>
            </props>
        </property>
    </bean>

    <bean id="viewResolver"
          class="org.springframework.web.servlet.view.InternalResourceViewResolver"
          p:prefix="/WEB-INF/jsp/"
          p:suffix=".jsp" />

    <!--
    The index controller.
    -->
    <bean name="indexController"
          class="org.springframework.web.servlet.mvc.ParameterizableViewController"
          p:viewName="index" />

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

</beans>

下面是我的HelloWorldController.java文件:

package controller;

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

@Controller
public class HelloWorldController {
    @RequestMapping("/helloWorld")
    public String helloWorld (Model model) {
        model.addAttribute("message", "Hello World from Controller");
        return "helloWorld";
    }
}

如果之前有人问过这个问题,我深表歉意,但我似乎无法从目前看到的答案中解决问题。再次感谢。

最佳答案

您的 dispatcherServlet 仅处理 url 模式 *.htm。将 *.htm 后缀添加到 @RequestMapping 中的 url:

@RequestMapping("/helloWorld.htm")

关于java - 基本的 Spring 4 MVC - Hello World - 找不到错误 404 文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25279532/

相关文章:

javascript - 访问 IFRAME 中的属性的权限被拒绝

java - 如何使用表单 :select outside the form? spring、hibernate 中的项目

java.io.FileNotFoundException - 文件确实存在于目录中

java - 如何使用 Java 类在 Cassandra 中创建 KEYSPACE

java - 在 Gae 上创建一个大的 Zip 文件(在 blobstore 中)

java - JUnit 5 - 使用 JUnit Jupiter 引擎时 IntelliJ IDEA 中的空测试套件

java.net.UnknownHostException : http://localhost:8082/consume/create

java - 如何在 Wildfly 中将外部属性文件加载到 Spring Boot

java - spring jpa @Query 错误,期待关闭,发现附近有 '('

java - 连接到 .java 类的 .JSP 和 servlet