开发者

Formatted HTML as output from method invocation from MX4J HTTP page

I have a huge set of data and want to display the data with some formatting.

This is what the method basically looks like:


    @ManagedOperation(description = "return html")

@ManagedOperationParameters({@ManagedOperationParameter(name = "someVal", description = "text")})

public String returnAsHtml(String someVal)

{

return "some formatted xml";

}

Looks like XSLTProcesso开发者_如何学JAVAr can be configured to use a XSLT template. However I could not find any examples on the internet using XSLT for html transformation in the context of MX4J. Could any one provide a sample XSLT template?


In case anyone comes back to this question, two things come to mind:

1) MX4J has several default implementations of HttpCommandProcessorAdaptor. These operations are mapped from the path. For JMX operations (aka ManagedOperation in Spring parlance), MX4J uses URLs like /invoke?operation=returnAsHtml

This will be passed to the InvokeOperationCommandProcessor to create an XML document with the result being just the toString() of whatever you returned, in an attribute called 'return'. It also passes back the return type in an attribute called 'returnclass'. You can see all this if you just add &template=identity to the invoke URL.

I mention all this because one option is to implement your own 'invoke.xsl'. The one in MX4J just calls the renderobject template:

Lo and behold, you find this in mbean_attributes.xsl, with a comment showing you exactly what you need to do:

   <xsl:template name="renderobject">
  <xsl:param name="objectclass"/>
  <xsl:param name="objectvalue"/>
  <xsl:choose>
     <xsl:when test="$objectclass='javax.management.ObjectName'">
        <xsl:variable name="name_encoded">
           <xsl:call-template name="uri-encode">
              <xsl:with-param name="uri">
                 <xsl:value-of select="$objectvalue"/>
              </xsl:with-param>
           </xsl:call-template>
        </xsl:variable>
        <a href="/mbean?objectname={$name_encoded}">
           <xsl:value-of select="$objectvalue"/>
        </a>
     </xsl:when>
     <xsl:otherwise>
        <!-- Use the following line when the result of an invocation
        returns e.g. HTML or XML data
        <xsl:value-of select="$objectvalue" disable-output-escaping="true" />
        -->
        <xsl:value-of select="$objectvalue"/>
     </xsl:otherwise>
  </xsl:choose>

Setting 'disable-output-escaping' to true will do the trick

2) Another option is to write your own HttpCommandProcessorAdaptor, and set it on the HttpAdapter. This could either replace the 'invoke' processor, or you could have an entirely new one.

Hope that helps


One way I figured out is to use java script in the XSL template to extract and parse the string. Make sure you test for the browser (IE vs Non IE) and use proper parser.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