当前位置: 代码迷 >> Web前端 >> juery 跟action
  详细解决方案

juery 跟action

热度:309   发布时间:2012-08-24 10:00:21.0
juery 和action
1.引入下jquery.js类库 http://jquery.com/ 建立页面xmlTest.jsp

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <script type="text/javascript" src="jquery/jquery.js"></script>
    <script type="text/javascript">
    function getInfo()
    {
$.post("getXmlAction.action",
{  
//发送参数name及值
name: $("#name").val()
}, function(returnedData, status)
{    
//服务器返回成功的值
if("success" == status)

//根据服务器返回的xml,获取各个节点的值
var id = $(returnedData).find("id").text();
var name = $(returnedData).find("name").text();
var age = $(returnedData).find("age").text();
var address = $(returnedData).find("address").text();
                //动态添加表格
var html = "<table width='60%' border='1' align='center'><tr><th>id</th><th>name</th><th>age</th><th>address</th></tr><tr align='center'><td>" + id + "</td><td>" + name + "</td><td>"+ age + "</td><td>" + address + "</td></tr></table>";
                //获得浏览器值,先清空浏览器表格数据  
$("body table:eq(0)").remove();
                 //把动态获取的表格添加到页面中
$("#theBody").append(html);
}
}
);
    }
    </script>
  </head>
  <body id="theBody">
   <select id="name">
   <option value="zhangsan" selected="selected">zhangsan</option>
   <option value="lisi">lisi</option>
   </select>
   <input type="button" value="get info" onclick="getInfo();">
  </body>
</html>

2 简单struts.xml文件的配置

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.1//EN" "http://struts.apache.org/dtds/struts-2.1.dtd">
<struts>
<package name="struts2.1" extends="json-default">
<action name="getXmlAction" class="com.test.action.xml.GetXmlAction"></action>
</package>
</struts> 

3 使用dom4j创建xml文档 下载dom4j.jar引入到项目
http://www.dom4j.org/dom4j-1.6.1/

package com.test.action.xml;

import java.io.PrintWriter;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts2.ServletActionContext;
import org.dom4j.*;
import org.dom4j.io.OutputFormat;
import org.dom4j.io.XMLWriter;
import com.opensymphony.xwork2.ActionSupport;
public class GetXmlAction extends ActionSupport {
private String name;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@Override
public String execute() throws Exception {
//zhangsan
Person person1 = new Person();
person1.setId(1);
person1.setName("zhangsan");
person1.setAge(20);
person1.setAddress("shanghai");

//lisi
Person person2 = new Person();
person2.setId(2);
person2.setName("lisi");
person2.setAge(30);
person2.setAddress("tianjin");

//代表整个xml文档
Document document = DocumentHelper.createDocument();
//代表文档根元素
Element rootElement = document.addElement("users");
//增加一个注释
rootElement.addComment("This is a comment!");
//增加一个根元素下的一个user子元素
Element userElement = rootElement.addElement("user");
//增加一个user子元素的一个各个user属性子元素
Element idElement = userElement.addElement("id");
Element nameElement = userElement.addElement("name");
Element ageElement = userElement.addElement("age");
Element addressElement = userElement.addElement("address");

//文档节点赋值
if("zhangsan".equals(this.name))
{
idElement.setText(person1.getId() + "");
nameElement.setText(person1.getName());
ageElement.setText(person1.getAge() + "");
addressElement.setText(person1.getAddress());
}
else
{
idElement.setText(person2.getId() + "");
nameElement.setText(person2.getName());
ageElement.setText(person2.getAge() + "");
addressElement.setText(person2.getAddress());
}

//获得客户端的相应的response对象   并设置向客户端输出的格式
HttpServletResponse response = ServletActionContext.getResponse();
response.setContentType("text/xml; charset=utf-8");//客户端的输入格式 和字体
response.setHeader("Cache-Control", "no-cache"); //取消浏览器缓存

    //获取向客户端输出流
PrintWriter out = response.getWriter();

//dom4j的输出格式
OutputFormat format = OutputFormat.createPrettyPrint();
format.setEncoding("utf-8");

XMLWriter writer = new XMLWriter(out, format);
//把文档格式document写到输出流中out中  返回给客户端
writer.write(document);
out.flush();
out.close();
return null;

}
}
  相关解决方案