1.struts2集成时需要对应的jar包
2.struts.xml:
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.4"
xmlns="http://java.sun.com/xml/ns/j2ee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee
http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
<filter>
<filter-name>struts2</filter-name>
<filter-class>org.apache.struts2.dispatcher.FilterDispatcher</filter-class>
</filter>
<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
</web-app>
3.对应的类
package testpack.xml;
import java.io.PrintWriter;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts2.ServletActionContext;
import org.dom4j.Document;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;
public class CarTypeAction {
public String getCarTypeXML() throws Exception {
System.out.println("diaoyong");
Document document = DocumentHelper.createDocument();
Element root = document.addElement("tree");
root.addAttribute("id", "0");
Element item = root.addElement("item");
item.addAttribute("text", "我的工作台");
item.addAttribute("id", "work console");
item.addAttribute("im0", "folderClosed.gif");
item.addAttribute("im1", "folderOpen.gif");
item.addAttribute("im2", "folderClosed.gif");
Element subItem = item.addElement("item");
subItem.addAttribute("text", "代办事宜");
subItem.addAttribute("id", "working");
subItem.addAttribute("open", "1");
subItem.addAttribute("im0", "folderClosed.gif");
subItem.addAttribute("im1", "folderOpen.gif");
subItem.addAttribute("im2", "folderClosed.gif");
// subItem.addAttribute("im0", "book_titel.gif");
// subItem.addAttribute("im1", "fbook.gif");
// subItem.addAttribute("im2", "book_titel.gif");
// subItem = item.addElement("item");
// subItem.addAttribute("text", "代办事宜2");
// subItem.addAttribute("id", "working");
// subItem.addAttribute("im0", "book_titel.gif");
// subItem.addAttribute("im1", "fbook.gif");
// subItem.addAttribute("im2", "book_titel.gif");
Element subitem1=subItem.addElement("item");
subitem1.addAttribute("text", "代办事宜2-1");
subitem1.addAttribute("id", "working-01");
subitem1.addAttribute("im0", "book_titel.gif");
subitem1.addAttribute("im1", "fbook.gif");
subitem1.addAttribute("im2", "book_titel.gif");
String content = document.asXML();
System.out.println(content);
HttpServletResponse response = ServletActionContext.getResponse();
response.setContentType("text/xml; charset=UTF-8");
response.setHeader("Cache-Control", "no-cache");
PrintWriter pw=response.getWriter();
pw.write(content);
pw.flush();
pw.close();
return null;
}
}
就会生成对应xml