Struts2.xml file Configuration:
<?xml version="1.0" encoding="UTF-8" ?><!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN" "http://struts.apache.org/dtds/struts-2.0.dtd"><struts> <package name="example" namespace="/example" extends="struts-default"><action name="getCarTypeXML" method="getCarTypeXML" class="com.iss.action.CarTypeAction"><result name="xmlMessage" type="plaintext"></result></action> </package></struts>
Index. jsp: page for loading trees
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%><%String path = request.getContextPath();String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";%><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
tree.loadXML("<%=basePath%>/example/getCarTypeXML.action");</script> </body>
Action Code in struts2:
Public void getcartypexml () throws exception {document = incluenthelper. createdocument (); element root = document. addelement ("Tree"); root. addattribute ("ID", "0"); element item = root. addelement ("item"); item. addattribute ("text", "My workbench"); 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", "agent"); subitem. addattribute ("ID", "working"); subitem. addattribute ("im0", "book_titel.gif"); subitem. addattribute ("im1", "fbook.gif"); subitem. addattribute ("IM2", "book_titel.gif"); subitem = item. addelement ("item"); subitem. addattribute ("text", "Agent 2"); subitem. addattribute ("ID", "working"); subitem. addattribute ("im0", "book_titel.gif"); subitem. addattribute ("im1", "fbook.gif"); subitem. 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 ();}