【问题标题】:spring mvc unable to map urlspring mvc 无法映射 url
【发布时间】:2018-07-04 02:48:56
【问题描述】:

我的dispatcher 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>

当我在 localhost 中运行应用程序时,我仍然收到 404 错误,因为 “源服务器没有找到目标资源的当前表示,或者不愿意透露存在的表示。” 请指导我哪里出错了

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

【问题讨论】:

    标签: spring-mvc url resources mapping


    【解决方案1】:

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

    所以删除@ResponseBody 并返回到类似于下面的查看页面:

    @Controller
    public class HomeController {
    
        @RequestMapping("/home")
        public String goHome()
        {
            return "welcome";//if the suffix is jsp
            //return "welcome.jsp"; // if do not have suffix in your spring configuration file
        }
    }
    

    【讨论】:

      【解决方案2】:

      请移除@ResponseBody,因为dispatcher servlet 应该明白返回值不是HTTP 响应,而是视图名称。并且请确保返回值应该与您的视图页面名称相同。

      【讨论】:

        【解决方案3】:

        实际上,我只是试图将该字符串发布为 ResponseBody。 这是对我有用的方法,我从一个新项目开始,并进行了以下更改

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

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

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2012-09-14
          • 1970-01-01
          • 1970-01-01
          • 2012-08-25
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多