Spring.Net2.0+NHibernate4.0 +Asp.Net Mvc4 二
2024-09-06 11:42:41
6.SN.Controllers
文件夹Config(Controllers.xml)
文件夹Controllers(TestController.cs)
Controllers.xml
<?xml version="1.0" encoding="utf-8" ?>
<objects xmlns="http://www.springframework.net">
<object type="SN.Controllers.TestController,SN.Controllers" singleton="false">
<property name="UserService" ref="UserServiceImpl"></property>
</object>
</objects>
TestController.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web .Mvc ;
using System.Web.Mvc.Ajax;
using SN.Servers; namespace SN.Controllers
{
public class TestController:BaseController
{
private IUserService UserService { get; set; }
public ActionResult Index()
{
ViewBag.Text = "测试Mvc程序";
ViewBag.Users = UserService.GetAllUsers();
return View();
}
}
}
7.SN.Web 这个主意介绍View web.config
文件夹View(Test(Index.cshtml))
Test=>Index.cshtml
@{
ViewBag.Title = "Index";
} <h2>Index</h2> <h3>@ViewBag.Text</h3>
@foreach (var item in ViewBag.Users)
{
<text>@item.Name +@item.Id</text>
}
Web.config
<?xml version="1.0" encoding="utf-8"?>
<!--
For more information on how to configure your ASP.NET application, please visit
http://go.microsoft.com/fwlink/?LinkId=169433
-->
<configuration>
<configSections>
<sectionGroup name="spring">
<section name="context" type="Spring.Context.Support.MvcContextHandler, Spring.Web.Mvc4" />
<!--<section name="parsers" type="Spring.Context.Support.NamespaceParsersSectionHandler, Spring.Core"/>
-->
<section name="objects" type="Spring.Context.Support.DefaultSectionHandler, Spring.Core" />
</sectionGroup>
<section name="databaseSettings" type="System.Configuration.NameValueSectionHandler" />
</configSections>
<!--spring-->
<spring>
<!--<parsers>
<parser type="Spring.Data.Config.DatabaseNamespaceParser, Spring.Data"/>
<parser type="Spring.Transaction.Config.TxNamespaceParser, Spring.Data"/>
</parsers>-->
<context>
<resource uri="assembly://SN.Dao/SN.Dao.Config/SpringNHibernate.xml" />
<resource uri="assembly://SN.Dao/SN.Dao.Config/Objects.xml" />
<resource uri="assembly://SN.Servers/SN.Servers.Config/Servers.xml" />
<resource uri="assembly://SN.Controllers/SN.Controllers.Config/Controllers.xml" />
<!--<resource uri="file://~/Config/SpringNHibernate.xml"/>-->
</context>
</spring>
<!--数据库配置-->
<databaseSettings>
<add key="db.server" value="127.0.0.1" />
<add key="db.database" value="MyTest" />
<add key="db.user" value="sa" />
<add key="db.password" value="sql" />
</databaseSettings> <appSettings>
<add key="webpages:Version" value="2.0.0.0" />
<add key="webpages:Enabled" value="false" />
<add key="PreserveLoginUrl" value="true" />
<add key="ClientValidationEnabled" value="true" />
<add key="UnobtrusiveJavaScriptEnabled" value="true" />
<!---配置Spring.Net NHibernate SessionFactory 对象-->
<add key="Spring.Data.NHibernate.Support.OpenSessionInViewModule.SessionFactoryObjectName" value="NHibernateSessionFactory" />
</appSettings>
<system.web>
<httpModules>
<!---spring net nhibernate sesion 自动加载必须设置次节点(本地测试需要配置否则有错误)-->
<add name="OpenSessionInView" type="Spring.Data.NHibernate.Support.OpenSessionInViewModule, Spring.Data.NHibernate4" />
</httpModules>
<httpRuntime />
<compilation debug="true" targetFramework="4.0" />
<pages controlRenderingCompatibilityVersion="4.0">
<namespaces>
<add namespace="System.Web.Helpers" />
<add namespace="System.Web.Mvc" />
<add namespace="System.Web.Mvc.Ajax" />
<add namespace="System.Web.Mvc.Html" />
<add namespace="System.Web.Routing" />
<add namespace="System.Web.WebPages" />
</namespaces>
</pages>
</system.web>
<system.webServer>
<validation validateIntegratedModeConfiguration="false" />
<modules runAllManagedModulesForAllRequests="true">
<!---spring net nhibernate sesion 自动加载必须设置次节点(ISS服务器必须配置)-->
<add name="OpenSessionInView" type="Spring.Data.NHibernate.Support.OpenSessionInViewModule, Spring.Data.NHibernate4" />
</modules>
<handlers>
<remove name="ExtensionlessUrlHandler-ISAPI-4.0_32bit" />
<remove name="ExtensionlessUrlHandler-ISAPI-4.0_64bit" />
<remove name="ExtensionlessUrlHandler-Integrated-4.0" />
<add name="ExtensionlessUrlHandler-ISAPI-4.0_32bit" path="*." verb="GET,HEAD,POST,DEBUG,PUT,DELETE,PATCH,OPTIONS" modules="IsapiModule" scriptProcessor="%windir%\Microsoft.NET\Framework\v4.0.30319\aspnet_isapi.dll" preCondition="classicMode,runtimeVersionv4.0,bitness32" responseBufferLimit="" />
<add name="ExtensionlessUrlHandler-ISAPI-4.0_64bit" path="*." verb="GET,HEAD,POST,DEBUG,PUT,DELETE,PATCH,OPTIONS" modules="IsapiModule" scriptProcessor="%windir%\Microsoft.NET\Framework64\v4.0.30319\aspnet_isapi.dll" preCondition="classicMode,runtimeVersionv4.0,bitness64" responseBufferLimit="" />
<add name="ExtensionlessUrlHandler-Integrated-4.0" path="*." verb="GET,HEAD,POST,DEBUG,PUT,DELETE,PATCH,OPTIONS" type="System.Web.Handlers.TransferRequestHandler" preCondition="integratedMode,runtimeVersionv4.0" />
</handlers>
</system.webServer>
<runtime>
<assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
<dependentAssembly>
<assemblyIdentity name="Spring.Core" publicKeyToken="65e474d141e25e07" culture="neutral" />
<bindingRedirect oldVersion="0.0.0.0-2.0.1.40000" newVersion="2.0.1.40000" />
</dependentAssembly>
<dependentAssembly>
<assemblyIdentity name="Common.Logging" publicKeyToken="af08829b84f0328e" culture="neutral" />
<bindingRedirect oldVersion="0.0.0.0-2.1.1.0" newVersion="2.1.1.0" />
</dependentAssembly>
<dependentAssembly>
<assemblyIdentity name="System.Web.Mvc" publicKeyToken="31bf3856ad364e35" culture="neutral" />
<bindingRedirect oldVersion="0.0.0.0-4.0.0.0" newVersion="4.0.0.0" />
</dependentAssembly>
</assemblyBinding>
</runtime>
</configuration>
8.修改Global.asax
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Http;
using System.Web.Mvc;
using System.Web.Routing; namespace SN.Web
{
// Note: For instructions on enabling IIS6 or IIS7 classic mode,
// visit http://go.microsoft.com/?LinkId=9394801
public class MvcApplication : Spring.Web.Mvc.SpringMvcApplication
{
protected void Application_Start()
{
AreaRegistration.RegisterAllAreas(); WebApiConfig.Register(GlobalConfiguration.Configuration);
FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
RouteConfig.RegisterRoutes(RouteTable.Routes);
}
}
}
9.问题解决方案:
1.设置cofing 中xml文件设置(A.始终复制 B.设置嵌入式)
2.引用Spring.Data.Nhibernate 和Spring相关组件版本要保持一致
3.注入问题仔细查看Spring注入机制
4.spring net nhibernate sesion 自动加载
最新文章
- NGUI创建Camera参数为Simple 2D的UI UI对象的结构UI Root(2D)
- Python3基础 逻辑与 and
- 对称加密DES和TripleDES
- javascript 柯里化
- MSMQ(消息队列)续
- skynet启动过程_bootstrap
- js如何判断一个对象是不是Array?(转载)
- java_Eclipse中SVN的安装步骤(两种)和使用方法
- C#编程实践—EventBroker简单实现
- HDFS- High Availability
- 迅为-ARM嵌入式开发一体化工业9.7寸屏幕 平板式智能触控屏
- python 数据类型元组与字典内置方法
- odoo10 入门
- java编写的Http协议的多线程下载器
- jquery 根据自定义属性选择
- LeetCode OJ 22. Generate Parentheses
- vue首屏加载优化
- linq(查询)
- Android使用Gradle命令动态传参完成打包,不需要修改代码
- JAVA IO操作:数据操作流:DataOutputStream和DataInputStream