源码如下:
public class SonToXmlServlet extends HttpServlet{
/**
*
*/
private static final long serialVersionUID = 1L;
private Logger logger=Logger.getLogger(this.getClass());
public static final String XML_TOP= " <?xml version=\ "1.0\ " encoding=\ "UTF-8\ "?> ";
protected void service(HttpServletRequest req,HttpServletResponse resp)
throws ServletException,java.io.IOException{
try{
String ParentID = RequestUtils.getStringParameter(req, "parentid ", "0 ");
String action = RequestUtils.getStringParameter(req, "action ", " ");
StringBuffer sb = new StringBuffer( " ");
String path = SonToXmlServlet.class.getResource( "/ ").toString();
path = path.substring(0,path.lastIndexOf( "/ "));
path = path.substring(0,path.lastIndexOf( "/ "));
logger.debug( "path is "+path);
ApplicationContext ctx = new FileSystemXmlApplicationContext(new String[] {
path+ "/action-servlet.xml ",
path+ "/applicationContext.xml ",
path+ "/applicationContext-hibernate.xml "
});
sb.append(XML_TOP);
if( "addclass ".equals(action)){
logger.debug( "11 ");
sb.append(this.getTreeByClass(ParentID,3,action,req,resp,ctx));
}else if( "uclass ".equals(action)){
logger.debug( "uu ");
sb.append(this.getTreeByUserClass(ParentID,3,action,req,resp,ctx));
}
resp.setContentType( "text/xml;charset=UTF-8 ");
resp.getWriter().print(sb.toString());
}catch(Exception e){
logger.info(e.toString());
}
}
private StringBuffer getTreeByClass(String parentid,
int s,
String action,
HttpServletRequest req,
HttpServletResponse resp,
ApplicationContext ctx){
StringBuffer sb = new StringBuffer( " ");
List l = null;
ClassManager cm = (ClassManager)ctx.getBean( "mclassManager ");
l = cm.getArticleClass(parentid,0,req,resp);
logger.debug(l.size());
Iterator it = l.iterator();
sb.append( " <tree> ");
while(it.hasNext()){
TArticleclass t = (TArticleclass)it.next();
String wantAction = " ";
switch(s){
case 3:wantAction = "javascript:i( 'inid ', ' "+t.getClassnameToQuote()+ " ', ' "+t.getClassid()+ " ', ' "+t.getParentid()+ " ', ' "+t.getChannelid()+ " '); ";break;
}
if(t.getChild()> 0)
sb.append( " <tree text=\ " "+t.getClassnameToQuote()+ "\ " src=\ "sontoxml?parentid= "+t.getClassid()+ "&action= "+action+ "\ " action=\ " "+wantAction+ "\ "/> ");
else
sb.append( " <tree text=\ " "+t.getClassnameToQuote()+ "\ " action=\ " "+wantAction+ "\ "/> ");