Spring Boot + JSF静态页面配置

时间:2017-03-24 11:01:21

标签: spring spring-mvc jsf

开始使用Spring Boot + JSF。 Spring文档说:

  • Spring会自动加载并提供内部资源 / static(或/ public或/ resources或/ META-INF / resources。 这不适合我。

  • 不鼓励使用“webapp”(src / main / webapp)文件夹。 但对于我的静态内容,它似乎是唯一使用我的JSF静态页面的文件夹。

请参阅Spring Boot static content

我尝试过这些配置:

@Configuration
public class JSFServletConfigs  implements ServletContextInitializer// ,ServletContextAware
{

    @Override // ServletContext initiallization parameters
    public void onStartup(ServletContext servletContext) throws ServletException {

        servletContext.setInitParameter("facelets.DEVELOPMENT", "false");
        servletContext.setInitParameter("javax.faces.PROJECT_STAGE", "Development");
        servletContext.setInitParameter("javax.faces.DEFAULT_SUFFIX", ".xhtml");
        servletContext.setInitParameter("javax.faces.PARTIAL_STATE_SAVING_METHOD", "true");
        servletContext.setInitParameter("javax.faces.FACELETS_REFRESH_PERIOD", "-1");
    }

    @Bean // Registering the FacesServlet.
    public ServletRegistrationBean servletRegistrationBean() {
        FacesServlet servlet = new FacesServlet();
        ServletRegistrationBean servletRegistrationBean = new ServletRegistrationBean(servlet) {
            @Override
            public void onStartup(ServletContext servletContext) throws ServletException {
                FacesInitializer facesInitializer = new FacesInitializer();
                Set<Class<?>> clazz = new HashSet<Class<?>>();
                clazz.add(JSFServletConfigs.class);
                facesInitializer.onStartup(clazz, servletContext);
            }
        };
        servletRegistrationBean.addUrlMappings("*.xhtml", "*.jsf", "*.html");
        return servletRegistrationBean;
    }

    @Bean
    public InternalResourceViewResolver jsfViewResolving(){
        InternalResourceViewResolver viewResolver = new InternalResourceViewResolver();
        viewResolver.setViewClass(JstlView.class);
        viewResolver.setPrefix("/<sub-folder of any LOCATION>/");
        viewResolver.setSuffix(".xhtml");       
        return viewResolver;
    }
}

Servlet pre-configs:

@Configuration
@EnableWebMvc
public class DefaultServletConfigs extends WebMvcConfigurerAdapter {

    @Override // Enabling the default Servlet at path="/"
    public void configureDefaultServletHandling(DefaultServletHandlerConfigurer configurer) {
        configurer.enable();
    }

    private final String[] LOCATIONS = {"<All my possible locations>"};

    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {

        if(!registry.hasMappingForPattern("/**")){
            registry.addResourceHandler("/**").addResourceLocations(LOCATIONS);
        }

        if(!registry.hasMappingForPattern("webjars")){
            registry.addResourceHandler("/webjars/**").addResourceLocations("classpath:/META-INF/resources/webjars/");
        }
    }
}

我的问题是:

我在这里缺少什么?

如何灵活地将资源放在上面的任何其他文件夹中 (即“/ static”,“/ public”,“/ resources”,“/ META-INF / resources”)。

0 个答案:

没有答案