选择显示字体大小

struts框架下,在jsp页面显示xsl格式化的xml。

struts框架下,在jsp页面显示xsl格式化的xml。需要以下步骤:
1 页面显示前xml的dom对象和xsl文件地址存入request

package com.nova.colimas.web.action;

import javax.servlet.http.httpservletrequest;
import javax.servlet.http.httpservletresponse;

import org.apache.struts.action.action;
import org.apache.struts.action.actionform;
import org.apache.struts.action.actionforward;
import org.apache.struts.action.actionmapping;
import org.w3c.dom.element;
import org.w3c.dom.document ;
import com.nova.colimas.common.xml.xmlobject;
import com.nova.colimas.web.constants.xslpathconstants;
import com.nova.colimas.web.constants.dtdpathconstants;
import com.nova.colimas.common.resources.dtd.ubidtd;
import javax.servlet.jsp.pagecontext;

public class loaddataaction extends action {
        private document mydata;
        private string xslpath;

        public actionforward execute(actionmapping mapping,
                         actionform form,
                         httpservletrequest request,
                         httpservletresponse response)
        throws exception{
                mydata=loaddata();
                xslpath=”resources/example.xsl”;
                //xml的dom对象
                request.setattribute("userxml",mydata);
                        //xsl文件地址
                request.setattribute("bodyxsl",xslpath);
                return mapping.findforward("success");
        }
        private document loaddata(){
                document result=null;
                try{
                        //创建xml的dom对象,xml的dtd下面给出
                }catch(exception e){
                        e.printstacktrace();
                }
                return result;
        }
}

