我最近创建了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);
}
}
在进行此配置之前,默认情况下,它会通过访问 index.html
来呈现http://localhost:8084/Mascot/
。在添加这三个类之后,它会显示404,我仍然无法弄清楚,如果我在任何类中的任何地方出现错误,请帮我。
答案 0 :(得分:1)
您所需要做的就是更新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");