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

脚本视图

要求

你需要将脚本引擎添加到你的类路径中,具体细节因脚本引擎而异:spring-doc.cadn.net.cn

你需要拥有脚本模板库。对于Javascript,一种方法是通过WebJarsspring-doc.cadn.net.cn

脚本模板

您可以声明一个 ScriptTemplateConfigurer bean 来指定要使用的脚本引擎、要加载的脚本文件、调用什么函数来渲染模板等。 以下示例使用 Mustache 模板和 Nashorn JavaScript 引擎:spring-doc.cadn.net.cn

@Configuration
@EnableWebMvc
public class WebConfig implements WebMvcConfigurer {

	@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
@EnableWebMvc
class WebConfig : WebMvcConfigurer {

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

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

以下示例展示了相同的XML安排:spring-doc.cadn.net.cn

<mvc:annotation-driven/>

<mvc:view-resolvers>
	<mvc:script-template/>
</mvc:view-resolvers>

<mvc:script-template-configurer engine-name="nashorn" render-object="Mustache" render-function="render">
	<mvc:script location="mustache.js"/>
</mvc:script-template-configurer>

控制器对于Java和XML配置看起来没有区别,如下例所示:spring-doc.cadn.net.cn

@Controller
public class SampleController {

	@GetMapping("/sample")
	public String test(Model model) {
		model.addAttribute("title", "Sample title");
		model.addAttribute("body", "Sample body");
		return "template";
	}
}
@Controller
class SampleController {

	@GetMapping("/sample")
	fun test(model: Model): String {
		model["title"] = "Sample title"
		model["body"] = "Sample body"
		return "template"
	}
}

以下示例显示了Mustache模板:spring-doc.cadn.net.cn

<html>
	<head>
		<title>{{title}}</title>
	</head>
	<body>
		<p>{{body}}</p>
	</body>
</html>

渲染函数将被调用,并传入以下参数:spring-doc.cadn.net.cn

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

如果你的模板技术需要一些定制,你可以提供一个实现自定义渲染函数的脚本。例如,Handlebars在使用模板之前需要编译模板,并且需要一个polyfill来模拟一些在服务器端脚本引擎中不可用的浏览器功能。spring-doc.cadn.net.cn

以下示例展示了如何操作:spring-doc.cadn.net.cn

@Configuration
@EnableWebMvc
public class WebConfig implements WebMvcConfigurer {

	@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
@EnableWebMvc
class WebConfig : WebMvcConfigurer {

	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
	}
}
sharedEngine属性设置为false是在使用非线程安全的脚本引擎和未设计为支持并发的模板库(如Handlebars或在Nashorn上运行的React)时所必需的。在这种情况下,需要Java SE 8更新60,因为这个bug,但通常建议使用最近的Java SE补丁版本。

polyfill.js 定义了 Handlebars 正常运行所需的唯一 window 对象,如下所示:spring-doc.cadn.net.cn

var window = {};

这个基本的render.js实现会在使用模板之前对其进行编译。一个生产就绪的实现还应该存储任何重复使用的缓存模板或预编译模板。 你可以在脚本侧执行此操作(并处理你需要的任何自定义——例如管理模板引擎配置)。以下示例展示了如何操作: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