ASP.NET MVC2+Spring.net1.3+Nhibernate1.2.1+Jquery完美框架

简介:
今天我就把上个月搭的MVC2+Spring.net+Nhibernate+Jquery开发框架介绍给大家,虽然本框架已经被老板“下架”,但是我还是想把这个实践经验介绍给大家。
首先先看看文件夹结构。
先看看Dao层。
其中Implement下放的是实现接口的Dao。Interface下放的是接口。我们再看看实现类BaseDao
InBlock.gif  public  class BaseDao<T, PK> :    HibernateDaoSupport,IDao<T, PK> 
InBlock.gif        { 
InBlock.gif                Type type; 
InBlock.gif                ISession iSession =  null
InBlock.gif                 public BaseDao() 
InBlock.gif                { } 
InBlock.gif 
InBlock.gif                 public BaseDao(Type type) 
InBlock.gif                { 
InBlock.gif                         this.type = type; 
InBlock.gif                } 
InBlock.gif 
InBlock.gif                 public T Get(PK pk) 
InBlock.gif                { 
InBlock.gif                        T test = (T)HibernateTemplate.Get(type, pk); 
InBlock.gif                         return test; 
InBlock.gif                } 
InBlock.gif 
InBlock.gif                 public IList GetAll() 
InBlock.gif                { 
InBlock.gif                         return HibernateTemplate.ExecuteFind( new HibernateGetAll<T>(type)); 
InBlock.gif                } 
InBlock.gif 
InBlock.gif                 public PK Save(T entity) 
InBlock.gif                { 
InBlock.gif                         return (PK)HibernateTemplate.Execute( new HibernateSave<T, PK>(entity)); 
InBlock.gif                } 
InBlock.gif 
InBlock.gif                 public  void Update(T entity) 
InBlock.gif                { 
InBlock.gif                        HibernateTemplate.Execute( new HibernateUpdate<T>(entity)); 
InBlock.gif                } 
InBlock.gif 
InBlock.gif                 public  void SaveOrUpdate(T entity) 
InBlock.gif                { 
InBlock.gif                        HibernateTemplate.Execute( new HibernateSaveOrUpdate<T>(entity)); 
InBlock.gif                } 
InBlock.gif 
InBlock.gif                 public  void Delete(T entity) 
InBlock.gif                { 
InBlock.gif                        HibernateTemplate.Execute( new HibernateDelete<T>(entity)); 
InBlock.gif                } 
InBlock.gif 
InBlock.gif                 public IList FindByCriteria(ICriterion[] Criterions) 
InBlock.gif                { 
InBlock.gif                         return HibernateTemplate.ExecuteFind( new HibernateFindByCriteria<T>(Criterions, type)); 
InBlock.gif                } 
InBlock.gif 
InBlock.gif                 public IList FindByCriteria(Order order, ICriterion[] criterions) 
InBlock.gif                { 
InBlock.gif                         return HibernateTemplate.ExecuteFind( new HibernateFindByCriterias<T>(order, criterions, type)); 
InBlock.gif                } 
InBlock.gif                 public IList GetListForPage(Type type, ICriterion[] criterions, 
InBlock.gif                          int offset,  int length) 
InBlock.gif                { 
InBlock.gif                         return HibernateTemplate.ExecuteFind( new HibernateGetListForPage<T>(criterions, type, offset, length)); 
InBlock.gif                } 
InBlock.gif 
InBlock.gif                 public IList FindByCriteria( int firstResult,  int rowCount, Order order, ICriterion[] criterions) 
InBlock.gif                { 
InBlock.gif                         return HibernateTemplate.ExecuteFind( new HibernateFindByCriteriass<T>(firstResult, rowCount, order, type, criterions)); 
InBlock.gif                } 
InBlock.gif 
InBlock.gif                 public IList GetAggValueByCriteria(ProjectionList projectionList, ICriterion[] criterions) 
InBlock.gif                { 
InBlock.gif                         return HibernateTemplate.ExecuteFind( new HibernateGetAggValue<T>(projectionList, criterions, type)); 
InBlock.gif                } 
InBlock.gif 
InBlock.gif                 public IList FindByNamedParam( string queryString,  string[] paramNames,  object[] values) 
InBlock.gif                { 
InBlock.gif                         return HibernateTemplate.FindByNamedParam(queryString, paramNames, values); 
InBlock.gif                } 
InBlock.gif
InBlock.gif}
BaseDao去实现IDao类。我们再看看每个Dao类去继承BaseDao实现对应的接口
InBlock.gif using System; 
InBlock.gif using System.Collections.Generic; 
InBlock.gif using System.Linq; 
InBlock.gif using System.Text; 
InBlock.gif 
InBlock.gif namespace Dao.Implement.SysManager 
InBlock.gif
InBlock.gif         using Dao.Interface.SysManager; 
InBlock.gif         using Dao.Implement.Util; 
InBlock.gif         using Model; 
InBlock.gif         public  class SysCodeDao : BaseDao<SS_CODE, SS_CODEID>,ISysCodeDao 
InBlock.gif        { 
InBlock.gif                 public SysCodeDao() 
InBlock.gif                        :  base( typeof(SS_CODE)) 
InBlock.gif                { } 
InBlock.gif        } 
InBlock.gif
我们再看看其对应的接口由于篇幅有限,我们接着看Model层
其中Models文件夹下放的是实体类,Mappings下放的是Nhibernate映射的xml文件。
Util下放的是要使用的关于加载Json解析的类。
接下来我们再看看我的程序集SpringConfig,其中的Controller_Dao.xml是对控制器中所使用用到的Dao注入到控制器。Dao.xml是对Dao的Spring配置。Spring_Hibernate.xml是对Spring+Nhibernate组合的一个配置。
我们大致来看看,Dao.xml
<? xml  version ="1.0"  encoding ="utf-8" ?> 
< objects  xmlns ="http://www.springframework.net" 
                  xmlns:xsi ="http://www.w3.org/2001/XMLSchema-instance" 
                  xsi:schemaLocation ="http://www.springframework.net http://www.springframework.net/xsd/spring-objects.xsd" > 
     < object  id ="CodeDao"  type ="Dao.Implement.SysManager.SysCodeDao,Dao"  autowire ="byName" > 
         < property  name ="sessionFactory"  ref ="sessionFactory"  /> 
     </ object > 
     < object  id ="sysOrgDao"  type ="Dao.Implement.SysManager.SysOrgDao,Dao"  autowire ="byName" > 
         < property  name ="sessionFactory"  ref ="sessionFactory"     /> 
     </ object > 
     < object  id ="sysCodeDao"  type ="Dao.Implement.SysManager.SysCodeDao,Dao"  autowire ="byName" > 
         < property  name ="sessionFactory"  ref ="sessionFactory"  /> 
     </ object > 
     < object  id ="sysUserDao"  type ="Dao.Implement.SysManager.SysUserDao,Dao" > 
         < property  name ="sessionFactory"  ref ="sessionFactory"     /> 
     </ object > 
     < object  id ="sysLogInfoDao"  type ="Dao.Implement.SysManager.SysLogInfoDao" > 
         < property  name ="sessionFactory"  ref ="sessionFactory"     /> 
     </ object > 
     < object  id ="sysModuleDao"  type ="Dao.Implement.SysManager.SysModulDao" > 
         < property  name ="sessionFactory"  ref ="sessionFactory"     /> 
     </ object > 
