1. Action其实是一个POJO,所以我们可以直接把Action生成一个XML.只要把Result Type设为xslt就可以了:
<action name="XmlModel" class="cpu.XmlModelAction">
<result name="success" type="xslt" />
</action>
里面包含了Action的所有属性,还有Locale等信息. 可以在浏览器里输入http://localhost:8080/XXX/XXX/XmlModel.action来看.
2. 为了定制自己想要的XML,我们可以输入参数来输出自己想要的Action的某个属性(属性可以是一个List).
<action name="XmlModel" class="cpu.XmlModelAction">
<result name="success" type="xslt">
<param name=”exposedValue”>modelList</param>
</result>
</action>
3. 如果有多于一个属性呢.加个大括号围起来
<action name="XmlModel" class="cpu.XmlModelAction">
<result name="success" type="xslt">
<param name=”exposedValue”>{modelList, type}</param>
</result>
</action>
4. 以上三个方法都是由Action 来控制生成格式.那么对于我们这种控制欲很强的人来说并不满足,最好是由我们自己来定义生成样子.那么这时我们就要做的就是在属性里生成一个 org.w3c.dom.Document属性.注意:不要用org.dom4j.Document类.要不会出现转化错误,死得很惨.
<action name="XmlModel" class="cpu.XmlModelAction">
<result name="success" type="xslt">
<param name="exposedValue">xmlDoc</param>
</result>
</action>
上面的xmlDoc就是 Action里的org.w3c.dom.Document类型.
5. 我只会用并且非要用org.dom4j.Document类怎么办? 那就在最后转化成 org.w3c.dom.Document:
- import java.io.ByteArrayInputStream;
- import java.io.IOException;
-
- import javax.xml.parsers.DocumentBuilderFactory;
- import javax.xml.parsers.ParserConfigurationException;
-
- import org.xml.sax.SAXException;
-
- public class XmlUtil {
-
- private XmlUtil() {
- }
-
- public static org.w3c.dom.Document XmlString2W3cDom(String xml)
- throws SAXException, IOException, ParserConfigurationException {
- byte[] byteArray = xml.getBytes("UTF-8");
- ByteArrayInputStream baos = new ByteArrayInputStream(byteArray);
- DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
- org.w3c.dom.Document doc = factory.newDocumentBuilder().parse(baos);
- return doc;
- }
-
- public static org.dom4j.Document W3c2Dom4j(org.w3c.dom.Document doc) {
- if (doc == null) {
- return (null);
- }
- org.dom4j.io.DOMReader xmlReader = new org.dom4j.io.DOMReader();
- return (xmlReader.read(doc));
- }
-
- }
import java.io.ByteArrayInputStream;
import java.io.IOException;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import org.xml.sax.SAXException;
public class XmlUtil {
private XmlUtil() {
}
public static org.w3c.dom.Document XmlString2W3cDom(String xml)
throws SAXException, IOException, ParserConfigurationException {
byte[] byteArray = xml.getBytes("UTF-8");
ByteArrayInputStream baos = new ByteArrayInputStream(byteArray);
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
org.w3c.dom.Document doc = factory.newDocumentBuilder().parse(baos);
return doc;
}
public static org.dom4j.Document W3c2Dom4j(org.w3c.dom.Document doc) {
if (doc == null) {
return (null);
}
org.dom4j.io.DOMReader xmlReader = new org.dom4j.io.DOMReader();
return (xmlReader.read(doc));
}
}
Action里加上一句: xmlDoc = Xmlutil.XmlString2W3cDom(dom4jDoc.asXML());
上面的xmlDoc就是 Action里的org.w3c.dom.Document类型.
6. 用org.dom4j.dom.DOMDocumentFactory()来建立 Document.而不要用DocumentHelper.createDocument()来产生.最后直接cast一下.
public String execute() throws UnsupportedEncodingException, ParserConfigurationException, SAXException, IOException {
- InfoCpuService cpuSvc = null;
- cpuSvc = (InfoCpuService) ServiceLocator.getInstance().getService(InfoCpuService.class);
-
- modelList = cpuSvc.getModels(prodBrand);
-
- log.info(ReflectionToStringBuilder.toString(this));
-
- org.dom4j.Document dom4jDoc = new org.dom4j.dom.DOMDocumentFactory().createDocument();
-
- Element root = dom4jDoc.addElement("root");
- root.addAttribute("prodType", "CPU");
-
- for (Object o : modelList) {
- root.addElement("model").addAttribute("name", (String) o);
- }
-
-
- xmlDoc = (Document) dom4jDoc;
-
- HttpServletResponse response = ServletActionContext.getResponse();
- response.setContentType("text/xml;charset=UTF-8");
- response.setHeader("Cache-Control", "no-cache");
-
- return SUCCESS;
-
public String execute() throws UnsupportedEncodingException, ParserConfigurationException, SAXException, IOException {
InfoCpuService cpuSvc = null;
cpuSvc = (InfoCpuService) ServiceLocator.getInstance().getService(InfoCpuService.class);
modelList = cpuSvc.getModels(prodBrand);
log.info(ReflectionToStringBuilder.toString(this));
org.dom4j.Document dom4jDoc = new org.dom4j.dom.DOMDocumentFactory().createDocument();
Element root = dom4jDoc.addElement("root");
root.addAttribute("prodType", "CPU");
for (Object o : modelList) {
root.addElement("model").addAttribute("name", (String) o);
}
//xmlDoc = XmlUtil.XmlString2W3cDom(doc.asXML());
xmlDoc = (Document) dom4jDoc;
HttpServletResponse response = ServletActionContext.getResponse();
response.setContentType("text/xml;charset=UTF-8");
response.setHeader("Cache-Control", "no-cache");
return SUCCESS;
}
其中三句代码是为了防止生成中文乱码
HttpServletResponse response = ServletActionContext.getResponse();
response.setContentType("text/xml;charset=UTF-8");
response.setHeader("Cache-Control", "no-cache");
7. 在action中返回类型最好设置为import org.w3c.dom.Document;
private Document retXML;
配置格式为:
- <action name="XmlModel" class="com.XmlModelAction">
- <result name="success" type="xslt">
- <param name=”exposedValue”>retXML</param>
- </result>
- </action>