【问题标题】:404 - the requested ressource is not available404 - 请求的资源不可用
【发布时间】:2018-05-15 20:45:21
【问题描述】:

在尝试做一个关于 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>

【问题讨论】:

  • 尽量把所有的`.java'文件放到同一个目录下
  • 在实际项目中,我们不会将所有文件放在同一个包中!
  • 我知道,但是尝试一下,因为有时这个错误是由未正确配置的 ComponentScan 引起的,所以如果您尝试它并且它有效,您就会知道问题出在哪里!
  • 你不需要为 project_name 指定映射吗?
  • 可能是因为映射是@RequestMapping("/"),试试@RequestMapping("/project_name")

标签: java spring eclipse spring-mvc tomcat


【解决方案1】:

我以为当你使用没有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/*");
    }

}

【讨论】:

  • 请注意ServletInitializerWebApplicationInitializer 并不完全相同,因为WebApplicationInitializer 可以包含多个Servlets,每个Servlets 都需要自己的初始化。
【解决方案2】:

我解决了这个问题:

  • 右键单击项目名称并选择属性
  • 选择部署程序集
  • 添加 spring 和 JTL jar

【讨论】:

    猜你喜欢
    • 2018-11-01
    • 1970-01-01
    • 2013-02-16
    • 2021-11-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多