Spring MVC无法映射URL

时间:2018-06-24 19:34:39

标签: spring-mvc url resources mapping

我的调度程序servlet如下

<?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:mvc="http://www.springframework.org/schema/mvc"
    xmlns:context="http://www.springframework.org/schema/context"
    xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.3.xsd
        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-4.3.xsd">

<mvc:annotation-driven/>
<context:component-scan base-package="com.springimplant.mvc.controllers"/>

</beans>

我在名称空间Home控制器下有一个home控制器,如下所示:

    package com.springimplant.mvc.controllers;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
public class HomeController {

    @RequestMapping("/home")
    @ResponseBody
    public String goHome()
    {
        return "Welcome Home";
    }
}

Web.xml文件是

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" version="3.1">
  <display-name>course-project</display-name>

  <listener>
    <listener-class>org.springframework.web.context.ContextLoaderListner</listener-class>
  </listener>

  <context-param>
  <param-name>contextConfigLocation</param-name>
  <param-value>/WEB-INF/spring/applicationContext.xml</param-value>
  </context-param>

  <servlet>
    <servlet-name>dispatcher</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <init-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>/WEB-INF/spring/*-servlet.xml</param-value>
    </init-param>
    <load-on-startup>1</load-on-startup>
  </servlet>

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

在本地运行应用程序时仍然出现404错误,原因是 “原始服务器找不到目标资源的当前表示,或不愿意透露该资源的存在。” 请指导我要去哪里

我点击的网址是 http://localhost:8080/course-project/home

3 个答案:

答案 0 :(得分:0)

原因是您在1337方法中添加了@ResponseBody,因此它不会返回页面,而只会返回一个纯字符串,但是在您的情况下,我们没有地方显示字符串

因此,删除goHome()并返回到类似于以下内容的视图页面:

@ResponseBody

答案 1 :(得分:0)

请删除@ResponseBody,因为调度程序Servlet应该了解返回值不是HTTP响应,而是视图名称。并且请确保返回值应与您的视图页面名称相同。

答案 2 :(得分:0)

实际上,我试图将该字符串仅作为ResponseBody发布。 这是我的工作,我从一个新项目开始,进行了以下更改

  1. 再次下载STS(Spring工具套件)(我的控制台调试日志未显示)。
  2. 使用Tomcat 8.5而不是关键TC服务器4.0,您可以尝试基于Tomcat 8.5的关键TC 3.2-3.5。
  3. 我正在使用4.0的Dynamic Web Module Facet,将其降级为3.1。(我看到4.0支持@RestController注释而不是@Controller注释)
  4. 我正在使用slf4j-log4j13 facet和log4j实现,这给了一个错误,例如找不到类“ log.logger”。因此,我添加了另一个方面“ log4j-simple”。

尽管我仍然知道其中有什么对我有用:) 感谢您的回复。 这是在github上 https://github.com/gauravmatta/springmvc/tree/master/my-project