gpt4 book ai didi

java - Spring基于Java的Config项目显示404

转载 作者:行者123 更新时间:2023-11-30 05:57:55 24 4
gpt4 key购买 nike

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

AppInitializer(web.xml 替换)

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("/");
}
}

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

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 {

}

最后是Dispatcher-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/ 呈现 index.html。添加这三个类后,显示404,我仍然无法弄清楚,如果我在任何类中的任何地方错误,请帮助我,请告诉我并纠正我。

最佳答案

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

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

@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
registry.addResourceHandler("/resources/**").addResourceLocations("/resources/");
registry.addResourceHandler("/*.html").addResourceLocations("/WEB-INF/static/");
}

然后重写 addViewControllers() 方法,该方法从 / (根)转发到 /index.html:

@Override
public void addViewControllers(ViewControllerRegistry registry) {
registry.addViewController("/").setViewName("forward:/index.html");
}

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

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

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

关于java - Spring基于Java的Config项目显示404,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52838505/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com