XSLT Views

XSLT 是 XML 的转换语言,并且作为 Web 应用程序中的视图技术很流行。如果您的应用程序自然处理 XML 或您的模型可以轻松转换为 XML,则 XSLT 可能是一种作为视图技术的理想选择。以下部分展示了如何在 Spring Web MVC 应用程序中生成 XML 文档作为模型数据并使用 XSLT 转换它。

XSLT is a transformation language for XML and is popular as a view technology within web applications. XSLT can be a good choice as a view technology if your application naturally deals with XML or if your model can easily be converted to XML. The following section shows how to produce an XML document as model data and have it transformed with XSLT in a Spring Web MVC application.

此示例是一个简单的 Spring 应用程序,它在 `Controller`中创建一个单词列表,并将它们添加到模型映射中。映射与 XSLT 视图的视图名称一起返回。有关 Spring Web MVC 的 `Controller`接口的详细信息,请参阅 Annotated Controllers。XSLT 控制器将单词列表转换为一个为转换准备好的简单 XML 文档。

This example is a trivial Spring application that creates a list of words in the Controller and adds them to the model map. The map is returned, along with the view name of our XSLT view. See Annotated Controllers for details of Spring Web MVC’s Controller interface. The XSLT controller turns the list of words into a simple XML document ready for transformation.

Beans

对于一个简单的 Spring Web 应用程序,配置是标准的:MVC 配置必须定义一个 XsltViewResolver bean 和常规 MVC 注释配置。以下示例展示了如何执行此操作:

Configuration is standard for a simple Spring web application: The MVC configuration has to define an XsltViewResolver bean and regular MVC annotation configuration. The following example shows how to do so:

  • Java

  • Kotlin

@EnableWebMvc
@ComponentScan
@Configuration
public class WebConfig implements WebMvcConfigurer {

	@Bean
	public XsltViewResolver xsltViewResolver() {
		XsltViewResolver viewResolver = new XsltViewResolver();
		viewResolver.setPrefix("/WEB-INF/xsl/");
		viewResolver.setSuffix(".xslt");
		return viewResolver;
	}
}
@EnableWebMvc
@ComponentScan
@Configuration
class WebConfig : WebMvcConfigurer {

	@Bean
	fun xsltViewResolver() = XsltViewResolver().apply {
		setPrefix("/WEB-INF/xsl/")
		setSuffix(".xslt")
	}
}

Controller

我们还需要一个封装我们的单词生成逻辑的控制器。

We also need a Controller that encapsulates our word-generation logic.

控制器逻辑封装在 @Controller 类中,其中的处理程序方法定义如下:

The controller logic is encapsulated in a @Controller class, with the handler method being defined as follows:

  • Java

  • Kotlin

@Controller
public class XsltController {

	@RequestMapping("/")
	public String home(Model model) throws Exception {
		Document document = DocumentBuilderFactory.newInstance().newDocumentBuilder().newDocument();
		Element root = document.createElement("wordList");

		List<String> words = Arrays.asList("Hello", "Spring", "Framework");
		for (String word : words) {
			Element wordNode = document.createElement("word");
			Text textNode = document.createTextNode(word);
			wordNode.appendChild(textNode);
			root.appendChild(wordNode);
		}

		model.addAttribute("wordList", root);
		return "home";
	}
}
import org.springframework.ui.set

@Controller
class XsltController {

	@RequestMapping("/")
	fun home(model: Model): String {
		val document = DocumentBuilderFactory.newInstance().newDocumentBuilder().newDocument()
		val root = document.createElement("wordList")

		val words = listOf("Hello", "Spring", "Framework")
		for (word in words) {
			val wordNode = document.createElement("word")
			val textNode = document.createTextNode(word)
			wordNode.appendChild(textNode)
			root.appendChild(wordNode)
		}

		model["wordList"] = root
		return "home"
	}
}

到目前为止,我们只创建了一个 DOM 文档并将其添加到 Model 映射。请注意,您还可以将 XML 文件作为一个 Resource 加载,并使用它来代替自定义 DOM 文档。

So far, we have only created a DOM document and added it to the Model map. Note that you can also load an XML file as a Resource and use it instead of a custom DOM document.

有一些可自动“domify”一个对象图的软件包,但在 Spring 中,您有完全的灵活性,可以使用任何选择的方式从您的模型中创建 DOM。这防止了 XML 的转换在您的模型数据的结构中发挥了过大的作用,这是在使用该工具管理 DOM 化过程时的危险。

There are software packages available that automatically 'domify' an object graph, but, within Spring, you have complete flexibility to create the DOM from your model in any way you choose. This prevents the transformation of XML playing too great a part in the structure of your model data, which is a danger when using tools to manage the DOMification process.

Transformation

最后,XsltViewResolver 解析了 “home” XSLT 模板文件并将其合并到 DOM 文档中,以生成我们的视图。如 XsltViewResolver 配置中所示,XSLT 模板位于 WAR 文件中的 WEB-INF/xsl 目录中,并以 xslt 文件扩展名结尾。

Finally, the XsltViewResolver resolves the “home” XSLT template file and merges the DOM document into it to generate our view. As shown in the XsltViewResolver configuration, XSLT templates live in the war file in the WEB-INF/xsl directory and end with an xslt file extension.

以下示例展示了一个 XSLT 转换:

The following example shows an XSLT transform:

<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

	<xsl:output method="html" omit-xml-declaration="yes"/>

	<xsl:template match="/">
		<html>
			<head><title>Hello!</title></head>
			<body>
				<h1>My First Words</h1>
				<ul>
					<xsl:apply-templates/>
				</ul>
			</body>
		</html>
	</xsl:template>

	<xsl:template match="word">
		<li><xsl:value-of select="."/></li>
	</xsl:template>

</xsl:stylesheet>

前述的转换呈现为以下 HTML:

The preceding transform is rendered as the following HTML:

<html>
	<head>
		<META http-equiv="Content-Type" content="text/html; charset=UTF-8">
		<title>Hello!</title>
	</head>
	<body>
		<h1>My First Words</h1>
		<ul>
			<li>Hello</li>
			<li>Spring</li>
			<li>Framework</li>
		</ul>
	</body>
</html>