此版本仍在开发中,尚未被视为稳定版本。对于最新的稳定版本,请使用 Spring Framework 6.2.4spring-doc.cadn.net.cn

XSLT 视图

XSLT 是一种 XML 转换语言,在 Web 中作为一种流行的视图技术 应用。如果您的应用程序 自然会处理 XML,或者如果您的模型可以很容易地转换为 XML。以下内容 部分介绍如何将 XML 文档生成为模型数据,并使用 Spring Web MVC 应用程序中的 XSLT 进行验证。spring-doc.cadn.net.cn

此示例是一个普通的 Spring 应用程序,它在Controller并将其添加到模型映射中。将返回 map 以及视图 XSLT 视图的名称。有关 Spring Web MVC 的详细信息,请参见带注释的控制器Controller接口。XSLT 控制器将单词列表转换为简单的 XML 文档已准备好进行转换。spring-doc.cadn.net.cn

配置是简单 Spring Web 应用程序的标准配置:MVC 配置 必须定义一个XsltViewResolverbean 和常规 MVC 注释配置。 以下示例显示了如何执行此作:spring-doc.cadn.net.cn

@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。spring-doc.cadn.net.cn

控制器逻辑封装在@Controller类中,使用 handler 方法定义如下:spring-doc.cadn.net.cn

@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"
	}
}

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.spring-doc.cadn.net.cn

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.spring-doc.cadn.net.cn

Transformation

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.spring-doc.cadn.net.cn

The following example shows an XSLT transform:spring-doc.cadn.net.cn

<?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>

The preceding transform is rendered as the following HTML:spring-doc.cadn.net.cn

<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>