gpt4 book ai didi

java - Spring Boot 调度程序 servlet 无法查找/映射静态内容

转载 作者:太空宇宙 更新时间:2023-11-04 12:18:40 25 4
gpt4 key购买 nike

我在使用 SpringBoot 运行 SpringMVC 应用程序时遇到问题。应用程序能够成功调用映射的 Controller 并解析 View ,但无法解析静态内容。

我读到 Spring Boot 自动看到以下静态资源文件夹:-静态公开Web_INF/resources 等,

我已尝试将文件保存在以下位置,但仍然没有帮助。

我在控制台上的输出是:

Inside Controller 2016-08-23 17:22:12.002 [http-nio-9990-exec-1] [org.springframework.web.servlet.PageNotFoundn] [WARN ] org.springframework.web.servlet.PageNotFound: {} - No mapping found for HTTP request with URI [/springBootApp/templates/hello.html] in DispatcherServlet with name 'dispatcherServlet'

其中 springBootApp 是我的上下文根

以下是我的类(class):-SpringBoot 类:-

@SpringBootApplication
public class ServiceApplication extends SpringBootServletInitializer {

public static void main(String[] args) {
SpringApplication app = new SpringApplication(ServiceApplication.class);
Set<ApplicationContextInitializer<?>> initLst = app.getInitializers();
initLst.add(new PropertyPasswordDecodingContextInitializer());
app.setInitializers(initLst);

ApplicationContext ctx = app.run(args);

System.out.println("Services Started");
}
@Override
protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
application = application.initializers(new PropertyPasswordDecodingContextInitializer());

return application.sources(ServiceApplication.class);
}

@Override
protected WebApplicationContext createRootApplicationContext(ServletContext servletContext) {
//project specific config
}

@Bean
public static PropertySourcesPlaceholderConfigurer propertySourcesPlaceholderConfigurer() {
return new PropertySourcesPlaceholderConfigurer();
}

}

MVCConfig类:

@EnableWebMvc
@Configuration
@ComponentScan(basePackageClasses = ServiceApplication.class, includeFilters = @Filter(Controller.class), useDefaultFilters = false)
public class WebMvcConfig extends WebMvcConfigurationSupport {

@Bean
public ViewResolver configureViewResolver() {
InternalResourceViewResolver viewResolve = new InternalResourceViewResolver();
viewResolve.setPrefix("/templates/");
viewResolve.setSuffix(".html");

return viewResolve;
}

@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
registry.addResourceHandler("/templates/**").addResourceLocations("/templates/");
}

@Override
public void configureDefaultServletHandling(DefaultServletHandlerConfigurer configurer) {
configurer.enable();
}

}

Restful Web 服务类 -

@RestController
@RequestMapping(value = "/myapp")
public class TestWebService {

@RequestMapping(value = { "/hello" }, method = { RequestMethod.GET, RequestMethod.POST })
public ModelAndView list(HttpServletRequest request) throws Exception {
// HttpSession session = request.getSession();
System.out.println("Inside Controller");
ModelAndView modelView = new ModelAndView();
ModelMap modelMap = modelView.getModelMap();
modelView.setViewName("/hello");
return modelView;
}

}

我在调用时得到上述输出:- http://localhost:9990/springBootApp/myapp/hello

最佳答案

扩展WebMvcConfigurerAdapter解决了问题

关于java - Spring Boot 调度程序 servlet 无法查找/映射静态内容,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39100617/

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