首页 > 解决方案 > HTTP 状态 404 – 未找到:源服务器未找到目标资源的当前表示

问题描述

尝试访问 Spring 框架中的服务时出现错误。

Spring MVC 应用程序

控制器类:-

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。

标签: javaspring-mvctomcat

解决方案


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

要检查源是否已绑定:

进入控制器页面,同时按下@Controller 上的控制和左键

  1. 如果 source 没有绑定,就会出现如图所示。关联

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

  3. 按照上述两个步骤检查其他罐子。


推荐阅读