</ objects >
Spring_Dao.xml
<? xml  version ="1.0"  encoding ="utf-8" ?> 
< objects  xmlns ="http://www.springframework.net" 
                  xmlns:xsi ="http://www.w3.org/2001/XMLSchema-instance" 
                  xsi:schemaLocation ="http://www.springframework.net http://www.springframework.net/xsd/spring-objects.xsd" > 
    <!--  部门管理所需加载的对象 --> 
     < object  id ="ssCodeId"  type ="Model.SS_CODEID,Model"  autowire ="byName" /> 
     < object  id ="ssOrga1"  type ="Model.SS_ORGA,Model"  autowire ="byName" > </ object > 
     < object  id ="ssCode"  type ="Model.SS_CODE,Model"  autowire ="byName" > </ object > 
     < object  id ="ListItem" type="Utility.Common.ListItem<Model.SS_CODE >,Utility" autowire="byName"> 
     </ object > 

     < object  id ="OrgaController"  type ="EduCationManage.Controllers.SysManager.OrgaController,EduCationManage"  singleton ="false" > 
         < property  name ="ssOrga"  ref ="ssOrga1" > </ property > 
         < property  name ="ssCode"  ref ="ssCode" > </ property > 
         < property  name ="sysOrgDao"  ref ="sysOrgDao" > </ property > 
         < property  name ="sysCodeDao"  ref ="sysCodeDao" > </ property > 
         < property  name ="ssCodeId"  ref ="ssCodeId" > </ property > 
         < property  name ="myListItem"  ref ="ListItem" > </ property > 
     </ object > 

     < object  id ="ssUser1"  type ="Model.SS_USER,Model"  autowire ="byName" > </ object > 
     < object  id ="ssLogInfos"  type ="Model.SS_LOG_INFO,Model"  autowire ="byName" > </ object > 
     < object  id ="LoginController"  type ="EduCationManage.Controllers.LoginController,EduCationManage"  singleton ="false" > 
         < property  name ="ssUser"  ref ="ssUser1"  /> 
         < property  name ="ssLogInfos"  ref ="ssLogInfos" > </ property > 
         < property  name ="sysUserDao"  ref ="sysUserDao" > </ property > 
         < property  name ="sysLogInfoDao"  ref ="sysLogInfoDao" > </ property > 
         < property  name ="sysModuleDao"  ref ="sysModuleDao" > </ property > 
     </ object > 
