问题: 提供简单 java 和 javascript API
解决: 对 hbm 得到的 bean[] 自动生成 xml public void addXml(String tableName,String idName,Object[] bean){
格式 <tableName> //主键是IdName
<row id=' idValue '> ........ </row>
.........
<tableName>
Action
说明
1. dao.addXml( " liu " , " ID " ,dao.getSelect()); //向xml 内添加 liu 表信息 主键 ID bean[] 由hbm 提供
2. dao.getXml(); //这里的 主要意思是 把 bean[] 变成 xml (反射)就可以与表无关性了
package
com.yourcompany.struts.action;
import java.io.UnsupportedEncodingException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import com.dao.Dao;
import com.yourcompany.struts.form.TestForm;
/** */ /**
* MyEclipse Struts
* Creation date: 06-05-2007
*
* XDoclet definition:
* @struts.action path="/test" name="testForm" input="/form/test.jsp" scope="request"
*/
public class TestAction extends Action {
public ActionForward execute(
ActionMapping mapping,
ActionForm form,
HttpServletRequest request,
HttpServletResponse response) {
Dao dao = new Dao();
dao.addXml("liu","ID",dao.getSelect());
try {
response.setContentType("text/xml;charset=utf-8");
response.addHeader("Cache-Control","no-cache"); //关键句 要不不刷新问题会郁闷 哈哈
request.setCharacterEncoding("gb2312");
response.setCharacterEncoding("gb2312");
} catch (UnsupportedEncodingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
request.setAttribute("xml",dao.getXml());
System.out.println(dao.getXml());
return mapping.findForward("list") ;
}
}
import java.io.UnsupportedEncodingException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import com.dao.Dao;
import com.yourcompany.struts.form.TestForm;
/** */ /**
* MyEclipse Struts
* Creation date: 06-05-2007
*
* XDoclet definition:
* @struts.action path="/test" name="testForm" input="/form/test.jsp" scope="request"
*/
public class TestAction extends Action {
public ActionForward execute(
ActionMapping mapping,
ActionForm form,
HttpServletRequest request,
HttpServletResponse response) {
Dao dao = new Dao();
dao.addXml("liu","ID",dao.getSelect());
try {
response.setContentType("text/xml;charset=utf-8");
response.addHeader("Cache-Control","no-cache"); //关键句 要不不刷新问题会郁闷 哈哈
request.setCharacterEncoding("gb2312");
response.setCharacterEncoding("gb2312");
} catch (UnsupportedEncodingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
request.setAttribute("xml",dao.getXml());
System.out.println(dao.getXml());
return mapping.findForward("list") ;
}
}
test.jsp
就简单的
<%
response.setContentType("text/xml;charset=utf-8");
response.addHeader("Cache-Control","no-cache");
%>
<%=(String)request.getAttribute("xml")%>
转送 xml
关键页面
功能为可以实现.........
1 . var table_1 = CreateTable(request.responseText, " liu " , my_array,myTitle); //CreateTable.js
就简单建表 以 名字为 liu 的表
并 显示 my_array 几列
后是 表头
与表的无关性 在 xml 上 展现的 没话说 ................
2. 后 扩展 也是 简单的 javascript
< html >
< head >
< title > MyHtml.html </ title >
< SCRIPT language ="Javascript" src ="prototype1.5.0.js" ></ SCRIPT >
< SCRIPT language ="Javascript" src ="CreateTable.js" ></ SCRIPT >
</ head >
< body >
< input id ='test' type ="submit" value ="test" onclick ="searchSales();" >
</ body >
</ html >
< script type ="text/javascript" >
<!--
function searchSales(){
var url = 'http://localhost:7000/xxs/test.do'; // url 不限制 可以为 jsp *.do 只要 out.print()出来的 是 xml 格式
var myAjax = new Ajax.Request(
url,
{
method: 'get',
onComplete: action // 成功得到 requ 的挂载方法
});
}
function action(request)
{
var body = document.getElementsByTagName('body')[0];
var my_array = new Array();
my_array[0] = "NAME";
my_array[1] = "AVG";
var myTitle = new Array();
myTitle[0] = '\u540d\u5b57'; //名字
myTitle[1] = '\u5e74\u9f84'; //年龄
var table_1 = CreateTable(request.responseText, "liu", my_array,myTitle); //CreateTable.js
addTableEdit(table_1, '/edit.do' ); //CreateTable.js
delTableEdit(table_1, '/del.do' ); //CreateTable.js
addTableStyle_0(table_1); //CreateTable.js
body.appendChild(table_1);
}
//-->
</ script >
下面是 支持类
1. java 方面
Dao.java
package
com.dao;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.Enumeration;
import java.util.Hashtable;
import java.util.Iterator;
import java.util.List;
import org.hibernate.Criteria;
import org.hibernate.Session;
import org.hibernate.Transaction;
import com.HibernateSessionFactory;
import com.bean.Xmltest;
public class Dao {
StringBuffer str = new StringBuffer(); {
str.append("<?xml version='1.0' encoding='gb2312'?>").append("\n");
str.append("<tables>").append("\n");
}
public Xmltest[] getSelect(){
Session sess = HibernateSessionFactory.currentSession();
List list = null ;
try {
Transaction tr = sess.beginTransaction() ;
Criteria cr = sess.createCriteria(Xmltest.class);
list = cr.list();
tr.commit();
} catch (Exception e) {}
finally{
HibernateSessionFactory.closeSession();
}
return (Xmltest[])list.toArray(new Xmltest[0]) ;
}
public void addXml(String tableName,String idName,Object[] obj){
Hashtable m1 = new Hashtable();
Hashtable m2 = new Hashtable();
str.append("<").append(tableName).append(">").append("\n");
// 取出 类 一 的 set 方法
Method[] me1 = obj[0].getClass().getMethods() ;
for ( int j = 0 ;j < me1.length;j ++ ) {
String name = me1[j].getName() ;
if ( name.indexOf("set") == 0 ) {
String Att = (name.substring( 3 ,name.length())).toUpperCase();
m1.put(Att,me1[j]);
}
}
Method[] me2 = obj[0].getClass().getMethods() ;
for ( int i = 0 ;i < me2.length;i ++ ) {
String name = me2[i].getName() ;
if ( name.indexOf( "get" ) == 0 ) {
String Att = (name.substring( 3 ,name.length())).toUpperCase();
if( m1.get(Att)!= null )
m2.put( Att,me2[i]);
}
}
try {
for(int i=0;i<obj.length;i++){
str.append("<row id='").append( ((Method)m2.get(idName)).invoke(obj[i],null)+"" ).append("'>").append("\n");
for( Enumeration en = m2.keys();en.hasMoreElements();){
String name = (String)en.nextElement();
Method me = (Method) m2.get(name);
str.append("<").append(name).append(">") ;
str.append( me.invoke(obj[i],null) );
str.append("</").append(name).append(">").append("\n") ;
}
str.append("</row>").append("\n");
}
} catch (Exception e) {System.out.println(e);}
str.append("</").append(tableName).append(">").append("\n");
}
public String getXml(){
str.append("</tables>").append("\n");
return str.toString();
}
}
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.Enumeration;
import java.util.Hashtable;
import java.util.Iterator;
import java.util.List;
import org.hibernate.Criteria;
import org.hibernate.Session;
import org.hibernate.Transaction;
import com.HibernateSessionFactory;
import com.bean.Xmltest;
public class Dao {
StringBuffer str = new StringBuffer(); {
str.append("<?xml version='1.0' encoding='gb2312'?>").append("\n");
str.append("<tables>").append("\n");
}
public Xmltest[] getSelect(){
Session sess = HibernateSessionFactory.currentSession();
List list = null ;
try {
Transaction tr = sess.beginTransaction() ;
Criteria cr = sess.createCriteria(Xmltest.class);
list = cr.list();
tr.commit();
} catch (Exception e) {}
finally{
HibernateSessionFactory.closeSession();
}
return (Xmltest[])list.toArray(new Xmltest[0]) ;
}
public void addXml(String tableName,String idName,Object[] obj){
Hashtable m1 = new Hashtable();
Hashtable m2 = new Hashtable();
str.append("<").append(tableName).append(">").append("\n");
// 取出 类 一 的 set 方法
Method[] me1 = obj[0].getClass().getMethods() ;
for ( int j = 0 ;j < me1.length;j ++ ) {
String name = me1[j].getName() ;
if ( name.indexOf("set") == 0 ) {
String Att = (name.substring( 3 ,name.length())).toUpperCase();
m1.put(Att,me1[j]);
}
}
Method[] me2 = obj[0].getClass().getMethods() ;
for ( int i = 0 ;i < me2.length;i ++ ) {
String name = me2[i].getName() ;
if ( name.indexOf( "get" ) == 0 ) {
String Att = (name.substring( 3 ,name.length())).toUpperCase();
if( m1.get(Att)!= null )
m2.put( Att,me2[i]);
}
}
try {
for(int i=0;i<obj.length;i++){
str.append("<row id='").append( ((Method)m2.get(idName)).invoke(obj[i],null)+"" ).append("'>").append("\n");
for( Enumeration en = m2.keys();en.hasMoreElements();){
String name = (String)en.nextElement();
Method me = (Method) m2.get(name);
str.append("<").append(name).append(">") ;
str.append( me.invoke(obj[i],null) );
str.append("</").append(name).append(">").append("\n") ;
}
str.append("</row>").append("\n");
}
} catch (Exception e) {System.out.println(e);}
str.append("</").append(tableName).append(">").append("\n");
}
public String getXml(){
str.append("</tables>").append("\n");
return str.toString();
}
}
javascript 支持方面
function getXmlDom(xml) {
var dom = new ActiveXObject("Microsoft.XMLDOM");
dom.loadXML(xml);
return dom;
}
function CreateTable(xml,tableName,tdNames,titles) {
tbl = document.createElement('table')
var tblBody = document.createElement('tbody')
var tr = document.createElement('tr')
for (var i = 0; i <titles.length; i++) {
var th = document.createElement('th')
th.innerHTML = titles[i] ;
tr.appendChild(th);
}
tblBody.appendChild(tr);
var tableXml = getXmlDom(xml).getElementsByTagName(tableName)[0];
var rows = tableXml.getElementsByTagName("row");
for (var i = 0; i <rows.length; i++) {
var tr = document.createElement('tr')
tr.setAttribute('id', rows[i].getAttribute('id') )
for(var j=0;j<tdNames.length;j++){
var col = rows.item(i).getElementsByTagName(tdNames[j])[0];
var value = col.firstChild.nodeValue ;
var td = document.createElement('td');
td.innerHTML = value ;
tr.appendChild(td);
}
tblBody.appendChild(tr);
}
tbl.appendChild(tblBody);
return tbl;
}
function addTableEdit( table , editURL ) {
var rows = table.getElementsByTagName("tr");
for(var i=1;i<rows.length;i++){
var newLink=document.createElement('a');
var url = editURL+'?id='+rows[i].getAttribute('id') ;
newLink.setAttribute('href', url );
var linkText=document.createTextNode("\u7f16\u8f91");
newLink.appendChild(linkText);
var td = document.createElement('td');
td.appendChild( newLink );
rows[i].appendChild(td);
}
}
function delTableEdit( table , editURL ) {
var rows = table.getElementsByTagName("tr");
for(var i=1;i<rows.length;i++){
var newLink=document.createElement('a');
var url = editURL+'?id='+rows[i].getAttribute('id') ;
newLink.setAttribute('href', url );
newLink.innerText = '\u5220\u9664'
var td = document.createElement('td');
td.appendChild( newLink );
rows[i].appendChild(td);
}
}
function addTableStyle_0(table) {
var listtr = table.getElementsByTagName('tr')
for(var i=0;i<listtr.length;i++){
listtr[i].setAttribute("align","center");
if(i%2==0)
listtr[i].setAttribute("bgColor","#33ffff");
}
table.setAttribute("border","1");
}
下载
注意 lib 中 *.jar 包 没有 可以看 .classpath 文件自己配
http://www.blogjava.net/Files/Good-Game/xxs.rar