dtd文件内容
<?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot;?>
<!--dtd generated by xmlspy v2005 rel. 3 u (http://www.altova.com)-->
<!--the entity declarations may be overridden in the internal subset-->
<!--the declarations below should not be modified-->
<!--element name mappings-->
<!entity % usersbasicinfo &quot;usersbasicinfo&quot;>
<!entity % userbasicinfo &quot;userbasicinfo&quot;>
<!entity % userid &quot;userid&quot;>
<!entity % password &quot;password&quot;>
<!entity % username &quot;username&quot;>
<!entity % email &quot;email&quot;>
<!--element and attribute declarations-->
<!--title: ubi.xsd

subject: the user basic information.
                        publisher: nova corporation, colimas.
format: text/xml

creator: zhao lei
                        
date.created: 2005-04-26
                        
language: en-us
                        
description: user basic information data definition
                        
change log:
version        date                     modifier        description

01.00        2005/04/26       zhao lei          initial release.                                                                                        
                        -->
<!element %usersbasicinfo; ((%userbasicinfo;)*)>
<!--one user basic information-->
<!element %userbasicinfo; (%userid;, (%password;)?, %username;, %email;)>
<!--the max length of characters of it is 20-->
<!element %userid; (#pcdata)>
<!--show it only when user is administor. 12 fixed characters-->
<!element %password; (#pcdata)>
<!element %username; (#pcdata)>
<!element %email; (#pcdata)>


xsl文件内容
<?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot;?>
<xsl:stylesheet version=&quot;1.0&quot; xmlns:xsl=&quot;http://www.w3.org/1999/xsl/transform&quot; xmlns:xsi=&quot;http://www.w3.org/2001/xmlschema-instance&quot;>
<xsl:variable name=&quot;userinfo.comp&quot; select=&quot;'component name'&quot;/>
<xsl:variable name=&quot;userinfo.id&quot; select=&quot;'serial no'&quot;/>
<xsl:variable name=&quot;userinfo.role&quot; select=&quot;'role name'&quot;/>
<xsl:variable name=&quot;userinfo.user&quot; select=&quot;'user id*'&quot;/>
<xsl:variable name=&quot;userinfo.name&quot; select=&quot;'user name*'&quot;/>
<xsl:variable name=&quot;userinfo.passord&quot; select=&quot;'password*'&quot;/>
<xsl:variable name=&quot;userinfo.mail&quot; select=&quot;'email address*'&quot;/>
<xsl:variable name=&quot;userinfo.userp&quot; select=&quot;'user profile'&quot;/>
<xsl:variable name=&quot;userinfo.required&quot; select=&quot;'all items are required'&quot;/>
<xsl:variable name=&quot;template.edit&quot; select=&quot;'edit!'&quot;/>
<xsl:variable name=&quot;userinfo.access&quot; select=&quot;'access role list'&quot;/>

<xsl:template match=&quot;userbasicinfo&quot;>
        <h1><xsl:value-of  select=&quot;$userinfo.userp&quot;/></h1>
        <xsl:variable name=&quot;userid&quot; select=&quot;userid&quot;/>
        <input type=&quot;button&quot; name=&quot;edit&quot; value=&quot;{$template.edit}&quot;/>
        <p><font color=&quot;#003399&quot; size=&quot;4&quot;><xsl:value-of  select=&quot;$userinfo.required&quot;/></font></p>        
        <table border=&quot;0&quot; width=&quot;826&quot; height=&quot;103&quot; cellpadding=&quot;2&quot; cellspacing=&quot;1&quot;>
        
                        <tr>
                                        <td bgcolor=&quot;#e0f1ff&quot;><xsl:value-of  select=&quot;$userinfo.user&quot;/></td>
                                        <td ><xsl:value-of select=&quot;$userid&quot;/></td>
                        </tr>
                        <tr>
                                        <td bgcolor=&quot;#e0f1ff&quot;><xsl:value-of  select=&quot;$userinfo.passord&quot;/></td>
                                        <td><xsl:value-of select=&quot;password&quot;/></td>
                        </tr>
                        <tr>                
                                        <td bgcolor=&quot;#e0f1ff&quot;><xsl:value-of  select=&quot;$userinfo.name&quot;/></td>
                                        <td><xsl:value-of select=&quot;username&quot;/></td>
                        </tr>
                        <tr>                
                                        <td bgcolor=&quot;#e0f1ff&quot;><xsl:value-of  select=&quot;$userinfo.mail&quot;/></td>
                                        <td><xsl:value-of select=&quot;email&quot;/></td>                                                                
                        </tr>
        </table>
</xsl:template>

</xsl:stylesheet>

2 创建xsl的tag
tld文件内容如下:
<?xml version=&quot;1.0&quot; encoding=&quot;iso-8859-1&quot;?>
<!doctype taglib public &quot;-//sun microsystems, inc.//dtd jsp tag library 1.1//en&quot; &quot;http://java.sun.com/j2ee/dtds/web-jsptaglibrary_1_1.dtd&quot;>
<taglib>
        <tlibversion>1.0</tlibversion>
        <jspversion>1.1</jspversion>
        <shortname>colimas</shortname>
        <info>contains xsl tag definitions used in colimas</info>
        <tag>
                <name>xsl</name>
                <tagclass>com.nova.colimas.common.tags.tagxsl</tagclass>
                <bodycontent>empty</bodycontent>
                <info>gets xml dom and xsl resource path from the http request and uses these datas to generate and display html. the xsl resource path is localized according to the java.util.locale that is found in the context. the different context are indicated by their scope : &quot;application&quot;, &quot;session&quot;, &quot;request&quot; or &quot;page&quot;. the locale is optional ; if it is not supplied, the default locale of the server is used.</info>
                <attribute>
                        <name>xslkey</name>
                        <required>true</required>
                        <rtexprvalue>false</rtexprvalue>
                </attribute>
                <attribute>
                        <name>xslscope</name>
                        <required>true</required>
                        <rtexprvalue>false</rtexprvalue>
                </attribute>
                <attribute>
                        <name>xmlkey</name>
                        <required>true</required>
                        <rtexprvalue>false</rtexprvalue>
                </attribute>
                <attribute>
                        <name>xmlscope</name>
                        <required>true</required>
                        <rtexprvalue>false</rtexprvalue>
                </attribute>
        </tag>
</taglib>



tagclass内容如下:
* gets xml dom and xsl resource path from the http request and uses these
* datas to generate and display html. the xsl resource path is localized
* according to the java.util.locale that is found in the context. the different
* context are indicated by their scope : &quot;application&quot;, &quot;session&quot;, &quot;request&quot; or &quot;page&quot;.
* the locale is optional ; if it is not supplied, the default locale of the server is used.
* @author tyrone
*/
public class tagxsl extends tagsupport
{
        /**
                * key for the xmlobject in its context, xmlkey
                */
        protected string xmlkey;
        /**
                * scope for the xmlobject, xmlscope
                */
        protected int xmlscope;
        /**
                * key for the xsl resource path, xslkey
                */
        protected string xslkey;
        /**
                * scope for the xsl resource path, xslscope
                */
        protected int xslscope;
        /**
         * executes the tag. it gets xml and xsl information, then generates the html before write it.
         */
        public int dostarttag() throws jspexception
        {
                string xslpath = null;
                long start = system.currenttimemillis();
                try
                {
                        // gets the xml object from the context
                        document xmlobject = (document) pagecontext.getrequest().getattribute(xmlkey);
                        if (xmlobject == null)
                                throw new jsptagexception(&quot;cannot retrieve the xmlobject in scope &quot; + xmlscope + &quot; under the key &quot; + xmlkey);
                        // gets the xsl resource path from the context
                        xslpath = (string) pagecontext.getrequest().getattribute(xslkey);
                        if (xslpath == null)
                                throw new jsptagexception(&quot;cannot retrieve the xsl path in scope &quot; + xslscope + &quot; under the key &quot; + xslkey);
                        // try to get the locale from the context
                        locale locale = (locale) pagecontext.getattribute(globals.locale_key, pagecontext.session_scope);

                        // right. we have all needed elements.
                        try
                        {
                                // do the transformation
                                xsltransform.transform(xmlobject, xslpath, locale, pagecontext.getout());
                                // all is done, continue to execute the jsp
                                return eval_page;
                        }
                        catch (exception ex)
                        {
                                //console.error (this, &quot;cannot transform xml&quot;, ex);
                                throw new jsptagexception(ex.getmessage());
                        }
                }
                finally
                {
                        long end = system.currenttimemillis();
                        system.out.println(&quot;time for tagxsl (&quot; + xslpath + &quot;) : &quot; + (end - start) + &quot; ms&quot;);
                        //console.verbose(this, &quot;time for tagxsl (&quot; + xslpath + &quot;) : &quot; + (end - start) + &quot; ms&quot;);
                }
        }
}


3 xsltransform的实现
/**
* transform xml with xsl file to show in web page
* @author tyrone
*
*/
public class xsltransform {
        /**
         * internal factory
         */
        private static transformerfactory transformerfactory;
        static
        {
                transformerfactory = transformerfactory.newinstance();
        }
        /**
         * method to transform an xmlobject into an xhtml string
         * by using an xsl stylesheet.
         *
         * the result of the transformation is given to the writer given as a parameter.
         * @param xo the xmlobject to transform
         * @param xslresourcepath the absolute resource path to the xsl file, without any
         * country information.
         * @param locale the information about localization of the xsl file
         * @param output output for the transformation.
         * @see xslserver
         */
        public static void transform(document xo, string xslresourcepath, locale locale, writer output) throws ioexception, parserconfigurationexception, saxexception, transformerexception, transformerconfigurationexception, clmsexception
        {
                // get the xmlobject dom
                document xmldom = xo
                // get the xsl dom,本文忽略如何得到xsl的document。
                document xsldom = xslserver.getxsl(xslresourcepath, locale);
                transformasstring(xmldom, xsldom, output);
        }

        /**
         * transforms the <code>xml</code> in a xml string
         * to the <code>xsl</code> xslt stylesheet.
         *
         * the output of the transformation is put into the given writer.
         * @param xml the xml dom
         * @param xsl the xsl dom
         * @param output output for the transformation.
         */
        public static void transformasstring(document xml, document xsl, writer output) throws ioexception, saxexception, transformerexception, transformerconfigurationexception
        {
                // xsl source
                domsource xslsource = new domsource(xsl);
                // transformer
                transformer transformer = transformerfactory.newtransformer(xslsource);
                // prepare the result
                streamresult result = new streamresult(output);
                // process
                transformer.transform(new domsource(xml), result);
                
        }        
}



 


关键字 本文所属关键字

相关 与本文相关文章

分类 所有文章关键字导航

源码编程相关

Java   Asp   PHP   .Net   XML   C/C++   CGI   VB   Jsp   J2ee   J2se   J2me   EJB   Servlet   Tomcat   Resin   Struts   Weblogic   Eclipse   ANT   GUI   JMS   Web servise   IDEA   Webphere   Hibernate   Spring   Jboss   Applet   Swing   Socket   Javamail   Perl   Ajax   P2P   安全   模式   框架   测试   开源   游戏

SQL数据库相关

My-SQL   Ms-SQL   Access   DB2   Oracle   Sybase   SQLserver   索引   存储过程   加密   数据库   分页   视图  

手机无线相关

3G   Wap   CDMA   GRPS   GSM   IVR   彩信   短信   无线   增值业务

网页设计制作相关

HTML   CSS   网页配色   网页特效   Javascript   VBscript   Dreamweaver   Frontpage   JS   Web   网站设计

网站建设推广相关

建站经验   网站优化   网站排名   推广   Alexa

操作系统/服务器相关

Windows XP   Windows 2000   Windows 2003   Windows Me   Windows 9.x   Linux   UNIX   注册表   操作系统   服务器   应用服务器

图形图像多媒体相关

Photoshop   Fireworks   Flash   Coreldraw   Illustrator   Freehand   Photoimpact   多媒体   图形图像

标准 网站致力的规范

Valid CSS!

无不良内容,无不良广告,无恶意代码

Valid XHTML 1.0 Transitional

creativecommons