</ objects > 
Spring_Hibernate.xml
ok,上面的就是本项目比较关键的配置。紧接着我们赶紧看看控制器
InBlock.gif using System; 
InBlock.gif using System.Collections; 
InBlock.gif using System.Collections.Generic; 
InBlock.gif using System.Linq; 
InBlock.gif using System.Web; 
InBlock.gif using System.Web.Mvc; 
InBlock.gif 
InBlock.gif namespace EduCationManage.Controllers.SysManager 
InBlock.gif
InBlock.gif         using Model; 
InBlock.gif         using Dao.Interface.SysManager; 
InBlock.gif         using EduCationManage.Controllers.Util; 
InBlock.gif         using NHibernate.Criterion; 
InBlock.gif         using Utility.Common; 
InBlock.gif         using Utility.Json; 
InBlock.gif         using Model.Util; 
InBlock.gif         public  class OrgaController : BaseController 
InBlock.gif        { 
InBlock.gif                 private SS_ORGA ssOrga ; //部门类 
InBlock.gif                 private SS_CODE ssCode ; //系统参数类 
InBlock.gif                 private SS_CODEID ssCodeId ; //系统参数复合主键类 
InBlock.gif                 private ISysOrgDao sysOrgDao ; //部门dao 
InBlock.gif                 private ISysCodeDao sysCodeDao ; //系统参数dao 
InBlock.gif                 private ListItem<SS_CODE> myListItem; 
InBlock.gif                 private List<SS_CODE> orgaTypeList ;     //部门类型的List 
InBlock.gif                 private List<SS_ORGA> orgaList =  new List<SS_ORGA>();  //部门的List 
InBlock.gif                 private String strJson ; //返回页面值 
InBlock.gif                 private  int total ; //总页数 
InBlock.gif                 private  int page ; //当前页 
InBlock.gif                 private  int records ; //总记录数 
InBlock.gif                 public ActionResult Execute() 
InBlock.gif                { 
InBlock.gif                        ICriterion[] criterions =  new ICriterion[1]; 
InBlock.gif                        criterions[0] = Expression.Eq( "Id.C_ENAME""ORGATYPE"); 
InBlock.gif                         this.orgaTypeList = (List<SS_CODE>)sysCodeDao.FindByCriteria(criterions); 
InBlock.gif                        SelectList listItem = myListItem.GetListItem(orgaTypeList,  "C_DISPLAY_CONTENT""", "---选择---"); 
InBlock.gif                        ViewData.Add( "listItem", listItem); 
InBlock.gif                         return View( "~/Views/SysManage/Orga/OrgaManager.aspx"); 
InBlock.gif                } 
InBlock.gif 
InBlock.gif                [HttpPost] 
InBlock.gif                 public JsonResult Show() 
InBlock.gif                { 
InBlock.gif                        SS_USER user = Session[ "user"as SS_USER; 
InBlock.gif                        String strOrgaId =  "0010"; // user.C_ORGA_ID;//登录者的所在部门ID 
InBlock.gif                         try 
InBlock.gif                        { 
InBlock.gif                                ssOrga = sysOrgDao.Get(strOrgaId); 
InBlock.gif                                 if (ssOrga.chkManageOrga()) 
InBlock.gif                                { 
InBlock.gif                                        ssOrga = sysOrgDao.Get(CONS_ROOT_ID); 
InBlock.gif                                } 
InBlock.gif                                JSTreeNode treeNode =  this.CreateJSTreeforOrgTree(ssOrga); 
InBlock.gif                                nodeList.Add(treeNode); 
InBlock.gif                                strJson =  "{suc:1,msg:'生成树成功'}"
InBlock.gif                                log.Info( "生成树成功"); 
InBlock.gif                                 return Json(nodeList, JsonRequestBehavior.AllowGet); 
InBlock.gif                        } 
InBlock.gif                         catch (Exception) 
InBlock.gif                        { 
InBlock.gif                                strJson =  "{suc:0,msg:'生成树失败'}"
InBlock.gif                                log.Error( "生成树失败"); 
InBlock.gif                                 return Json( "{msg:'生成树失败'}"); 
InBlock.gif                        } 
InBlock.gif                } 
InBlock.gif
InBlock.gif}
我们在使用类的时候,只需要声明无需实例化,因为实例化这个过程由Spring容器管理。还要讲的是,有接口的类我们都是用接口声明,而在spring配置中只要配置成实现类即可,这样有利于代码的安全。
再看看页面
< %@ Page  Language ="C#" Inherits="System.Web.Mvc.ViewPage<Model.Models.SystemManage.Orga.SS_ORGA >" %> 

< !DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd" > 
< html  xmlns ="http://www.w3.org/1999/xhtml" > 
< head  runat ="server" > 
         < title >OrgaManage </title> 
         < link  rel ="Stylesheet"  href ="../../Content/form.css"  /> 
         < script  type ="text/javascript"  src ="../../Scripts/Project/SysManages/orgmanager.js" > </script> 
</head> 
< body > 
         < div  id ="orgWapper"  style ="overflow: hidden; margin: 0; padding-right: 0;" > 
                <div id="Basic_html" style="float: left; overflow: hidden; width: 16%; margin-left: 0px; 
                        padding: 0;"> 
                 </div> 
                 < div  style ="overflow: auto; padding: 0; margin: 0;" > 
                         < div  id ="orgRightPane"  style ="margin-left: 0px; padding: 0; float: right;" > 
                                 < table  id ="dataGrid" > 
                                 </table> 
                                 < div  id ="pager"  style ="text-align: right; clear: both;" > 
                                 </div> 
                         </div> 
                 </div> 
         </div> 
        <div id="content" class="content ui-widjet window" style="text-align: center; width: 460px; 
                height: 260px; display: none"> 
                 < div  class ="headbar" > 
                        <img id="img_close" src="../../../Images/gbb.gif" style="float: right; cursor: pointer;" 
                                alt="" /> 
                        <span style="float: left; height: 30px; line-height: 30px; color: #fff; font-size: 14px; 
                                font-weight: bolder">  部门信息 </span> 
                 </div> 
                <% using (Html.BeginForm("", "Orga", FormMethod.Post, new { id = "orgaForm" })) 
                     { %> 
                 < %: Html.HiddenFor(m = > m.C_FATHER_ID, new { id = "orgaFatherId", name = "CFatherId" })%> 
                 < script  language ="javascript"  type ="text/javascript" > 
                        var x = document.getElementById("orgaFatherId"); 
                        alert(x.value); 
                 </script> 
                 < %: Html.HiddenFor(m = > m.C_ORGA_ID, new { id = "orgaId", name = "COrgaId" })%> 
                 < table  border ="0"  class ="tableStyle" > 
                         < tr > 
                                 < td > 
                                          
                                 </td> 
                         </tr> 
                         < tr > 
                                 < td  align ="right" > 
                                        部门名称 
                                 </td> 
                                 < td  align ="left" > 
                                         < %: Html.TextBoxFor(m = > m.C_ORGA_NM, new { id = "orgaName", cssClass = "text required" })%> 
                                         < font  color ="red"  style ="font-size: 20" >* </font> 
                                 </td> 
                                 < td  align ="right" > 
                                        部门类型 
                                 </td> 
                                 < td  align ="left" > 
                                         < %: Html.DropDownListFor(m = > m.C_ORGA_TP, ViewData["listItem"] as SelectList, new { id = "orgaType", name = "COrgaTp" })%> 
                                         < font  color ="red"  style ="font-size: 20" >* </font> 
                                 </td> 
                         </tr> 
                         < tr > 
                                 < td  align ="right" > 
                                        负责人 
                                 </td> 
                                 < td  align ="left" > 
                                         < %:Html.TextBoxFor(m= >m.C_LEADER, new { id = "leader", cssClass = "text" })%> 
                                         < font  color ="red"  style ="font-size: 20" >* </font> 
                                 </td> 
                                 < td  align ="right" > 
                                        电话 
                                 </td> 
                                 < td  align ="left" > 
                                         < %:Html.TextBoxFor(m = > m.C_PHONE, new { id = "phone", cssClass = "text", Size = "20px" })%> 
                                         < font  color ="red"  style ="font-size: 20" >* </font> 
                                 </td> 
                         </tr> 
                         < tr > 
                                 < td  align ="right" > 
                                        Email 
                                 </td> 
                                 < td  colspan ="3"  align ="left" > 
                                         < %:Html.TextBoxFor(m= >m.C_EMAIL, new { id = "email", cssClass = "text", Size = "60px" })%> 
                                 </td> 
                         </tr> 
                         < tr > 
                                 < td  align ="right"  valign ="top" > 
                                        部门描述 
                                 </td> 
                                 < td  colspan ="3"  align ="left" > 
                                         < %: Html.TextAreaFor(m= >m.C_ORGA_DESC, new { id = "orgaDesc", cssClass = "text", Columns = "58", Rows = "4" })%> 
                                 </td> 
                         </tr> 
                         < tr > 
                                 < td  colspan ="4"  align ="center" > 
                                         < button  id ="orgModified"  name ="modified"  type ="button"  class ="Pcip1" > 
                                                修 改 
                                         </button> 
                                         < button  id ="orgSave"  name ="orgSave"  type ="submit"  class ="Pcip1" > 
                                                保 存 
                                         </button> 
                                         < button  id ="orgReset"  type ="button"  name ="orgReset"  class ="Pcip1" > 
                                                重 置 
                                         </button> 
                                         < button  id ="orgClose"  name ="close"  type ="button"  class ="Pcip1" > 
                                                取 消 
                                         </button> 
                                 </td> 
                         </tr> 
                 </table> 
                 < %} % > 
         </div> 
         < div  id ="mask"  class ="mask" > 
         </div> 
</body> 
</html> 
废话不多说,我们最后看看公共类。
一个是Json解析,另外的是一些加密,下拉绑定的公用类。
ok,大概就这么介绍下,如果那位仁兄想要我的源代码,请留言,在要之前,确保你的机子上装上了VS2010。


本文转自 BruceAndLee 51CTO博客,原文链接:http://blog.51cto.com/leelei/343101,如需转载请自行联系原作者

相关文章
|
1月前
|
缓存 前端开发 Java
Spring MVC 面试题及答案整理,最新面试题
Spring MVC 面试题及答案整理,最新面试题
90 0
|
1月前
ssm(Spring+Spring mvc+mybatis)——updateDept.jsp
ssm(Spring+Spring mvc+mybatis)——updateDept.jsp
10 0
|
1月前
ssm(Spring+Spring mvc+mybatis)——showDept.jsp
ssm(Spring+Spring mvc+mybatis)——showDept.jsp
9 0
|
1月前
|
SQL JavaScript Java
springboot+springm vc+mybatis实现增删改查案例!
springboot+springm vc+mybatis实现增删改查案例!
26 0
|
1月前
|
SQL Java 数据库连接
挺详细的spring+springmvc+mybatis配置整合|含源代码
挺详细的spring+springmvc+mybatis配置整合|含源代码
42 1
|
15天前
|
数据采集 前端开发 Java
数据塑造:Spring MVC中@ModelAttribute的高级数据预处理技巧
数据塑造:Spring MVC中@ModelAttribute的高级数据预处理技巧
23 3
|
15天前
|
存储 前端开发 Java
会话锦囊:揭示Spring MVC如何巧妙使用@SessionAttributes
会话锦囊:揭示Spring MVC如何巧妙使用@SessionAttributes
14 1
|
15天前
|
前端开发 Java Spring
数据之桥:深入Spring MVC中传递数据给视图的实用指南
数据之桥:深入Spring MVC中传递数据给视图的实用指南
31 3
|
25天前
|
前端开发 安全 Java
使用Java Web框架:Spring MVC的全面指南
【4月更文挑战第3天】Spring MVC是Spring框架的一部分,用于构建高效、模块化的Web应用。它基于MVC模式,支持多种视图技术。核心概念包括DispatcherServlet(前端控制器)、HandlerMapping(请求映射)、Controller(处理请求)、ViewResolver(视图解析)和ModelAndView(模型和视图容器)。开发流程涉及配置DispatcherServlet、定义Controller、创建View、处理数据、绑定模型和异常处理。
使用Java Web框架:Spring MVC的全面指南
|
1月前
|
敏捷开发 监控 前端开发
Spring+SpringMVC+Mybatis的分布式敏捷开发系统架构
Spring+SpringMVC+Mybatis的分布式敏捷开发系统架构
73 0