struts2输出相应xml,并输出xml格式给javascript

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

 

 

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值