对于最新稳定版本,请使用Spring Framework 7.0.1spring-doc.cadn.net.cn

查看技术

Spring WebFlux 中视图技术的使用是可插拔的。无论你决定使用Thymeleaf、FreeMarker,还是其他视图技术,主要取决于配置的更改。本章介绍了与 Spring WebFlux 集成的视图技术。WebFlux。我们假设你已经熟悉视图分辨率spring-doc.cadn.net.cn

西梅勒夫

Thymeleaf 是一个现代的服务器端 Java 模板引擎,强调自然的 HTML模板,可以通过双击在浏览器中预览,这非常对于独立于 UI 模板的开发(例如设计师)工作非常有帮助,无需运行服务器。Thymeleaf 提供了丰富的功能,并且正在积极开发并得到维护。如需更完整的介绍,请参见 Thymeleaf 项目主页。spring-doc.cadn.net.cn

Thymeleaf 与 Spring WebFlux 的集成由 Thymeleaf 项目管理。 这 配置包含一些豆子声明,例如SpringResourceTemplateResolver,SpringWebFlux模板引擎ThymeleafReactiveViewResolver. 更多详情请参见Thymeleaf+Spring及WebFlux集成公告spring-doc.cadn.net.cn

自由评分

Apache FreeMarker 是一个模板引擎,用于生成任何从 HTML 到电子邮件及其他类型的文本输出。Spring 框架内置了用于使用 Spring WebFlux 与 FreeMarker 模板的集成。spring-doc.cadn.net.cn

查看配置

以下示例展示了如何将FreeMarker配置为视图技术:spring-doc.cadn.net.cn

@Configuration
@EnableWebFlux
public class WebConfig implements WebFluxConfigurer {

	@Override
	public void configureViewResolvers(ViewResolverRegistry registry) {
		registry.freeMarker();
	}

	// Configure FreeMarker...

	@Bean
	public FreeMarkerConfigurer freeMarkerConfigurer() {
		FreeMarkerConfigurer configurer = new FreeMarkerConfigurer();
		configurer.setTemplateLoaderPath("classpath:/templates/freemarker");
		return configurer;
	}
}
@Configuration
@EnableWebFlux
class WebConfig : WebFluxConfigurer {

	override fun configureViewResolvers(registry: ViewResolverRegistry) {
		registry.freeMarker()
	}

	// Configure FreeMarker...

	@Bean
	fun freeMarkerConfigurer() = FreeMarkerConfigurer().apply {
		setTemplateLoaderPath("classpath:/templates/freemarker")
	}
}

你的模板需要存储在由FreeMarkerConfigurer, 如前例所示。给定上述配置,如果你的控制器返回视图名称,欢迎,解析器寻找classpath:/模板/freemarker/welcome.ftl模板。spring-doc.cadn.net.cn

FreeMarker 配置

你可以直接把FreeMarker的“设置”和“共享变量”传递给FreeMarker配置对象(由 Spring 管理)通过设置相应的 bean属性FreeMarkerConfigurer豆。 这freemarker设置性质要求 一个java.util.Properties对象,以及自由标记变量性质要求java.util.Map. 以下示例展示了如何使用FreeMarkerConfigurer:spring-doc.cadn.net.cn

@Configuration
@EnableWebFlux
public class WebConfig implements WebFluxConfigurer {

	// ...

	@Bean
	public FreeMarkerConfigurer freeMarkerConfigurer() {
		Map<String, Object> variables = new HashMap<>();
		variables.put("xml_escape", new XmlEscape());

		FreeMarkerConfigurer configurer = new FreeMarkerConfigurer();
		configurer.setTemplateLoaderPath("classpath:/templates");
		configurer.setFreemarkerVariables(variables);
		return configurer;
	}
}
@Configuration
@EnableWebFlux
class WebConfig : WebFluxConfigurer {

	// ...

