hibernate+spring+mvc+Easyui框架模式下使用grid++report的总结

时间:2022-02-08 21:27:34

最近刚开始接触hibernate+spring+mvc+Easyui框架,也是刚开通了博客,希望能记录一下自己实践出来的东西,让其他人少走弯路。

转让正题,以个人浅薄的认识hibernate对于开发人员的作用主要是节省了写sql连接数据库的时间,而grid++report内部提供的类来看,却是通过ajax直接连接数据库,还是需要写sql。感觉把grid++report用到这个框架里来的话,给人的感觉就是一匹快马,拉了一辆旧车,虽然速度上影响不了多少,但是审美感觉上很不爽。作为一个完美主义的程序员来说,这是不允许的。然后我就贱贱的尝试着怎么改掉它。关于hibernate+spring+mvc+Easyui框架的东西我就不说了,我只说一下grid++report的使用。

  

  

  1、打开grid++report客户端,连接数据库并绘制报表。

hibernate+spring+mvc+Easyui框架模式下使用grid++report的总结

2.绘制完成后使用记事本打开.grf文件,把里面的数据库连接给清掉。

hibernate+spring+mvc+Easyui框架模式下使用grid++report的总结

3.添加mvc的三个文件在html中载入.grf文件这个不细说,grid++的demo里多的是。

4.修改载入的数据源url

 ReportViewer.Stop();
ReportViewer.DataURL = "user/load";
// var BeginDate = document.getElementById("txtBeginDate").value;
// var EndDate = document.getElementById("txtEndDate").value;
// var DataURL = encodeURI("xmlSummary.aspx?BeginDate=" + BeginDate + "&EndDate=" + EndDate);
// ReportViewer.DataURL = DataURL; //更新查询参数更新报表付标题,设置对应静态框的“Text”属性
//ReportViewer.Report.ControlByName("SubTitle").AsStaticBox.Text = "日期范围:" + BeginDate + "至" + EndDate; ReportViewer.Start();

5.在control中编写load方法获取数据源,由于框架中使用hibernate获得的数据源格式为IList<T>格式,而grid++中接收的是xml格式。所以需要方法把这给转换一下。

度娘告诉我是这样转

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml; namespace Common.ToolsHelper
{
public class XMLHelperToList<T> where T : new()
{
#region 实体类转成Xml
/// <summary>
/// 对象实例转成xml
/// </summary>
/// <param name="item">对象实例</param>
/// <returns></returns>
public static string EntityToXml(T item)
{
IList<T> items = new List<T>();
items.Add(item);
return EntityToXml(items);
} /// <summary>
/// 对象实例集转成xml
/// </summary>
/// <param name="items">对象实例集</param>
/// <returns></returns>
public static string EntityToXml(IList<T> items)
{
//创建XmlDocument文档
XmlDocument doc = new XmlDocument();
//创建根元素
XmlElement root = doc.CreateElement(typeof(T).Name + "s");
//添加根元素的子元素集
foreach (T item in items)
{
EntityToXml(doc, root, item);
}
//向XmlDocument文档添加根元素
doc.AppendChild(root); return doc.InnerXml;
} private static void EntityToXml(XmlDocument doc, XmlElement root, T item)
{
//创建元素
XmlElement xmlItem = doc.CreateElement(typeof(T).Name);
//对象的属性集
System.Reflection.PropertyInfo[] propertyInfo = typeof(T).GetProperties(System.Reflection.BindingFlags.Public | System.Reflection.BindingFlags.Instance); foreach (System.Reflection.PropertyInfo pinfo in propertyInfo)
{
if (pinfo != null)
{
//对象属性名称
string name = pinfo.Name;
//对象属性值
string value = String.Empty; if (pinfo.GetValue(item, null) != null)
value = pinfo.GetValue(item, null).ToString();//获取对象属性值
//设置元素的属性值
xmlItem.SetAttribute(name, value);
}
}
//向根添加子元素
root.AppendChild(xmlItem);
} #endregion #region Xml转成实体类 /// <summary>
/// Xml转成对象实例
/// </summary>
/// <param name="xml">xml</param>
/// <returns></returns>
public static T XmlToEntity(string xml)
{
IList<T> items = XmlToEntityList(xml);
if (items != null && items.Count > )
return items[];
else return default(T);
} /// <summary>
/// Xml转成对象实例集
/// </summary>
/// <param name="xml">xml</param>
/// <returns></returns>
public static IList<T> XmlToEntityList(string xml)
{
XmlDocument doc = new XmlDocument();
try
{
doc.LoadXml(xml);
}
catch
{
return null;
}
if (doc.ChildNodes.Count != )
return null;
if (doc.ChildNodes[].Name.ToLower() != typeof(T).Name.ToLower() + "s")
return null; XmlNode node = doc.ChildNodes[]; IList<T> items = new List<T>(); foreach (XmlNode child in node.ChildNodes)
{
if (child.Name.ToLower() == typeof(T).Name.ToLower())
items.Add(XmlNodeToEntity(child));
} return items;
} private static T XmlNodeToEntity(XmlNode node)
{
T item = new T(); if (node.NodeType == XmlNodeType.Element)
{
XmlElement element = (XmlElement)node; System.Reflection.PropertyInfo[] propertyInfo = typeof(T).GetProperties(System.Reflection.BindingFlags.Public | System.Reflection.BindingFlags.Instance); foreach (XmlAttribute attr in element.Attributes)
{
string attrName = attr.Name.ToLower();
string attrValue = attr.Value.ToString();
foreach (System.Reflection.PropertyInfo pinfo in propertyInfo)
{
if (pinfo != null)
{
string name = pinfo.Name.ToLower();
Type dbType = pinfo.PropertyType;
if (name == attrName)
{
if (String.IsNullOrEmpty(attrValue))
continue;
switch (dbType.ToString())
{
case "System.Int32":
pinfo.SetValue(item, Convert.ToInt32(attrValue), null);
break;
case "System.Boolean":
pinfo.SetValue(item, Convert.ToBoolean(attrValue), null);
break;
case "System.DateTime":
pinfo.SetValue(item, Convert.ToDateTime(attrValue), null);
break;
case "System.Decimal":
pinfo.SetValue(item, Convert.ToDecimal(attrValue), null);
break;
case "System.Double":
pinfo.SetValue(item, Convert.ToDouble(attrValue), null);
break;
default:
pinfo.SetValue(item, attrValue, null);
break;
}
continue;
}
}
}
}
}
return item;
}
#endregion
}
}

然后自己获取数据并Response到页面上

方法如下。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.IO;
using CLGL.Web.Controllers;
using System.Text;
using Wat.Common;
using Domain;
using System.IO.Compression;
using Newtonsoft.Json;
using System.Globalization;
using Common.ToolsHelper; namespace CLGL.Web.Areas.GrfDemo.Controllers
{
public class UserController : Controller
{
//
// GET: /GrfDemo/User/
Service.IUserManager userManage { get; set; }
//User user = new User();
public ActionResult Index()
{
return View();
} public ActionResult Load()
{
IList<User> list = userManage.LoadAll();
string str = XMLHelperToList<User>.EntityToXml(list);
Response.Write(str);
Response.End();
return View();
}
}
}

运行后,结果:

hibernate+spring+mvc+Easyui框架模式下使用grid++report的总结