HTTP状态404 –找不到:原始服务器未找到目标资源的当前表示形式

时间:2018-11-30 12:01:14

标签: java spring-mvc tomcat

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

Spring MVC Application

控制器类:-

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。

3 个答案:

答案 0 :(得分:0)

尝试重定向到控制器类中的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替换为首页的路径

答案 1 :(得分:0)

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

要检查源是否绑定:

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

  1. 如果未绑定源,则它将如图所示。 link

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

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

答案 2 :(得分:0)

我还不能发表评论,所以这是以答案的形式。

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

并在此处共享您的服务器登录。