首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >Spring的基于Java的配置项目显示404

Spring的基于Java的配置项目显示404
EN

Stack Overflow用户
提问于 2018-10-16 23:02:23
回答 1查看 69关注 0票数 1

我最近创建了Spring maven项目,我正在使用纯基于java的配置,在运行相同的配置时,我遇到了404。我在这里一无所知。

AppInitializer (web.xml更换)

代码语言:javascript
复制
package com.mzk.mascot.configuration;

import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.ServletRegistration;
import org.springframework.web.WebApplicationInitializer;
import org.springframework.web.context.ContextLoaderListener;
import org.springframework.web.context.support.AnnotationConfigWebApplicationContext;
import org.springframework.web.servlet.DispatcherServlet;

/**
 *
 * @author Ossu
 */
public class WebApplicationBootstrapper implements WebApplicationInitializer {

    @Override
    public void onStartup(ServletContext sc) throws ServletException {
        AnnotationConfigWebApplicationContext applicationContext=new AnnotationConfigWebApplicationContext();
        AnnotationConfigWebApplicationContext dispatcherContext=new AnnotationConfigWebApplicationContext();

        applicationContext.register(SpringBeanContainer.class);
        dispatcherContext.register(DispatcherServletConfiguration.class);

        ContextLoaderListener contextLoaderListener=new ContextLoaderListener(applicationContext);
        sc.addListener(contextLoaderListener);

        DispatcherServlet dispatcherServlet = new DispatcherServlet(dispatcherContext);
        ServletRegistration.Dynamic dispatcher = sc.addServlet("dispatcher", dispatcherServlet);

        dispatcher.setLoadOnStartup(1);
        dispatcher.addMapping("/");
    }
}

app Spring bean容器(-context.xml替换)

代码语言:javascript
复制
package com.mzk.mascot.configuration;

import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;

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

}

最终调度程序-servlet配置文件

代码语言:javascript
复制
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/.来呈现index.html在添加了这三个类之后,它显示404,我仍然不能弄清楚,如果我在任何类的任何地方出错了,请告诉我并纠正我。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2018-10-17 04:06:32

您所需要做的就是更新DispatcherServletConfiguration类。

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

代码语言:javascript
复制
@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
    registry.addResourceHandler("/resources/**").addResourceLocations("/resources/");
    registry.addResourceHandler("/*.html").addResourceLocations("/WEB-INF/static/");
}

然后覆盖从/(根)转发到/index.html的addViewControllers()方法

代码语言:javascript
复制
@Override
public void addViewControllers(ViewControllerRegistry registry) {
    registry.addViewController("/").setViewName("forward:/index.html");
}

最后,将静态文件移动到WEB-INF/ index.html文件夹中。

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

代码语言:javascript
复制
registry.addViewController("/").setViewName("forward:/welcome");
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/52838505

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档