运行结果:
MyJsp.jsp
<%@ page language="java" import="java.util.*" pageEncoding="ISO-8859-1"%>
<%@ taglib uri="http://struts.apache.org/tags-html" prefix="html" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title></title>
</head>
<body>
<html:link action="/outputXML">outputxml</html:link>
</body>
</html>
OutputXMLAction .java
package cn.hxex.action.struts.action;
import java.io.IOException;
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.jdom.Document;
import org.jdom.Element;
import org.jdom.output.XMLOutputter;
/**
* MyEclipse Struts
* Creation date: 06-20-2006
*
* XDoclet definition:
* @struts.action validate="true"
*/
public class OutputXMLAction extends Action {
// --------------------------------------------------------- Instance Variables
// --------------------------------------------------------- Methods
/**
* Method execute
* @param mapping
* @param form
* @param request
* @param response
* @return ActionForward
*/
public ActionForward execute(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response)
throws IOException {
response.setContentType("text/xml");
Document doc = new Document();
Element root = new Element("users");
doc.setRootElement(root);
Element e = new Element("user");
e.setAttribute("name", "Mike");
e.setAttribute("age", "30");
root.addContent(e);
e = new Element("user");
e.setAttribute("name", "David");
e.setAttribute("age", "45");
root.addContent(e);
XMLOutputter xout = new XMLOutputter();
xout.output(doc, response.getWriter());
response.getWriter().close();
return null;
}
}
struts-config.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts-config PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 1.2//EN" "http://struts.apache.org/dtds/struts-config_1_2.dtd">
<struts-config>
<data-sources />
<form-beans >
<form-bean name="tokenForm" type="cn.hxex.action.struts.form.TokenForm" />
<form-bean name="uploadForm" type="cn.hxex.action.struts.form.UploadForm" />
</form-beans>
<global-exceptions />
<global-forwards />
<action-mappings >
<action path="/outputXML" type="cn.hxex.action.struts.action.OutputXMLAction" />
</action-mappings>
<message-resources parameter="cn.hxex.action.struts.ApplicationResources" />
</struts-config>