【问题标题】:No mapping found for HTTP request with URI [/Spring4MVCHelloWorld/hello/] in DispatcherServlet with name 'dispatcher'在名称为“dispatcher”的 DispatcherServlet 中找不到具有 URI [/Spring4MVCHelloWorld/hello/] 的 HTTP 请求的映射
【发布时间】:2014-06-20 20:18:56
【问题描述】:

我尝试了这里提到的 SpringMVC 示例-http://javahash.com/spring-4-mvc-hello-world-tutorial-full-example/。我自定义了 web.xml 和调度程序 servlet,但没有得到结果。

当我点击 URL-/Spring4MVCHelloWorld/hello/?name=JavaHash 时,我得到了错误页面标记下配置的错误页面

但是,如果我点击 URL - /Spring4MVCHelloWorld/ 我得到欢迎文件列表中指定的页面,即 index.jsp 页面。

预期的行为是当用户点击第一个 URL 时应该加载 helloworld.jsp。它工作正常,但我更改了调度程序 servlet 和 web.xml 文件中的某些内容。

这是我的 web.xml 文件 -

<web-app id="WebApp_ID" version="2.4"
  xmlns="http://java.sun.com/xml/ns/j2ee"
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee
         http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">

<display-name>Web Application</display-name>

 <welcome-file-list>
   <welcome-file>index.jsp</welcome-file>
</welcome-file-list>

<context-param>
     <param-name>contextConfigLocation</param-name>
     <param-value>/WEB-INF/dispatcher-servlet.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>1</load-on-startup>
 </servlet>

 <servlet-mapping>
     <servlet-name>dispatcher</servlet-name>
     <url-pattern>/</url-pattern>
 </servlet-mapping>

 <error-page>
    <error-code>404</error-code>
    <location>/WEB-INF/errors/404.jsp</location>
</error-page>

这是我的 dispatcher-servlet 文件 -

<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-3.0.xsd
         http://www.springframework.org/schema/context
         http://www.springframework.org/schema/context/spring-context-3.0.xsd">

 <context:component-scan base-package="com.javahash.spring.controller" />
 <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
     <property name="prefix">
        <value>/WEB-INF/views/</value>
     </property>
     <property name="suffix">
        <value>.jsp</value>
     </property>
 </bean>

这里是控制器文件 -

@Controller
 public class HelloWorldController { 

@RequestMapping("/hello")
public String hello(@RequestParam(value="name", required=false, defaultValue="World") String name, Model model) {
    model.addAttribute("name", name);
    return "helloworld";
}
}

helloWorld.jsp 有以下内容-

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"      "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
  <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
  <title>Spring4 MVC -HelloWorld</title>
</head>
<body>
   <h1>Hello : ${name}</h1> 
 </body>
 </html>

【问题讨论】:

  • 您正在点击/Spring4MVCHelloWorld/hello/?name=JavaHash,但我认为您应该点击/Spring4MVCHelloWorld/hello?name=JavaHash。我很确定它会起作用,如果它起作用,请告诉我,以便我可以将其添加为答案
  • 你的代码在 GitHub 上可以让我看看吗?
  • 你能不能试试这里给出的 tseps javahash.com/spring-4-mvc-hello-world-tutorial-full-example。它按预期工作正常,但现在不行。同时我会将代码上传到 GITHub。
  • 我可以关注它,但这可能无法帮助我解决问题。我需要访问您的代码。如果您在 Github 上没有它,也许您可​​以将其存档并上传到某些共享服务

标签: java spring spring-mvc


【解决方案1】:

如果您的 viewName 是 helloWorld.jsp,您的控制器方法 hello 应该返回“helloWorld”。视图名称区分大小写。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-08-04
    • 2018-01-05
    • 1970-01-01
    • 2011-03-30
    • 2016-04-18
    • 1970-01-01
    • 2016-12-06
    • 2016-10-24
    相关资源
    最近更新 更多