【问题标题】:No mapping found for HTTP request with URI [/HelloWeb/] in DispatcherServlet with name 'HelloWeb' Spring MVC在名称为'HelloWeb' Spring MVC 的 DispatcherServlet 中找不到带有 URI [/HelloWeb/] 的 HTTP 请求的映射
【发布时间】:2014-04-26 20:40:21
【问题描述】:

我正在学习 Spring 框架,我正在教程点上做 HelloWeb 教程,但我无法让它工作。我正在使用 Spring MVC 4.0,并且正在将我的应用程序从 Netbeans 8.0 部署到 Glassfish 服务器。 http://www.tutorialspoint.com/spring/spring_mvc_hello_world_example.htm

我在此站点和其他站点上查找了类似的问题,但建议的解决方案对我不起作用。我真的很感激一些帮助,因为我很确定我在这里遗漏了一些基本的东西。

这是我的相关文件:

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

<display-name>Spring MVC Application</display-name>

<context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>/WEB-INF/HelloWeb-servlet.xml</param-value>
</context-param>

<listener>
    <listener-class>
        org.springframework.web.context.ContextLoaderListener
    </listener-class>
</listener>
<servlet>
    <servlet-name>HelloWeb</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <load-on-startup>2</load-on-startup>
</servlet>
<servlet-mapping>
    <servlet-name>HelloWeb</servlet-name>
    <url-pattern>/</url-pattern>
</servlet-mapping>
<session-config>
    <session-timeout>
        30
    </session-timeout>
</session-config>
</web-app>

HelloWeb-servlet.xml

<?xml version='1.0' encoding='UTF-8' ?>
<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"
xsi:schemaLocation="
http://www.springframework.org/schema/beans     
http://www.springframework.org/schema/beans/spring-beans-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">

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

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

</beans>

hello.jsp

<%@ page contentType="text/html; charset=UTF-8" %>
<html>
<head>
<title>Hello World</title>
</head>
<body>
<h2>${message}</h2>
</body>
</html>

HelloController.java

package com.tutorialspoint;

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

@Controller
@RequestMapping("/hello")
public class HelloController{

   @RequestMapping(method = RequestMethod.GET)
   public String printHello(ModelMap model) {
      model.addAttribute("message", "Hello Spring MVC Framework!");

      return "hello";
   }

}

【问题讨论】:

  • 应用的上下文路径是什么,浏览器地址栏中的URL是什么?
  • @JBNizet 应用的上下文路径是/HelloWeb,URL是localhost:8080/HelloWeb
  • 你试一试会发生什么?
  • GlassFish 服务器抛出 404 页面错误,Netbeans 控制台在标题中显示该错误。不过现在解决了,下面的答案是对的。我只是不知道为什么它现在可以工作,而以前没有:D

标签: java spring jsp spring-mvc web


【解决方案1】:

您的应用中有一个控制器,映射到 /hello(这就是 @RequestMapping("/hello") 的含义)。因此,该控制器的 URL 是 http://localhost:8080/HelloWeb/hello

【讨论】:

  • 好吧...所以我觉得有点尴尬,因为这个解决方案是在一个相关的线程中,当我尝试它没有工作时。它也不识别 /HelloWeb/hello 。现在它可以工作了,所以谢谢。我想知道 Firefox 缓存是否一直为我提供 GlassFish 404 页面。
  • 我想知道是否有可能使用了该端口,所以这就是它之前找不到它的原因。我知道 Skype 使用 8080,但这不应该完全阻止 glassfish 吗?我在这里抓住了稻草,因为到目前为止我只使用过 apache 和 php,我的网络知识仅限于其他技术。
  • 如果 Glassfish 的端口不可用,它会给你一个错误。并且 Skype 不会抛出 SpringMVC 异常。我怀疑 Firefox 会缓存 404 页面。如有疑问,请在按住 Shift 键的同时使用重新加载按钮:这会强制浏览器重新加载而不检查缓存。
  • 谢谢,尽管我使用 ctrl-shift R 而不是 F5,但我会记住这一点,所以我真的不知道。但无论如何,谢谢......下次我会更加小心。最好的问候!
  • 对不起,我还是没明白。我面临同样的问题。行动,我该怎么办?
【解决方案2】:

我希望这对你有用。 我遇到了同样的错误,但我决定更新 HelloWeb-servlet.xml 在这里你可以把这段代码放在 HelloWeb-servlet.xml

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-07-02
    • 2013-08-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多