404 - 请求的资源不可用

时间:2018-05-15 20:45:21

标签: java spring eclipse spring-mvc tomcat

在尝试做一个关于Spring MVC的简单程序时,我在运行http://localhost:8080/project_name/时总是遇到同样的错误:

404 - the requested resource is not available

这是项目的架构:

src
|    +--com
|            +--memorynotfound
|                +--config
|                    |--ServletInitializer.java
|                    |--WebConfig.java
|                +--controller
|                    |--HomeController.java
|    +--resources
|    +--webapp
|        +--WEB-INF
|            +--views
|                |--index.jsp

以下是包com.memorynotfound.config中的文件:

  

ServletInitializer.java

package com.memorynotfound.config;

import org.springframework.web.servlet.support.AbstractAnnotationConfigDispatcherServletInitializer;

public class ServletInitializer extends AbstractAnnotationConfigDispatcherServletInitializer {

    @Override
    protected Class<?>[] getServletConfigClasses() {
        return new Class[] { WebConfig.class };
    }

    @Override
    protected String[] getServletMappings() {
        return new String[] { "/" };
    }

    @Override
    protected Class<?>[] getRootConfigClasses() {
        return null;
    }

}
  

WebConfig.java

package com.memorynotfound.config;

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

@Configuration
@EnableWebMvc
@ComponentScan(basePackages="com.memorynotfound")
public class WebConfig extends WebMvcConfigurerAdapter {

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

com.memorynotfound.controller中的文件:

  

HomeController.java

package com.memorynotfound.controller;

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

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

    @RequestMapping(method = RequestMethod.GET)
    public String index(ModelMap model){
        System.out.println("This is a test ================>");
        model.addAttribute("message", "Spring MVC Java Configuration Example");
        return "index";
    }

}

最后是Jsp文件:

  

的index.jsp

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>Spring MVC Java Configuration Example</title>
</head>
<body>

    ${message}

</body>
</html>

2 个答案:

答案 0 :(得分:0)

我认为当你在没有web.xml文件的情况下使用Spring MVC时,you needed to supply an implementation of WebApplicationInitializer

如果没有这样的初始化程序,您仍需要提供web.xml文件。

最小的WebApplicationInitializer实例应该使用setLoadOnStartup(1)并为调度程序提供映射。

从上面引用的网页

public class MyWebApplicationInitializer implements WebApplicationInitializer {

    @Override
    public void onStartup(ServletContext container) {
        ServletRegistration.Dynamic registration = container.addServlet("dispatcher", new DispatcherServlet());
        registration.setLoadOnStartup(1);
        registration.addMapping("/example/*");
    }

}

答案 1 :(得分:0)

我通过这样做解决了这个问题:

  • 右键单击项目名称并选择属性
  • 选择部署程序集
  • 添加弹簧和JTL罐子