Directory
Spring. NET + nhib.pdf + ASP. MVC + easyui practices (1)
Spring. NET + nhib.pdf + ASP. MVC + easyui practices (2)
Spring. NET + nhib.pdf + ASP. MVC + easyui Practices (3)
Spring. NET + nhib.pdf + ASP. MVC + easyui Practices (4)
In the previous blog, you wrote the basic code for this project. This chapter will write the code for the mvc4.0 project:
1. First add spring.net to inject MVC
(1) Add reference spring. Web. mvc3.dll (spring. Web. mvc4 can also be used, mainly because mvc4 has not been updated in nuget), nuget automatically installs its dependent object
(2) Add an attribute to homecontroll
public class HomeController : Controller { public string Message { get; set; } public ActionResult Index() { ViewBag.Message = this.Message; return View(); }……
(2) Add the folder configs, add the file controllers. XML, and modify the attribute to an embedded resource.
<? XML version = "1.0" encoding = "UTF-8"?> <Objects xmlns = "http://www.springframework.net"> <object type = "financewebsys. controllers. homecontroller, financewebsys "Singleton =" false "> <property name =" message "value =" Spring. net 1.3.2 ASP. net MVC dependency injection "/> </Object> </objects>
(3) modify global. asax to make mvcapplication: Inherit springmvcapplication
public class MvcApplication : SpringMvcApplication { protected void Application_Start() { AreaRegistration.RegisterAllAreas(); WebApiConfig.Register(GlobalConfiguration.Configuration); FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters); RouteConfig.RegisterRoutes(RouteTable.Routes); BundleConfig.RegisterBundles(BundleTable.Bundles); AuthConfig.RegisterAuth(); } }
(4) Configure web. config
<configuration> <configSections> …… <sectionGroup name="spring"> <section name="context" type="Spring.Context.Support.MvcContextHandler, Spring.Web.Mvc3"/> </sectionGroup> </configSections><spring> <context> <resource uri="file://~/Configs/Controllers.xml"/> </context> </spring>……</configuration>
(5) Effect
2. spring injection to Nhibernate: Add reference model, ibll, BLL, nhibernatedao, and npgsql based on the above. DLL (note that npgsql cannot be installed with nuget because of spring. data. nhibernate32 supports npgsql2.0.0.0 at most)
(1) Add repository. xml and manager. xml in the configs folder
<? XML version = "1.0" encoding = "UTF-8"?> <Objects xmlns = "http://www.springframework.net" xmlns: DB = "http://www.springframework.net/database"> <! -- Used to configure data access in other applications --> <object type = "Spring. objects. factory. config. propertyplaceholderconfigurer, spring. core "> <property name =" configsections "value =" databasesettings "/> </Object> <! -- Database and nhib.pdf configuration --> <DB: provider id = "dbprovider" provider = "$ {provider}" connectionstring = "$ {connectionstring}"/> <! -- Sessionfactory object, which includes some important attributes --> <Object ID = "nhibernatesessionfactory" type = "Spring. data. nhib.pdf. localsessionfactoryobject, spring. data. nhibernate32 "> <property name =" dbprovider "ref =" dbprovider "/> <property name =" mappingassemblies "> <list> <! -- This is XX corresponding to XX class for configuring embedded resources. HBM. project name of the XML file --> <value> financewebsys. model </value> </List> </property> <property name = "hibernateproperties"> <dictionary> <Entry key = "hibernate. connection. provider "value =" nhib.pdf. connection. driverconnectionprovider "/> <! -- Database Dialect --> <Entry key = "dialect" value = "$ {dialect}"/> <! -- Database driver --> <Entry key = "hibernate. connection. driver_class "value =" $ {driver_class} "/> </dictionary> </property> </Object> <Object ID =" hibernatetemplate "type =" Spring. data. nhib.pdf. generic. hibernatetemplate "> <property name =" sessionfactory "ref =" nhibernatesessionfactory "/> <property name =" templateflushmode "value =" Auto "/> <property name =" cachequeries "value = ""True"/> </Object> <Object ID = "userdepinforepository" type = "financewebsys. nhibernatedao. userdepinforepository, financewebsys. nhibernatedao "> <property name =" hibernatetemplate "ref =" hibernatetemplate "/> </Object> </objects>
<? XML version = "1.0" encoding = "UTF-8"?> <Objects xmlns = "http://www.springframework.net"> <Object ID = "transactionmanager" type = "Spring. data. nhib.pdf. hibernatetransactionmanager, spring. data. nhibernate32 "> <property name =" dbprovider "ref =" dbprovider "/> <property name =" sessionfactory "ref =" nhibernatesessionfactory "/> </Object> <Object ID =" transactioninterceptor "type =" Spring. transaction. interceptor. transactioninterceptor, spring. data "> <p Roperty name = "transactionmanager" ref = "transactionmanager"/> <property name = "transactionattributesource"> <object type = "Spring. transaction. interceptor. attributestransactionattributesource, spring. data "/> </property> </Object> <! -- Transaction Manager --> <Object ID = "basetransactionmanager" type = "Spring. transaction. interceptor. transactionproxyfactoryobject, spring. data "abstract =" true "> <property name =" platformtransactionmanager "ref =" transactionmanager "/> <property name =" transactionattributes "> <name-values> <! -- Add --> <add key = "Save *" value = "propagation_required"/> <! -- Modify --> <add key = "Update *" value = "propagation_required"/> <! -- Delete --> <add key = "delete *" value = "propagation_required"/> <! -- Get --> <add key = "get *" value = "propagation_required"/> <! -- Browse --> <add key = "find *" value = "propagation_supports, readonly"/> <! -- Search --> <add key = "Search *" value = "propagation_supports, readonly"/> <! -- Report --> <add key = "query *" value = "propagation_supports, readonly"/> <! -- Load --> <add key = "load *" value = "propagation_supports, readonly"/> <! -- Report --> <add key = "report *" value = "propagation_supports, readonly"/> <! -- Others --> <add key = "*" value = "propagation_required"/> </name-values> </property> </Object> <Object ID = "userdepinfomanager" parent = "basetransactionmanager"> <property name = "target"> <object type = "financewebsys. bll. userdepinfomanager, financewebsys. bll "> <property name =" currentrepository "ref =" userdepinforepository "/> </Object> </property> </Object> </objects>
(2) modify web. config
<Configsections> ...... <! -- Spring injection to MVC --> <sectiongroup name = "Spring"> <section name = "context" type = "Spring. context. support. mvccontexthandler, spring. web. mvc3 "/> </sectiongroup> <! -- Database settings --> <section name = "databasesettings" type = "system. configuration. namevaluesectionhandler"/> ...... </Configsections> <! -- Spring configuration file --> <spring> <context> <! -- Web --> <resource uri = "file ://~ /Configs/controllers. xml "/> <! -- BLL --> <resource uri = "file ://~ /Configs/manager. xml "/> <! -- Dao --> <resource uri = "file ://~ /Configs/Repository. xml "/> </context> </spring> <! -- Database connection string --> <databasesettings> <add key = "provider" value = "Npgsql-2.0"/> <add key = "connectionstring" value = "Server = 127.0.0.1; port = 5432; user id = Postgres; Password = zllzh; database = financewebdb; "/> <add key =" dialect "value =" nhib.pdf. dialect. postgresqldialect "/> <add key =" driver_class "value =" nhib.pdf. driver. npgsqldriver "/> </databasesettings>
Two other functions are not clear for the time being.
<Deleetask> <! -- Spring added --> <add key = "Spring. Data. nhib.pdf. Support. opensessioninviewmodule. sessionfactoryobjectname" value = "nhibernatesessionfactory"/> </appsettings> <system. Web> <! -- Add -->
(3) Modify homecontroller
public class HomeController : Controller { public string Message { get; set; } public IUserDepInfoManager UserDepInfoManager { get; set; } public ActionResult Index() { var a = UserDepInfoManager.Get(1); ViewBag.Message = a.DepName + a.Id; //ViewBag.Message = this.Message; return View(); }
(4) Modify controllers. xml
<? XML version = "1.0" encoding = "UTF-8"?> <Objects xmlns = "http://www.springframework.net"> <object type = "financewebsys. controllers. homecontroller, financewebsys "Singleton =" false "> <property name =" message "value =" Spring. net 1.3.2 ASP. net MVC dependency injection "/> <property name =" userdepinfomanager "ref =" userdepinfomanager "/> </Object> </objects>
3. Finally, let's look at the effect.
Insert two data entries in the database at will:
Run: succeeded
Supplement: Automatic table creation and log4net Configuration
1. Automatic table Creation
(1) modify web. config
<! -- Database connection string --> <databasesettings> ...... <! -- Automatically create a table --> <add key = "hbm2ddl. Auto" value = "Update"/> ...... </Databasesettings>
(2) Modify repository. xml and add automatic table creation <Entry key = "hbm2ddl. Auto" value = "$ {hbm2ddl. Auto}"/>
<Dictionary> ...... <! -- Automatic table creation (reverse ing) --> <Entry key = "hbm2ddl. Auto" value = "$ {hbm2ddl. Auto}"/> </dictionary>
2. Configure log4net
Modify web. confg. The next article provides the full web. config