2016-08-23 91 views
1

我在使用SpringBoot運行SpringMVC應用程序時遇到了問題。應用程序能夠調用映射控制器併成功解析視圖,但無法解析靜態內容。Spring Boot調度程序servlet無法找到/映射靜態內容

我已閱讀,春天開機時自動看到了靜態資源的以下文件夾: - staticpublic Web_INF/resources等,

我試圖保持在以下幾個地方我的文件,但它仍然沒有幫助。

我的在控制檯上的輸出是:

內部控制器2016年8月23日17:22:12.002 [HTTP-NIO-9990-EXEC-1] [org.springframework.web.servlet.PageNotFoundn ] [WARN] org.springframework.web.servlet.PageNotFound:{} - 沒有映射找到 用於與HTTP請求的URI [/springBootApp/templates/hello.html]在 DispatcherServlet的名爲 '的DispatcherServlet'

其中springBootApp是我的上下文根

下面是我的班: - 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(); 
    } 

} 

MVCConfigClass:

@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

+0

「@ RestController」旨在直接返回*,例如JSON或XML。如果您想呈現HTML模板,請使用簡單的「@ Controller」。 – chrylis

+0

將會有更多的方法將會返回jsons – Saumyaraj

+1

@Saumyaraj然後你應該用'@ ResponseBody'或者使用單獨的控制器來註釋這些方法。 – g00glen00b

回答

0

擴展WebMvcConfigurerAdapter解決了問題

0

春季開機自動查找src/main/resources文件夾中的資源。 但是如果您爲資源處理實現WebMvcConfigurationSupport方法,那麼它將刪除資源處理方法的默認實現。

+0

是的,我有overidden的addResource處理程序,但仍然它無法獲得靜態資源 – Saumyaraj

相關問題