zoukankan      html  css  js  c++  java
  • Spring.Web.Mvc 注入(控制器属性注入)

    1.web.config配置

    <?xml version="1.0" encoding="utf-8"?>
    <!--
    有关如何配置 ASP.NET 应用程序的详细信息,请访问
    http://go.microsoft.com/fwlink/?LinkId=301880
    -->
    <configuration>
    <configSections>
    <sectionGroup name="spring">
    <section name="context" type="Spring.Context.Support.MvcContextHandler, Spring.Web.Mvc"/>
    </sectionGroup>
    </configSections>

    <connectionStrings>
    <add name="ApplicationServices"
    connectionString="data source=.SQLEXPRESS;Integrated Security=SSPI;AttachDBFilename=|DataDirectory|aspnetdb.mdf;User Instance=true"
    providerName="System.Data.SqlClient" />
    </connectionStrings>

    <spring>
    <context>
    <resource uri="~/Config/Controllers.xml"/>
    </context>
    </spring>
    <appSettings>
    <add key="webpages:Version" value="3.0.0.0"/>
    <add key="webpages:Enabled" value="false"/>
    <add key="ClientValidationEnabled" value="true"/>
    <add key="UnobtrusiveJavaScriptEnabled" value="true"/>
    </appSettings>
    <system.web>
    <compilation debug="true" targetFramework="4.5.2"/>
    <httpRuntime targetFramework="4.5.2"/>
    </system.web>
    <runtime>
    <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
    <dependentAssembly>
    <assemblyIdentity name="System.Web.Helpers" publicKeyToken="31bf3856ad364e35"/>
    <bindingRedirect oldVersion="1.0.0.0-3.0.0.0" newVersion="3.0.0.0"/>
    </dependentAssembly>
    <dependentAssembly>
    <assemblyIdentity name="System.Web.WebPages" publicKeyToken="31bf3856ad364e35"/>
    <bindingRedirect oldVersion="1.0.0.0-3.0.0.0" newVersion="3.0.0.0"/>
    </dependentAssembly>
    <dependentAssembly>
    <assemblyIdentity name="System.Web.Mvc" publicKeyToken="31bf3856ad364e35"/>
    <bindingRedirect oldVersion="1.0.0.0-5.2.3.0" newVersion="5.2.3.0"/>
    </dependentAssembly>
    </assemblyBinding>
    </runtime>
    <system.codedom>
    <compilers>
    <compiler language="c#;cs;csharp" extension=".cs"
    type="Microsoft.CodeDom.Providers.DotNetCompilerPlatform.CSharpCodeProvider, Microsoft.CodeDom.Providers.DotNetCompilerPlatform, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
    warningLevel="4" compilerOptions="/langversion:6 /nowarn:1659;1699;1701"/>
    <compiler language="vb;vbs;visualbasic;vbscript" extension=".vb"
    type="Microsoft.CodeDom.Providers.DotNetCompilerPlatform.VBCodeProvider, Microsoft.CodeDom.Providers.DotNetCompilerPlatform, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
    warningLevel="4" compilerOptions="/langversion:14 /nowarn:41008 /define:_MYTYPE=&quot;Web&quot; /optionInfer+"/>
    </compilers>
    </system.codedom>
    </configuration>

    2.根目录添加lib文件夹(spring.core spring.web.mvc Common.Logging.dll Common.Logging.Log4Net.dll)

    程序集引用添加这4个东西

    3.mvcapplication 注册路由重写 继承SpringMvcApplication

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using System.Web.Mvc;
    using System.Web.Routing;
    using Spring.Web.Mvc;

    namespace WebApplication12
    {
    public class MvcApplication :SpringMvcApplication
    {
    protected override void RegisterRoutes(RouteCollection routes)
    {
    routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

    routes.MapRoute(
    name: "Default",
    url: "{controller}/{action}/{id}",
    defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
    );
    }
    //protected void Application_Start()
    //{
    // AreaRegistration.RegisterAllAreas();
    // RouteConfig.RegisterRoutes(RouteTable.Routes);
    //}
    }
    }

    3.根目录下新建Config文件夹 里面添加Controllers.xml(右键属性 始终复制 我的迷信)

    <?xml version="1.0" encoding="utf-8" ?>
    <objects xmlns="http://www.springframework.net">

    <object type="WebApplication12.Controllers.HomeController, WebApplication12" singleton="false" >
    <property name="Message1" value="海贼王的乱入" />
    </object>
      <!--集合注入-->

    <object type="WebApplication12.Controllers.HomeController, WebApplication12" singleton="false" >
    <property name="Message" >
    <list element-type="System.String">
    <value>B0 Stage</value>
    <value>B1 Stage</value>
    <value>B2 Stage</value>
    <value>B3 Stage</value>
    <value>B4 Stage</value>
    <value>B5 Stage</value>
    <value>B6 Stage</value>
    </list>
    </property>
    </object>

    </objects>

    4.控制器添加Message属性之后用于注入

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using System.Web.Mvc;

    namespace WebApplication12.Controllers
    {
    public class HomeController : Controller
    {
    public string Message { get; set; }
    // GET: Home
    public ActionResult Index()
    {
    ViewData["Message"] = Message;
    return View();
    }
    }
    }

    4.视图调用


    @{
    Layout = null;
    }

    <!DOCTYPE html>

    <html>
    <head>
    <meta name="viewport" content="width=device-width" />
    <title>Index</title>
    </head>
    <body>
    <div>
    <h1>@ViewData["Message"]</h1>
    </div>
    </body>
    </html>

  • 相关阅读:
    青春小女生 科研大梦想——微软亚洲研究院女实习生的故事
    刷新神经网络新深度:ImageNet计算机视觉挑战赛微软中国研究员夺冠
    优质博士的养成之道——对话2015微软学者奖学金获得者
    洪小文谈科研:自信与谦虚并行
    Java 8 新特性
    Java 实例
    Java 文档注释
    Java Applet 基础
    Java 多线程编程
    Java 发送邮件
  • 原文地址:https://www.cnblogs.com/kexb/p/4823236.html
Copyright © 2011-2022 走看看