这两周需要写一个页面来请求另一个服务中的接口,服务器采用了超轻量级的undertow,模板引擎采用的是Thymeleaf,在寻找页面资源位置这个地方难住了我。下面分享一下,这方面的代码。
SpringWebConfig方面:
public class SpringWebConfig extends WebMvcConfigurerAdapter { private static boolean initialized = false; @Bean public static PropertySourcesPlaceholderConfigurer config() { return SpringConfigHelper.createPlaceholderConfigBean("classpath:prime.cfg"); } /** * Spring容器初始化完成后回调该函数 * 通过该机制可以动态扩展bean实例配置 * 用于工作服务框架加载工作服务实现相关bean * * @param basePackage 扩展bean扫描路径 * @return ApplicationListener实例 */ @Bean public ApplicationListener<ContextRefreshedEvent> initializedEvent( @Value("${Optimus.Prime.ServiceBasePackage:}") String basePackage) { return new ApplicationListener<ContextRefreshedEvent>() { @Override public void onApplicationEvent(ContextRefreshedEvent event) { if (!initialized) { initialized = true; SpringConfigHelper.appendBeanFromPackage( (AnnotationConfigWebApplicationContext) event.getApplicationContext(), basePackage); } } }; } /** * 模板解析器 * @return 解析器 */ @Bean public SpringResourceTemplateResolver templateResolver() { SpringResourceTemplateResolver resolver = new SpringResourceTemplateResolver(); resolver.setPrefix("classpath:/template/"); resolver.setSuffix(".html"); resolver.setTemplateMode(TemplateMode.HTML); resolver.setCacheable(false); resolver.setCharacterEncoding("UTF-8"); return resolver; } /** * 解析器 * @param templateResolver 解析器 * @return */ @Bean public SpringTemplateEngine templateEngine(SpringResourceTemplateResolver templateResolver) { SpringTemplateEngine engine = new SpringTemplateEngine(); engine.setTemplateResolver(templateResolver); return engine; } /** * 视图解析器 * @param templateEngine 解析器 * @return 视图解析 */ @Bean public ThymeleafViewResolver viewResolver(SpringTemplateEngine templateEngine) { ThymeleafViewResolver resolver = new ThymeleafViewResolver(); resolver.setTemplateEngine(templateEngine); resolver.setCharacterEncoding("UTF-8"); return resolver; }} UndertowServer 的启动方法:
public int start() { DeploymentInfo servletBuilder = Servlets.deployment() .setClassLoader(UndertowServer.class.getClassLoader()) .setContextPath("/TAE/rest/v1") .setDeploymentName("test.war") .addServletContextAttribute( SpringContextListener.PARENT_CONTEXT, context) .addListeners( Servlets.listener(SpringContextListener.class)) .addServlets( Servlets.servlet("resource", DispatcherServlet.class) .addInitParam("contextClass", "org.springframework.web.context.support" + ".AnnotationConfigWebApplicationContext") .addInitParam("contextConfigLocation", configClass) .addMapping("/*") .setLoadOnStartup(1) ) .addServletContextAttribute(WebSocketDeploymentInfo.ATTRIBUTE_NAME, new WebSocketDeploymentInfo() .setBuffers(new DefaultByteBufferPool(true, 100)) ); DeploymentManager manager = Servlets.defaultContainer() .addDeployment(servletBuilder); manager.deploy(); try { PathHandler path = Handlers.path(Handlers.redirect("/")) .addPrefixPath("/", manager.start()); server = Undertow.builder() .addHttpListener(port, host) .setHandler(path) .build(); server.start(); } catch (Exception e) { log.fatal("cannot start web server", e); return CommonError.FATAL_FAIL; } return CommonError.SUCCESS;}
原文地址:https://www.cnblogs.com/jetHu/p/8634769.html
时间: 2024-10-11 23:27:43