	@Bean
	fun freeMarkerConfigurer() = FreeMarkerConfigurer().apply {
		setTemplateLoaderPath("classpath:/templates")
		setFreemarkerVariables(mapOf("xml_escape" to XmlEscape()))
	}
}

有关设置和变量的详细信息,请参阅FreeMarker文档 这配置对象。spring-doc.cadn.net.cn

表单处理

Spring 提供了一个用于 JSP 的标签库,其中包含包括<Spring:绑定/>元素。 该元素主要允许表单显示来自表单支持对象的值,并显示来自的失败验证结果验证器在 网页版或商务版。Spring 也支持 FreeMarker 中的相同功能,并附带额外的方便宏来生成表单输入元素。spring-doc.cadn.net.cn

绑定宏

spring-webflux.jar文件FreeMarker,这样它们始终可以被配置得合适的应用程序使用。spring-doc.cadn.net.cn

Spring模板库中定义的一些宏被视为内部的(私有的),但宏定义中没有这样的范围限制,使所有宏都能被调用代码和用户模板看到。接下来的章节仅聚焦宏你需要直接从模板内部调用。如果你想直接查看宏代码该文件被调用Spring.超光速并且属于org.springframework.web.reactive.result.view.freemarker包。spring-doc.cadn.net.cn

有关绑定支持的更多细节,请参见 Spring MVC 的简单绑定spring-doc.cadn.net.cn

表单宏

有关 Spring 对 FreeMarker 模板的表单宏支持详情,请参阅以下Spring MVC 文档中的部分。spring-doc.cadn.net.cn

脚本视图

Spring 框架内置集成,可将 Spring WebFlux 与任何能够运行在 JSR-223 Java 脚本引擎之上运行的模板库使用。下表展示了我们在不同脚本引擎上测试过的模板库:spring-doc.cadn.net.cn

脚本库 脚本引擎

车把spring-doc.cadn.net.cn

纳肖恩spring-doc.cadn.net.cn

胡子spring-doc.cadn.net.cn

纳肖恩spring-doc.cadn.net.cn

反应spring-doc.cadn.net.cn

纳肖恩spring-doc.cadn.net.cn

EJSspring-doc.cadn.net.cn

纳肖恩spring-doc.cadn.net.cn

欧洲广播委员会spring-doc.cadn.net.cn

JRubyspring-doc.cadn.net.cn

字符串模板spring-doc.cadn.net.cn

吉松spring-doc.cadn.net.cn

Kotlin 脚本模板spring-doc.cadn.net.cn

Kotlinspring-doc.cadn.net.cn

集成任何其他脚本引擎的基本规则是必须实现脚本引擎可调用接口。

要求

你需要在你的类路径上安装脚本引擎,具体细节因脚本引擎而异:spring-doc.cadn.net.cn

你需要脚本模板库。JavaScript 的一种方法是 通过WebJarsspring-doc.cadn.net.cn

脚本模板

你可以声明ScriptTemplateConfigurerBEAN 指定要使用的脚本引擎, 脚本文件要加载,调用哪个函数来渲染模板,等等。 以下示例使用了 Mustache 模板和 Nashorn JavaScript 引擎:spring-doc.cadn.net.cn

@Configuration
@EnableWebFlux
public class WebConfig implements WebFluxConfigurer {

	@Override
	public void configureViewResolvers(ViewResolverRegistry registry) {
		registry.scriptTemplate();
	}

	@Bean
	public ScriptTemplateConfigurer configurer() {
		ScriptTemplateConfigurer configurer = new ScriptTemplateConfigurer();
		configurer.setEngineName("nashorn");
		configurer.setScripts("mustache.js");
		configurer.setRenderObject("Mustache");
		configurer.setRenderFunction("render");
		return configurer;
	}
}
@Configuration
@EnableWebFlux
class WebConfig : WebFluxConfigurer {

