【问题标题】:Spring mvc - org.springframework.web.servlet.PageNotFound noHandlerFoundSpring mvc - org.springframework.web.servlet.PageNotFound noHandlerFound
【发布时间】:2016-02-13 10:40:59
【问题描述】:

网上有很多类似的问题,但没有一个能解决我的问题。我想使用基于 spring mvc 注释的方法制作简单的“hello world”应用程序,但现在这个错误停留了 1 周。

我在浏览器上点击http://localhost:8080/FirstSpringMVCProject/welcome时得到的错误是404,请求的资源不可用,控制台显示如下:

 Nov 12, 2015 11:56:12 AM org.springframework.web.servlet.PageNotFound noHandlerFound
WARNING: No mapping found for HTTP request with URI [/FirstSpringMVCProject/welcome] in DispatcherServlet with name 'dispatcher'

web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app id="WebApp_ID" version="3.0" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd">

<display-name>FirstSpringMVCProject</display-name>


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

dispatcher-servlet.xml

<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
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="com.gontuseries.hellocontroller" />

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


  </beans>

HelloController.java

package com.gontuseries.hellocontroller;


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

@Controller
public class HelloController {

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

        ModelAndView model = new ModelAndView("HelloPage");
        model.addObject("msg","hello world");

        return model;
    }

}

HelloPage.jsp

<html>
<body>
    <h1>First Spring MVC Application Demo</h1>

    <h2>${msg}</h2>

</body>
</html>

我正在使用 spring mvc 4.2 和 Apache tomcat 7.0

附: 当我使用基于非注释的方法时一切正常,我能够看到正在运行的网页,直到我使用基于注释的方法。

编辑:我的目录结构截图:

【问题讨论】:

  • 如果你执行 '@RequestMapping(value="/welcome") 会发生什么
  • @James Jithin 是的,使用 Eclipse。
  • @bmarkham 相同的结果
  • 试试这个'@RequestMapping(value= {"/", "/welcome"}, method = RequestMethod.GET)'。如果这不起作用,请在 Eclipse 中导入您的 Web 服务器,就像您的项目一样,选择“运行为...”,然后选择“在服务器上运行”
  • 尝试从你的配置中移除

标签: java spring spring-mvc


【解决方案1】:

尝试以下解决方案:

1.在eclipse中双击服务器有模块点击模块选项卡会有一个提到的路径将其更改为FirstSpringMvcProject

2.在 Eclipse 中按 alt+enter 进入属性选项卡,您可以在 web 项目设置中将路径更改为 FirstSpringMvcProject

  1. http://localhost:8080/FirstSpringMVCProject/welcome那里你将拥有你的第一个mvc spring应用程序

【讨论】:

    【解决方案2】:

    “dispacher-servlet.xml”似乎拼写错误,即它应该是“dispatcher-servlet.xml”。 它应该与您在 web.xml 中定义的 servlet 名称相匹配

    【讨论】:

    • 谢谢,写问题时拼写错误,在我的开发环境中是正确的...现在更新!!
    • 你能分享一下你的项目结构吗?
    • 我的目录结构的屏幕截图已更新,请检查。
    【解决方案3】:

    默认情况下,spring 项目将基础包名称的最后一部分作为项目名称,并将其作为 artifactId 标签添加到 pom.xml,因此请尝试点击此 url

    http://localhost:8080/hellocontroller/welcome

    【讨论】:

    • 我没有任何 pom.xml,因为我没有使用任何自动化工具,如 maven 或 gradle,只是使用基本设置。
    • 只是给出相同的结果
    【解决方案4】:

    检查你的tomcat配置文件,你的项目名称是不是:FirstSpringMVCProject,你可以尝试访问http://localhost:8080/welcome

    【讨论】:

      猜你喜欢
      • 2016-02-09
      • 2016-08-26
      • 2017-04-17
      • 2017-05-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-10-09
      • 2020-02-04
      相关资源
      最近更新 更多