弹簧&x27;s基于Java的配置项目显示404

弹簧&x27;s基于Java的配置项目显示404,java,spring,Java,Spring,我最近创建了SpringMaven项目,我使用的是纯java配置,在运行同样的配置时,我遇到了404。我在这里一窍不通 AppInitializer(web.xml替换) SpringBean容器(app-context.xml替换) 最终调度程序servlet配置文件 package com.mzk.mascot.configuration; import org.springframework.context.annotation.Bean; import org.springframew

我最近创建了SpringMaven项目,我使用的是纯java配置,在运行同样的配置时,我遇到了404。我在这里一窍不通

AppInitializer(web.xml替换)

SpringBean容器(app-context.xml替换)

最终调度程序servlet配置文件

package com.mzk.mascot.configuration;

import org.springframework.context.annotation.Bean;
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.ContentNegotiationConfigurer;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistration;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
import org.springframework.web.servlet.view.InternalResourceViewResolver;

/**
 *
 * @author Ossu
 */
@Configuration
@EnableWebMvc
@ComponentScan({"com.mzk.mascot.controller"})
public class DispatcherServletConfiguration implements WebMvcConfigurer {

    @Bean
    ViewResolver viewResolver() {
        InternalResourceViewResolver viewResolver = new InternalResourceViewResolver();
        viewResolver.setPrefix("WEB-INF/view/");
        viewResolver.setSuffix(".jsp");
        return viewResolver;
    }

    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        ResourceHandlerRegistration handlerRegistration = registry.addResourceHandler("/resources/**");
        handlerRegistration.addResourceLocations("/resources/");
    }

    @Override
    public void configureContentNegotiation(ContentNegotiationConfigurer configurer) {
        configurer.favorPathExtension(false);
    }

}

在此配置之前,默认情况下,它通过访问
http://localhost:8084/Mascot/
。添加这三个类后,它显示404,我仍然无法理解,请帮助我,如果我在任何类中的任何位置出错,请告诉我并更正。

您只需更新
DispatcherServletConfiguration

首先更新
addResourceHandlers()
方法并注册静态HTML页面处理程序:

@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
    registry.addResourceHandler("/resources/**").addResourceLocations("/resources/");
    registry.addResourceHandler("/*.html").addResourceLocations("/WEB-INF/static/");
}
然后重写从/(root)转发到/index.html的
addViewControllers()
方法:

最后将index.html文件移到WEB-INF/static文件夹中

注意:如果您的index.html页面只是在其他地方重定向(例如在/welcome上),那么您可以直接转发:

registry.addViewController("/").setViewName("forward:/welcome");

thanx dude,一定会执行你的建议并通知你是否对我有用:)谢谢yuo bro,但我还是被卡住了,我实现了相同的,但无法摆脱404。我使用了与你在问题中使用的相同的类,并且有效。你使用什么版本的Spring?当你尝试时会发生什么?您是否有这一行
registry.addResourceHandler(“/*.html”).addResourceLocations(“/WEB-INF/static/”)
addResourceHandlers()
方法中?index.html在/WEB-INF/static文件夹中吗?@user9634982如何处理JSP是在
viewsolver()中定义的(在问题中)。但是在
addResourceHandlers()
方法中将JSP注册为资源毫无意义。删除此行:
registry.addResourceHandler(“*/*.jsp”).addResourceLocations(“/WEB-INF/view/”)
@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
    registry.addResourceHandler("/resources/**").addResourceLocations("/resources/");
    registry.addResourceHandler("/*.html").addResourceLocations("/WEB-INF/static/");
}
@Override
public void addViewControllers(ViewControllerRegistry registry) {
    registry.addViewController("/").setViewName("forward:/index.html");
}
registry.addViewController("/").setViewName("forward:/welcome");