	override fun configureViewResolvers(registry: ViewResolverRegistry) {
		registry.scriptTemplate()
	}

	@Bean
	fun configurer() = ScriptTemplateConfigurer().apply {
		engineName = "nashorn"
		setScripts("mustache.js")
		renderObject = "Mustache"
		renderFunction = "render"
	}
}

呈现函数调用时参数如下:spring-doc.cadn.net.cn

Mustache.render()与该签名原生兼容,因此你可以直接调用它。spring-doc.cadn.net.cn

如果你的模板技术需要一些定制,你可以提供一个脚本 实现了自定义渲染函数。例如,Handlerbars 需要在使用模板前编译模板,并且需要多边填充来模拟某些模板 服务器端脚本引擎不具备浏览器功能。 以下示例展示了如何设置自定义渲染函数:spring-doc.cadn.net.cn

@Configuration
@EnableWebFlux
public class WebConfig implements WebFluxConfigurer {

	@Override
	public void configureViewResolvers(ViewResolverRegistry registry) {
		registry.scriptTemplate();
	}

	@Bean
	public ScriptTemplateConfigurer configurer() {
		ScriptTemplateConfigurer configurer = new ScriptTemplateConfigurer();
		configurer.setEngineName("nashorn");
		configurer.setScripts("polyfill.js", "handlebars.js", "render.js");
		configurer.setRenderFunction("render");
		configurer.setSharedEngine(false);
		return configurer;
	}
}
@Configuration
@EnableWebFlux
class WebConfig : WebFluxConfigurer {

	override fun configureViewResolvers(registry: ViewResolverRegistry) {
		registry.scriptTemplate()
	}

	@Bean
	fun configurer() = ScriptTemplateConfigurer().apply {
		engineName = "nashorn"
		setScripts("polyfill.js", "handlebars.js", "render.js")
		renderFunction = "render"
		isSharedEngine = false
	}
}
设置共享引擎属性到false在使用非线程安全时是必须的 带有模板库但不设计用于并发的脚本引擎,如Handlebars或 反应在纳肖恩上奔跑。在这种情况下,由于这个bug,Java SE 8 更新60是必需的,但通常情况下 无论如何,建议使用最近的 Java SE 补丁版本。

polyfill.js仅定义Handlebars 需要对象才能正常运行, 如下片段所示:spring-doc.cadn.net.cn

var window = {};

这是基本的render.js实现在使用模板前会编译。一部制作 Ready 实现还应存储和重复使用缓存模板或预编译模板。 这可以在脚本端完成,也可以在你需要的自定义(管理)上完成 例如模板引擎配置)。 以下示例展示了如何编译模板:spring-doc.cadn.net.cn

function render(template, model) {
	var compiledTemplate = Handlebars.compile(template);
	return compiledTemplate(model);
}

可以看看Spring Framework单元测试、Java相关资源, 更多配置示例。spring-doc.cadn.net.cn

JSON 和 XML

对于内容协商来说,能够交替是很有用的 在用HTML模板渲染模型或以其他格式(如JSON或XML)渲染模型之间, 这取决于客户要求的内容类型。为了支持这一点,Spring WebFlux 提供HttpMessageWriterView你可以用它来插入任何可用的编解码器春网Jackson2JsonEncoder,Jackson2Smile编码器, 或Jaxb2XmlEncoder.spring-doc.cadn.net.cn

与其他视图技术不同,HttpMessageWriterView不需要ViewResolver而是被设置为默认视图。您可以 配置一个或多个这样的默认视图,并对不同的内容进行封装HttpMessageWriter实例 或编码器实例。运行时使用与请求内容类型匹配的版本。spring-doc.cadn.net.cn

在大多数情况下,模型包含多个属性。为了确定要序列化哪一个, 你可以配置HttpMessageWriterView并表示用于 的模型属性名称 渲染。如果模型只包含一个属性,则使用该属性。spring-doc.cadn.net.cn