xslt 通过springmvc、js转换的方式

--》待翻译的xml文件内容
<?xml version='1.0' encoding='UTF-8' standalone='no'?>
<citizens>
<citizen>
<ssn>Z345T</ssn>
<firstname>Cheryl</firstname>
<lastname>Johnson</lastname>
<role>Manager</role>
<salary>12000</salary>
</citizen>
<citizen>
<ssn>Z446T</ssn>
<firstname>John</firstname>
<lastname>Smith</lastname>
<role>Employee</role>
<salary>1000</salary>
</citizen>
<citizen>
<ssn>Z335T</ssn>
<firstname>Justin</firstname>
<lastname>Claire</lastname>
<role>Senior Manager</role>
<salary>14000</salary>
</citizen>
<citizen>
<ssn>Z389T</ssn>
<firstname>Clark</firstname>
<lastname>Rick</lastname>
<role>Employee</role>
<salary>2000</salary>
</citizen>
</citizens>

--》xslt文件内容
<?xml version="1.0"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
version="1.0">
<xsl:output method="html" indent="yes" />
<xsl:template match="/">
<html>
<body>
<div align="center">
<xsl:apply-templates />
</div>
</body>
</html>
</xsl:template>
<xsl:template match="citizens">
<table border="1" width="100%">
<xsl:for-each select="citizen">
<tr>
<td>
<xsl:value-of select="ssn" />
</td>
<td>
<xsl:value-of select="firstname" />
</td>
<td>
<xsl:value-of select="lastname" />
</td>
<td>
<xsl:value-of select="role" />
</td>
<td>
<xsl:value-of select="salary" />
</td>
</tr>
</xsl:for-each>
</table>
</xsl:template>
</xsl:stylesheet>

--》springmvc controller代码
package net.codejava.spring.controller;

//参考http://www.codejava.net/frameworks/spring/spring-mvc-xstlview-and-xsltviewresolver-example
import java.io.File;
import java.io.IOException;
import java.io.StringReader;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.xml.transform.Source;
import javax.xml.transform.stream.StreamSource;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

@Controller
public class HomeController {
@RequestMapping(value = "/")
public ModelAndView goHome(HttpServletResponse response) throws IOException {
return new ModelAndView("home");
}

@RequestMapping(value = "/viewXSLT")
public ModelAndView viewXSLT(HttpServletRequest request,
HttpServletResponse response) throws IOException {
/* //从xml文件翻译
String xmlFile = "resources/citizens.xml";
String contextPath = request.getSession().getServletContext().getRealPath("");
String xmlFilePath = contextPath + File.separator + "WEB-INF" + File.separator + xmlFile;
Source source = new StreamSource(new File(xmlFilePath));*/
//从xml字符串翻译
String xmlStr = "<?xml version='1.0' encoding='UTF-8' standalone='no'?><citizens> <citizen> <ssn>Z345T</ssn> <firstname>Cheryl</firstname> <lastname>Johnson</lastname> <role>Manager</role> <salary>12000</salary> </citizen> <citizen> <ssn>Z446T</ssn> <firstname>John</firstname> <lastname>Smith</lastname> <role>Employee</role> <salary>1000</salary> </citizen> <citizen> <ssn>Z335T</ssn> <firstname>Justin</firstname> <lastname>Claire</lastname> <role>Senior Manager</role> <salary>14000</salary> </citizen> <citizen> <ssn>Z389T</ssn> <firstname>Clark</firstname> <lastname>Rick</lastname> <role>Employee</role> <salary>2000</salary> </citizen></citizens>";
Source source = new StreamSource(new StringReader(xmlStr));
// adds the XML source file to the model so the XsltView can detect
ModelAndView model = new ModelAndView("XSLTView");
model.addObject("xmlSource", source);
return model;
}
}

--》spring mvc配置文件
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd
">
<!-- 默认扫描的包路径 -->
<context:component-scan base-package="net.codejava.spring" />
<!-- 添加注解驱动 -->
<mvc:annotation-driven />
<!-- 定义跳转的文件的前后缀 -->
<bean id="XSLTViewResolver"
class="org.springframework.web.servlet.view.xslt.XsltViewResolver">
<property name="order" value="1" />
<property name="sourceKey" value="xmlSource" />
<property name="viewClass"
value="org.springframework.web.servlet.view.xslt.XsltView" />
<property name="viewNames">
<array>
<value>XSLTView</value>
</array>
</property>
<property name="prefix" value="/WEB-INF/xsl/" />
<property name="suffix" value=".xsl" />
</bean>
<bean id="JSPViewResolver"
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="order" value="2" />
<property name="prefix" value="/WEB-INF/jsp/" />
<property name="suffix" value=".jsp" />
</bean>
</beans>

--》xslt配置文件
XSLTView.(class)=org.springframework.web.servlet.view.xslt.XsltView
XSLTView.sourceKey=xmlSource
XSLTView.url=/WEB-INF/xsl/XSLTView.xsl


--->xslt_client2.jsp xslt_client.jsp 为客户端转换处理方法

---》参考
http://www.codejava.net/frameworks/spring/spring-mvc-xstlview-and-xsltviewresolver-example
http://tech.it168.com/KnowledgeBase/Articles/8/6/a/86a8266e3eeb9654020fbd3d03545c78.htm
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值