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

标签 java spring spring-mvc spring-boot

我在使用 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/

相关文章:

java - 如何使用java和JSch停止linux上的tomcat服务器?

java - Spring初始化方法

java - 如何获取 Spring 当前运行的计划任务/作业列表?

java - 用于字符串列表的 Initbinder

javascript - 奇怪的文字 : "#_=_" showing on the URL after successful Login to Facebook with spring social

java - 使用php将base64图像从android上传到服务器时出错

java - Eclipse:避免将某些依赖项包含在 JUnit 运行时类路径中

java - 图像没有在正确的时刻显示在屏幕上 - Java

java - jsp 未在请求的 uri 上呈现

java - 如何配置 Spring REST 服务来处理多个版本?