Define a new return type in struts2-return XML string and generate HTML with XSLT

Source: Internet
Author: User
Tags i18n xsl xslt

1. define a new return type:

package com.yuxipacific.common.result;import java.io.StringReader;import javax.xml.parsers.DocumentBuilder;import javax.xml.parsers.DocumentBuilderFactory;import javax.xml.transform.Source;import org.apache.struts2.views.xslt.XSLTResult;import org.xml.sax.InputSource;public class StringXSLTResult extends XSLTResult {private static final long serialVersionUID = -5603306939538425896L;private boolean parseStringAsXML = true;public void setParseStringAsXML(boolean parseStringAsXML) {this.parseStringAsXML = parseStringAsXML;}@Overrideprotected Source getDOMSourceForStack(Object value) throws IllegalAccessException, InstantiationException {if(parseStringAsXML && value instanceof String) {try {DocumentBuilder builder = DocumentBuilderFactory.newInstance().newDocumentBuilder();value = builder.parse(new InputSource(new StringReader((String)value)));} catch (Exception e) {return super.getDOMSourceForStack(value);}}return super.getDOMSourceForStack(value);}}

2. Struts. xml declares the new return type and defines the action to be executed:

<!DOCTYPE struts PUBLIC"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN""http://struts.apache.org/dtds/struts-2.1.dtd"><struts><constant name="struts.devMode" value="true" />    <constant name="struts.enable.DynamicMethodInvocation" value="false" />    <constant name="struts.i18n.encoding" value="UTF-8" />    <constant name="struts.custom.i18n.resources" value="ApplicationResources" />        <package name="default" namespace="/" extends="struts-default">    <result-types>    <result-type name="sxslt" class="com.yuxipacific.common.result.StringXSLTResult"></result-type>    </result-types><interceptors><interceptor name="cmInterceptor" class="com.yuxipacific.common.aop.CmInterceptor" /><interceptor-stack name="cmStack"><interceptor-ref name="cmInterceptor" /><interceptor-ref name="defaultStack"/></interceptor-stack></interceptors><default-interceptor-ref name="cmStack" /><action name="query" class="com.yuxipacific.action.QueryAction" method="query"><result name="success" type="sxslt"><param name="exposedValue">xmlString</param>                <param name="stylesheetLocation ">/xslt/result.xslt</param>                <param name="noCache">true</param></result></action>    </package></struts>

3. queryaction. Java

package com.yuxipacific.action;import com.yuxipacific.common.action.CmAction;public class QueryAction extends CmAction {private static final long serialVersionUID = 174546491330585634L;@Overridepublic void doBeforeProcess() throws Exception {System.out.println("------------------------1");}@Overridepublic void doAfterProcess() throws Exception {System.out.println("------------------------2");}public String query() {//String xmlString = "<user><name>Admin</name></user>";//Map<String, Object> contextMap = ActionContext.getContext().getContextMap();//contextMap.put("xmlString", xmlString);xmlString = "<user><name>Admin</name></user>";return SUCCESS;}private String xmlString;public String getXmlString() {return xmlString;}public void setXmlString(String xmlString) {this.xmlString = xmlString;}}

XSLT/result. XSLT:

<?xml version="1.0" encoding="UTF-8"?><xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">    <xsl:output method="html" omit-xml-declaration="yes" />    <xsl:template match="/user">        <xsl:value-of select="name"/>    </xsl:template></xsl:stylesheet> 

For more information, see:

Http://unmi.cc/struts2-xslt-string-document-final

Http://unmi.cc/struts2-xsltresult-details

Http://eryk.iteye.com/blog/643946

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.