指令碼檢視

Spring Framework 內建了與任何可運行於 JSR-223 Java 指令碼引擎之上的模板庫整合,以與 Spring MVC 一起使用。我們已在不同的指令碼引擎上測試了以下模板庫:

指令碼庫 指令碼引擎

Handlebars

Nashorn

Mustache

Nashorn

React

Nashorn

EJS

Nashorn

ERB

JRuby

字串模板

Jython

Kotlin 指令碼模板

Kotlin

整合任何其他指令碼引擎的基本規則是它必須實現 ScriptEngineInvocable 介面。

要求

你需要將指令碼引擎新增到你的類路徑中,具體細節因指令碼引擎而異。

  • Nashorn JavaScript 引擎隨 Java 8+ 提供。強烈建議使用最新可用的更新版本。

  • 對於 Ruby 支援,應新增 JRuby 作為依賴項。

  • 對於 Python 支援,應新增 Jython 作為依賴項。

  • 對於 Kotlin 指令碼支援,應新增 org.jetbrains.kotlin:kotlin-script-util 依賴項,以及一個包含 org.jetbrains.kotlin.script.jsr223.KotlinJsr223JvmLocalScriptEngineFactory 行的 META-INF/services/javax.script.ScriptEngineFactory 檔案。有關更多詳細資訊,請參見此示例

你需要擁有指令碼模板庫。對於 JavaScript,一種方法是透過 WebJars

指令碼模板

你可以宣告一個 ScriptTemplateConfigurer bean 來指定要使用的指令碼引擎、要載入的指令碼檔案、要呼叫哪個函式來渲染模板等等。以下示例使用 Mustache 模板和 Nashorn JavaScript 引擎。

  • Java

  • Kotlin

@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 形式展示了相同的配置。

<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 配置,控制器看起來並沒有什麼不同,如下例所示:

  • Java

  • Kotlin

@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 模板:

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

渲染函式被呼叫時帶有以下引數:

  • String template:模板內容

  • Map model:檢視模型

  • RenderingContext renderingContextRenderingContext,它提供對應用程式上下文、區域設定、模板載入器和 URL 的訪問(自 5.0 起)

Mustache.render() 原生相容此簽名,因此你可以直接呼叫它。

如果你的模板技術需要一些自定義,你可以提供一個實現自定義渲染函式的指令碼。例如,Handlerbars 需要在使用模板之前對其進行編譯,並需要一個 polyfill 來模擬伺服器端指令碼引擎中不可用的某些瀏覽器功能。

以下示例展示瞭如何實現:

  • Java

  • Kotlin

@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
	}
}
當使用非執行緒安全的指令碼引擎和未設計為併發的模板庫(例如在 Nashorn 上執行的 Handlebars 或 React)時,需要將 sharedEngine 屬性設定為 false。在這種情況下,由於存在 此錯誤,需要 Java SE 8 update 60,但通常建議在任何情況下都使用最新的 Java SE 補丁版本。

polyfill.js 只定義了 Handlebars 正常執行所需的 window 物件,如下所示:

var window = {};

這個基本的 render.js 實現在使用模板之前對其進行編譯。一個生產就緒的實現還應該儲存任何重用快取的模板或預編譯模板。你可以在指令碼端這樣做(並處理你需要的任何自定義 — 例如,管理模板引擎配置)。以下示例展示瞭如何做到這一點:

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

有關更多配置示例,請查閱 Spring Framework 單元測試,Java資源

© . This site is unofficial and not affiliated with VMware.