用XSLT和XML改进Struts
http://tech.ddvip.com 2006年11月25日 社区交流
本文详细介绍用XSLT和XML改进Struts
图3显示的是XSLT的处理流程。下面一节提供了一个将内容和版面设计分离的一个例子。
转化例子
TestForm是一个简单的窗体bean,它只有两个属性:
public class TestForm extends ActionForm {
private String testString;
private List testList;
}假设testString的值为My Test String,testList的值为One、two、Three,XML串行化代码会生成一下XML片断。在XML文档中的元素名是可以预见的,这样编写样式表的时候会简单一些。
<page name="TestForm">
<request>
<TestForm>
<testString>My Test String</testString>
<testList>
<item>One</item>
<item>Two</item>
<item>Three</item>
</testList>
</TestForm>
</request>
</page>简单的XSLT模板把已经串行化的XML流转换到XHTML片断。
<xsl:template match="page">
<h2>Please enter some text and submit</h2>
<br/>
<form name="testForm" method="get" action="result">
<input type="text" name="testString"
value="{request/TestForm/testString}"/>
<br/>
<select name="outSelect">
<xsl:for-each select="request/TestForm/testList/item">
<option><xsl:value-of select="."/></option>
</xsl:for-each>
</select>
<br/>
<input type="submit" value="Submit"/>
</form>
<hr/>
</xsl:template>经过转化和HTML串行化,结果应该是如下
责编:豆豆技术应用
正在加载评论...