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

兰特姆

我正在尝试访问localhost:8080 / HelloWeb / helloWorld,但始终收到未找到文件的错误。我知道我做的映射不正确,但是作为一个初学者,我无法真正查明位置。请帮忙。

我有以下观点 WEB-INF/jsp/helloWorld.jsp

<%@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";
    }
}

抱歉,如果您以前曾问过这个问题,但我似乎无法从到目前为止所看到的答案中解决问题。再次感谢。

斯达瓦拉基

如果您在html中使用.htm *正则表达式,我认为您应该使用localhost:8080 / HelloWeb / helloWorld.html,否则应将web.xml中的servlet映射更改为/而不是.htm *

另外,如果要将属性消息添加到控制器中的模型,请确保在JSP中也使用相同的属性。所以应该是

名称:$ {message}

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

Spring MVC Hello World问题

来自分类Dev

Spring mvc 4 hello world 示例抛出 javax.servlet.ServletException:无法实例化 WebApplicationInitializer 类

来自分类Dev

Spring Hello world Not working-404 Error

来自分类Dev

Spring Hello World无法正常工作-404错误

来自分类Dev

C ++的Hello World错误

来自分类Dev

找不到简单的Jersey Hello World 404资源

来自分类Dev

Java-Hello World错误

来自分类Dev

EmberJs错误中的Hello World

来自分类Dev

Hello World无法打印。错误

来自分类Dev

Java-Hello World错误

来自分类Dev

Java Hello World Compliation错误

来自分类Dev

AS3文件系统Hello World

来自分类Dev

eclipse之外的HTTP 404 Hello World JSP

来自分类Dev

Hello World示例的Node.js错误

来自分类Dev

Nasm Hello World Bus错误OS X

来自分类Dev

Java中的Hello World投掷错误

来自分类Dev

UEFI hello world编译错误

来自分类Dev

Atlassian Hello World JIRA构建错误

来自分类Dev

Nasm Hello World Bus错误OS X

来自分类Dev

Java中的Hello World投掷错误

来自分类Dev

hello world 上的 PHP 解析错误

来自分类Dev

Spring“ Hello World”示例错误:发生了JNI错误和NoClassDefFoundError:org / springframework / beans / factory / ListableBeanFactory

来自分类Dev

Java / Wicket:使用资源编译基本Hello World

来自分类Dev

CMake-代码::块-Hello World-基本示例

来自分类Dev

基本的“ hello world”是否可以作为CGI运行?

来自分类Dev

Neo4j没有hello World输出

来自分类Dev

在Mac上编译Hello World WxWidget程序时找不到'wx / wx.h'文件

来自分类Dev

Docker - Spring 启动应用程序 - Windows 上的 Hello World

来自分类Dev

通过PInvoke的“ Hello World”