【问题标题】:HTTP Status 404 – Not Found : The origin server did not find a current representation for the target resourceHTTP 状态 404 – 未找到:源服务器未找到目标资源的当前表示
【发布时间】:2018-11-30 12:01:14
【问题描述】:

我在尝试访问 Spring 框架中的服务时遇到错误。

控制器类:-

package com.spring.mvc.tutorial;

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

@Controller
@RequestMapping("/")
public class HelloWorldController {

    @RequestMapping(method = RequestMethod.GET)
    public String sayHello(ModelMap model) {
        model.addAttribute("message", "Hello World from Spring 4 MVC");
        return "welcome";
    }

    @RequestMapping(value = "/hello", method = RequestMethod.GET)
    public String sayHelloAgain(ModelMap model) {
        model.addAttribute("message", "Hello World Again, from Spring 4 MVC");
        return "welcome";
    }
}

配置类:-

package com.spring.mvc.tutorial;

import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.ViewResolver;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import org.springframework.web.servlet.view.InternalResourceViewResolver;
import org.springframework.web.servlet.view.JstlView;

@Configuration
@EnableWebMvc
@ComponentScan(basePackages = "com.spring.mvc.tutorial")
public class HelloWorldConfiguration {

    public ViewResolver viewResolver() {
        InternalResourceViewResolver viewResolver = new InternalResourceViewResolver();
        viewResolver.setViewClass(JstlView.class);
        viewResolver.setPrefix("/WEB-INF/views/");
        viewResolver.setSuffix(".jsp");

        return viewResolver;
    }
}

初始化类:-

package com.spring.mvc.tutorial;

import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.ServletRegistration;

import org.springframework.web.WebApplicationInitializer;
import org.springframework.web.context.support.AnnotationConfigWebApplicationContext;
import org.springframework.web.servlet.DispatcherServlet;

public class HelloWorldInitializer implements WebApplicationInitializer {

    @Override
    public void onStartup(ServletContext container) throws ServletException {
        AnnotationConfigWebApplicationContext ctx = new AnnotationConfigWebApplicationContext();

        ctx.register(HelloWorldConfiguration.class);
        ctx.setServletContext(container);

        ServletRegistration.Dynamic servlet = container.addServlet("dispatcher", new DispatcherServlet(ctx));

        servlet.setLoadOnStartup(1);
        servlet.addMapping("/");
    }
}

查看:-

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>HelloWorld page</title>
</head>
<body>
    <h2>${message}</h2>
</body>
</html>

注意:- 请求:http://localhost:8080/SpringMvcHelloWorld/ 这是在 Eclipse Photon 中开发并部署到 Tomcat 8.5。

【问题讨论】:

  • 是否有任何配置将“SpringMvcHelloWorld”设置为应用程序的上下文根?您是否尝试仅在 localhost:8080 上浏览?
  • @Vitor Santos No 表示第一,Yes 表示第二。我将其部署为 SpringMvcHelloWorld.war 文件。

标签: java spring-mvc tomcat


【解决方案1】:

尝试重定向到控制器类中的主 html 页面:

 @RequestMapping(method = RequestMethod.GET)
    public String sayHello(ModelMap model) {
        model.addAttribute("message", "Hello World from Spring 4 MVC");
        return "redirect:/main.html";
    }

将 main.html 替换为你的主页路径

【讨论】:

    【解决方案2】:

    我遇到了同样的问题,并通过检查 @controller 和 @RequestMapping 等属性绑定到 mvc 包的位置来解决它

    检查源是否绑定:

    转到控制器页面并同时按下@Controller 上的控制和左键

    1. 如果source没有被绑定,就会出现如图。

    2. 如果源未绑定,则通过单击附加源按钮并指向 spring-webmvc-5.1.3.RELEASE-sources 来附加源。

    3. 按照以上两个步骤检查其他jar。

    【讨论】:

      【解决方案3】:

      我还不能发表评论,因此这是一个答案的形式。

      当 tomcat 未初始化 Web 应用程序时,我遇到了类似的问题。您能否在初始化程序中添加为 System.out.println 以查看它是否首先初始化了 webapp。

      并在此处分享您的服务器日志。

      【讨论】:

        猜你喜欢
        • 2023-01-31
        • 2021-10-02
        • 2017-11-11
        • 1970-01-01
        • 1970-01-01
        • 2017-10-06
        • 2019-09-29
        • 2019-10-26
        • 1970-01-01
        相关资源
        最近更新 更多