对于最新稳定版本,请使用Spring Framework 7.0.1spring-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
@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"
	}
}

以下示例展示了胡须模板: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

如果你的模板技术需要一些定制,你可以提供一个脚本 实现了自定义渲染函数。例如,Handlerbars需要编译模板后才能使用,并且需要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
	}
}
设置共享引擎属性到false在使用非线程安全时是必须的 带有模板库但不设计用于并发的脚本引擎,如Handlebars或 反应在纳肖恩上奔跑。在这种情况下,由于这个bug,Java SE 8 更新60是必需的,但通常情况下 无论如何,建议使用最近的 Java SE 补丁版本。

polyfill.js仅定义Handlebars 需要的对象才能正常运行,具体如下: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