控制台输出:org.springframework.web.servlet.PageNotFound noHandlerFound HTTP状态404:请求的资源不可用

时间:2017-12-15 02:30:51

标签: java spring-mvc tomcat

之前我曾见过类似的问题。我尝试了提供给他们的解决方案,但仍面临同样的问题。 我正在尝试运行简单的hello world spring mvc应用程序。 我得到的输出是HTTP Status 404

在控制台中, I can see following message.

This is my project structure

以下是代码文件。

的web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
xmlns="http://java.sun.com/xml/ns/javaee" 
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee 
http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" version="3.0">
<display-name>HelloWorldApplication</display-name>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>

<servlet>
    <servlet-name>HelloWorld</servlet-name>
    <servlet-class>
        org.springframework.web.servlet.DispatcherServlet
    </servlet-class>
    <load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
    <servlet-name>HelloWorld</servlet-name>
    <url-pattern>/welcome.jsp</url-pattern>
    <url-pattern>/index.jsp</url-pattern>
    <url-pattern>/welcome.html</url-pattern>
    <url-pattern>*.html</url-pattern>
</servlet-mapping>

</web-app>

的HelloWorld-servlet.xml中

<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:context="http://www.springframework.org/schema/context"
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
    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">

<context:component-scan base-package="com.HelloWorld.controller" />

<bean id="viewResolver"
    class="org.springframework.web.servlet.view.UrlBasedViewResolver">
    <property name="viewClass"
        value="org.springframework.web.servlet.view.JstlView" />
    <property name="prefix" value="/WEB-INF/jsp/" />
    <property name="suffix" value=".jsp" />
</bean> 

HelloWorldMain.java

package com.HelloWorld.controller;

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

/*
 * author: Siddhesh Jethe
 * 
 */

@Controller
public class HelloWorldMain {

@RequestMapping("/welcome")
public ModelAndView helloWorld() {

    String message = "<br><div style='text-align:center;'>"
            + "<h3>********** Hello World </h3> Welcome**********</div><br><br>";
    return new ModelAndView("welcome", "message", message);
}
}

的index.jsp

<html>
<head>
<title>Test project</title>
<style type="text/css">

</style>
</head>
<body>
    <br>
    <div style="text-align:center">
        <h2>
            test<br> <br>
        </h2>
        <h3>
            <a href="welcome.html">Click here to See Welcome Message... </a>
        </h3>
    </div>
 </body>
 </html>

2 个答案:

答案 0 :(得分:0)

您在浏览器的Devtools的“网络”标签中看到了什么Http请求? 这与您的控制器匹配吗? 根据您的配置,应该为您的welcome.jsp命中URL http://<servername>:<port>/HelloWorld/welcome

答案 1 :(得分:0)

我能够解决问题。我刚删除了我在web.xml中编写的所有url模式,只添加了一个“/”。它起作用了。