jsp调用xml和xslt动态生成html页面的两种实现方法

工作的需要,用到了在jsp中调用xml、xslt生成静态页面,下面把自己写过的放到这,以后备用和学习。。。写的不好,尽管扔砖头。。。

第一种实现方式::
xml从调用服务器端的方法返回字符串类型
<%@page language="java" import="java.util.*" pageEncoding="GB18030"%>
<%@page import="java.io.FileInputStream"%>
<%@page import="java.io.File"%>
<%@page import="org.springframework.web.context.support.WebApplicationContextUtils"%>
<%@page import="org.springframework.context.ApplicationContext"%>
<%@page import="java.io.ByteArrayOutputStream"%>
<%@page import="javax.xml.transform.TransformerFactory"%>
<%@page import="javax.xml.transform.stream.StreamResult"%>
<%@page import="javax.xml.transform.stream.StreamSource"%>
<%@page import="javax.xml.transform.Transformer"%>
<%@page import="服务器端bean"%>
<%@page import="java.io.StringReader"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme() + "://"
+ request.getServerName() + ":" + request.getServerPort()
+ path + "/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<title>XXX界面</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
</head>
<body>

<%
String topicName="";
if(request.getParameter("topic")!=null){topicName = request.getParameter("topic");}
ApplicationContext applicationContext = WebApplicationContextUtils.getWebApplicationContext(this.getServletContext());
//下面这句是加载服务器端bean
XXXService xxxService = (XXXService) applicationContext.getBean("xxxService");
//以下是调用服务器端方法发挥xml格式字符串
String xmlSource = xxxService.loadTopicDefineXmlByCn(topicName);
StreamSource xml = new StreamSource(new StringReader(xmlSource));
StreamSource xsl = new StreamSource(new File("d:\\Tomcat-6\\webapps\\项目\\info\\model.xslt"));
ByteArrayOutputStream resultByte = new ByteArrayOutputStream();
//response.setContentType("text/html; charset=UTF-8");
StreamResult result = new StreamResult(resultByte);

Transformer trans = TransformerFactory.newInstance().newTransformer(xsl);
trans.setParameter("entity_name",topicName);
//trans.setOutputProperty("encoding","UTF-8");
trans.setOutputProperty(javax.xml.transform.OutputKeys.ENCODING,  
         "GB2312");
trans.transform(xml, result);
out.print(resultByte.toString()); 

%>
</body>
</html>


第二种实现方法::

--------xml文件以路径的方式获得

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<title>界面</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
</head>
<body> 
<%
String topicName="";
if(request.getParameter("topic")!=null){topicName = request.getParameter("topic");}
ApplicationContext applicationContext = WebApplicationContextUtils.getWebApplicationContext(this.getServletContext());
//下面这句是加载服务器端bean
XXXService xxxService = (XXXService) applicationContext.getBean("xxxService");
FileInputStream sourceXml = new FileInputStream(resourceService.loadTopicDefineXmlByCn(topicName));
//FileInputStream sourceXml = new FileInputStream(new File("D:\\Tomcat-6\\webapps\\项目\\info\\model.xml"));
ByteArrayOutputStream resultByte = new ByteArrayOutputStream();
TransformerFactory factory = TransformerFactory.newInstance();
Transformer transformer = factory.newTransformer(new StreamSource(new File("d:\\Tomcat-6\\webapps\\项目\\info\\model.xslt")));
transformer.setParameter("entity_name",topicName);
StreamResult streamResult = new StreamResult(resultByte);
transformer.setOutputProperty(javax.xml.transform.OutputKeys.ENCODING,  
         "GB2312");
transformer.transform(new StreamSource(sourceXml), streamResult);
out.print(resultByte.toString());
%>
</body>
</html>

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值