6.SN.Controllers
文件夹Config(Controllers.xml)
文件夹Controllers(TestController.cs)
Controllers.xml
1 <?xml version="1.0" encoding="utf-8" ?> 2 <objects xmlns="http://www.springframework.net"> 3 <object type="SN.Controllers.TestController,SN.Controllers" singleton="false"> 4 <property name="UserService" ref="UserServiceImpl"></property> 5 </object> 6 </objects>
TestController.cs
1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Text; 5 using System.Web .Mvc ; 6 using System.Web.Mvc.Ajax; 7 using SN.Servers; 8 9 namespace SN.Controllers 10 { 11 public class TestController:BaseController 12 { 13 private IUserService UserService { get; set; } 14 public ActionResult Index() 15 { 16 ViewBag.Text = "测试Mvc程序"; 17 ViewBag.Users = UserService.GetAllUsers(); 18 return View(); 19 } 20 } 21 }
7.SN.Web 这个主意介绍View web.config
文件夹View(Test(Index.cshtml))
Test=>Index.cshtml
1 @{ 2 ViewBag.Title = "Index"; 3 } 4 5 <h2>Index</h2> 6 7 <h3>@ViewBag.Text</h3> 8 @foreach (var item in ViewBag.Users) 9 { 10 <text>@item.Name +@item.Id</text> 11 }
Web.config
1 <?xml version="1.0" encoding="utf-8"?> 2 <!-- 3 For more information on how to configure your ASP.NET application, please visit 4 http://go.microsoft.com/fwlink/?LinkId=169433 5 --> 6 <configuration> 7 <configSections> 8 <sectionGroup name="spring"> 9 <section name="context" type="Spring.Context.Support.MvcContextHandler, Spring.Web.Mvc4" /> 10 <!--<section name="parsers" type="Spring.Context.Support.NamespaceParsersSectionHandler, Spring.Core"/> 11 --> 12 <section name="objects" type="Spring.Context.Support.DefaultSectionHandler, Spring.Core" /> 13 </sectionGroup> 14 <section name="databaseSettings" type="System.Configuration.NameValueSectionHandler" /> 15 </configSections> 16 <!--spring--> 17 <spring> 18 <!--<parsers> 19 <parser type="Spring.Data.Config.DatabaseNamespaceParser, Spring.Data"/> 20 <parser type="Spring.Transaction.Config.TxNamespaceParser, Spring.Data"/> 21 </parsers>--> 22 <context> 23 <resource uri="assembly://SN.Dao/SN.Dao.Config/SpringNHibernate.xml" /> 24 <resource uri="assembly://SN.Dao/SN.Dao.Config/Objects.xml" /> 25 <resource uri="assembly://SN.Servers/SN.Servers.Config/Servers.xml" /> 26 <resource uri="assembly://SN.Controllers/SN.Controllers.Config/Controllers.xml" /> 27 <!--<resource uri="file://~/Config/SpringNHibernate.xml"/>--> 28 </context> 29 </spring> 30 <!--数据库配置--> 31 <databaseSettings> 32 <add key="db.server" value="127.0.0.1" /> 33 <add key="db.database" value="MyTest" /> 34 <add key="db.user" value="sa" /> 35 <add key="db.password" value="sql" /> 36 </databaseSettings> 37 38 <appSettings> 39 <add key="webpages:Version" value="2.0.0.0" /> 40 <add key="webpages:Enabled" value="false" /> 41 <add key="PreserveLoginUrl" value="true" /> 42 <add key="ClientValidationEnabled" value="true" /> 43 <add key="UnobtrusiveJavaScriptEnabled" value="true" /> 44 <!---配置Spring.Net NHibernate SessionFactory 对象--> 45 <add key="Spring.Data.NHibernate.Support.OpenSessionInViewModule.SessionFactoryObjectName" value="NHibernateSessionFactory" /> 46 </appSettings> 47 <system.web> 48 <httpModules> 49 <!---spring net nhibernate sesion 自动加载必须设置次节点(本地测试需要配置否则有错误)--> 50 <add name="OpenSessionInView" type="Spring.Data.NHibernate.Support.OpenSessionInViewModule, Spring.Data.NHibernate4" /> 51 </httpModules> 52 <httpRuntime /> 53 <compilation debug="true" targetFramework="4.0" /> 54 <pages controlRenderingCompatibilityVersion="4.0"> 55 <namespaces> 56 <add namespace="System.Web.Helpers" /> 57 <add namespace="System.Web.Mvc" /> 58 <add namespace="System.Web.Mvc.Ajax" /> 59 <add namespace="System.Web.Mvc.Html" /> 60 <add namespace="System.Web.Routing" /> 61 <add namespace="System.Web.WebPages" /> 62 </namespaces> 63 </pages> 64 </system.web> 65 <system.webServer> 66 <validation validateIntegratedModeConfiguration="false" /> 67 <modules runAllManagedModulesForAllRequests="true"> 68 <!---spring net nhibernate sesion 自动加载必须设置次节点(ISS服务器必须配置)--> 69 <add name="OpenSessionInView" type="Spring.Data.NHibernate.Support.OpenSessionInViewModule, Spring.Data.NHibernate4" /> 70 </modules> 71 <handlers> 72 <remove name="ExtensionlessUrlHandler-ISAPI-4.0_32bit" /> 73 <remove name="ExtensionlessUrlHandler-ISAPI-4.0_64bit" /> 74 <remove name="ExtensionlessUrlHandler-Integrated-4.0" /> 75 <add name="ExtensionlessUrlHandler-ISAPI-4.0_32bit" path="*." verb="GET,HEAD,POST,DEBUG,PUT,DELETE,PATCH,OPTIONS" modules="IsapiModule" scriptProcessor="%windir%Microsoft.NETFrameworkv4.0.30319aspnet_isapi.dll" preCondition="classicMode,runtimeVersionv4.0,bitness32" responseBufferLimit="0" /> 76 <add name="ExtensionlessUrlHandler-ISAPI-4.0_64bit" path="*." verb="GET,HEAD,POST,DEBUG,PUT,DELETE,PATCH,OPTIONS" modules="IsapiModule" scriptProcessor="%windir%Microsoft.NETFramework64v4.0.30319aspnet_isapi.dll" preCondition="classicMode,runtimeVersionv4.0,bitness64" responseBufferLimit="0" /> 77 <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" /> 78 </handlers> 79 </system.webServer> 80 <runtime> 81 <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1"> 82 <dependentAssembly> 83 <assemblyIdentity name="Spring.Core" publicKeyToken="65e474d141e25e07" culture="neutral" /> 84 <bindingRedirect oldVersion="0.0.0.0-2.0.1.40000" newVersion="2.0.1.40000" /> 85 </dependentAssembly> 86 <dependentAssembly> 87 <assemblyIdentity name="Common.Logging" publicKeyToken="af08829b84f0328e" culture="neutral" /> 88 <bindingRedirect oldVersion="0.0.0.0-2.1.1.0" newVersion="2.1.1.0" /> 89 </dependentAssembly> 90 <dependentAssembly> 91 <assemblyIdentity name="System.Web.Mvc" publicKeyToken="31bf3856ad364e35" culture="neutral" /> 92 <bindingRedirect oldVersion="0.0.0.0-4.0.0.0" newVersion="4.0.0.0" /> 93 </dependentAssembly> 94 </assemblyBinding> 95 </runtime> 96 </configuration>
8.修改Global.asax
1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Web; 5 using System.Web.Http; 6 using System.Web.Mvc; 7 using System.Web.Routing; 8 9 namespace SN.Web 10 { 11 // Note: For instructions on enabling IIS6 or IIS7 classic mode, 12 // visit http://go.microsoft.com/?LinkId=9394801 13 public class MvcApplication : Spring.Web.Mvc.SpringMvcApplication 14 { 15 protected void Application_Start() 16 { 17 AreaRegistration.RegisterAllAreas(); 18 19 WebApiConfig.Register(GlobalConfiguration.Configuration); 20 FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters); 21 RouteConfig.RegisterRoutes(RouteTable.Routes); 22 } 23 } 24 }
9.问题解决方案:
1.设置cofing 中xml文件设置(A.始终复制 B.设置嵌入式)
2.引用Spring.Data.Nhibernate 和Spring相关组件版本要保持一致
3.注入问题仔细查看Spring注入机制
4.spring net nhibernate sesion 自动加载