Struts2中定义新的返回类型---返回XML字符串并结合XSLT生成HTML

1. 定义新的返回类型:

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;
	}

	@Override
	protected 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中声明新的返回类型,并定义需要执行的action:

<!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;

	@Override
	public void doBeforeProcess() throws Exception {
		System.out.println("------------------------1");
	}

	@Override
	public 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> 

更多参考内容如下:

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

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

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


你可能感兴趣的:(exception,xml,struts,String,XSLT,stylesheet)