最近项目中用到Spring.Net,所以研究了一下,以下是我对Spring.Net的IOC,页面注入,本地化的一点见解,
希望能给感兴趣的同行一点启发。

示例的源文件:
Test.rar

一.创建类库,源代码如下:
 value; }
        }
    }
}


二.创建Web Application,添加引用Spring.Core,Spring.Web,Spring.Ao. 工程如图所示:


Spring.Net应用(WEB)

三. 添加页面和资源文件
(1).Site.master,Default.aspx,CustomerDetail.aspx
(2).添加全局资源文件(Global)
(3).添加本地资源文件(Local)

添加完成后,项目工程如下:
Spring.Net应用(WEB)

四. 添加Spring.net支持
(1).修改Web.Config
<?xml version="1.0"?>

<
configuration>
<!--添加Spring.net配置节点-->
    
<configSections>
      
<sectionGroup name="spring">
        
<section name="context" type="Spring.Context.Support.WebContextHandler, Spring.Web"/>
        
<section name="objects" type="Spring.Context.Support.DefaultSectionHandler, Spring.Core"/>
      
</sectionGroup>
    
</configSections>
  
    <
appSettings/>
    <
connectionStrings
/>

    
<spring>
    <!--添加Spring所使用的配置文件-->
      
<context>

        
<resource uri="~/Config/Web.xml"/>
      
</context>
    
</spring>
    <system.web>
    <!--添加页面的程序集引用(@Page指令)-->
      
<pages>
        
<controls>
          
<add tagPrefix="spring" namespace="Spring.Web.UI.Controls" assembly="Spring.Web"/>
        
</controls>
      
</pages>
        <!-- 
            Set compilation debug
="true" to insert debugging 
            symbols into the compiled page. Because this 
            affects performance, 
set this
 value to true only 
            during development.
        
-->
        <
compilation debug="true" 
/>
        
<!--

            The <
authentication> section enables configuration 
            of the security authentication mode used by 
            ASP.NET to identify an incoming user. 
        
-->
        <
authentication mode="Windows" 
/>
        
<!--

            The <
customErrors> section enables configuration 
            of what to 
do if
/when an unhandled error occurs 
            during the execution of a request. Specifically, 
            it enables developers to configure html error pages 
            to be displayed 
in place of a error stack trace.

        
<customErrors mode="RemoteOnly" defaultRedirect="GenericErrorPage.htm"
>
            <
error statusCode="403" redirect="NoAccess.htm" 
/>
            <
error statusCode="404" redirect="FileNotFound.htm" 
/>
        </
customErrors
>
        
-->

      <!--
Spring Web-->
      <!--添加Spring的页面处理器和模块(对于使用页面注入非常重要)-->
      
<httpHandlers>

        
<add verb="*" path="*.aspx" type="Spring.Web.Support.PageHandlerFactory, Spring.Web"/>
      
</httpHandlers>
      
<httpModules>
        
<add name="Spring" type="Spring.Context.Support.WebSupportModule, Spring.Web"/>
      
</httpModules>
    </
system.web>
</
configuration
>

(2).添加Spring.net配置文件Web.xml

<?xml version="1.0" encoding="utf-8" ?>
<
objects xmlns="http://www.springframework.net
">
  <!--进行本地化的配置-->
  
<object id="cultureResolver" type="Spring.Globalization.Resolvers.SessionCultureResolver, Spring.Web" />

  
<object id="localizer" type="Spring.Globalization.Localizers.ResourceSetLocalizer, Spring.Core" />
  <!--添加对资源文件访问的配置-->
  
<object id="messageSource" type="Spring.Context.Support.ResourceSetMessageSource, Spring.Core">
    
<property name="ResourceManagers">
      
<list>
        <!--全局资源文件的位置,其中Resources.Strings中的Strings为全局资源文件的程序集名-->
        
<value>Resources.Strings, App_GlobalResources</value>

      
</list>
    
</property>
    
<property name="UseCodeAsDefaultMessage" value="true" />
  
</object>

  <!--
UserInfoMaintenance-->
  <!--
      配置Site.Master页面为抽象类型,
      其中的MasterPageFile,Localizer,CultureResolver为Spring.net的保留名称,不可更改。
  -->
  
<object id="BasePage" abstract="true">

    
<property name="MasterPageFile" value="~\Site.Master"/>
    
<property name="Localizer" ref="Localizer"/>
    
<property name="CultureResolver" ref ="cultureResolver"/>
    
<property name="ScriptsRoot" value="~/Scripts"/>
    
<property name="ImagesRoot" value="~/Images"/>
    
<property name="CssRoot" value="~/Css"/>
    
<property name="Title" value="My Core"/>
  
</object>

  
<object type="Default.aspx" parent="BasePage">
    
<!-- inject other objects that page needs -->
    <!--
       利用属性注入的方式,向页面Default.aspx页面中注入CustomerService对象
       其中的name="CustomerService"中的CustomerService是Default.aspx页面暴露的属性。
    -->
    
<property name="CustomerService" ref="customerService"/>

  
</object>

  
<object type="CustomerDetail.aspx" parent="BasePage">
    
<!-- inject other objects that page needs -->
  
</object>

  
<object id="customerService" type="Customer.CustomerService, Customer"></object>
</
objects>

五.运行效果
(1).打开Default.aspx页面
Spring.Net应用(WEB)

(2).点击"English"后的效果
Spring.Net应用(WEB)

(3).点击"客户名称"进入CustomerDetail.aspx页面
Spring.Net应用(WEB)

(4).代码简要说明

//Site.Master

<head runat="server">
<
spring:Head ID="Head2" runat="server"
>
    <
title
>
        <
spring:ContentPlaceHolder id="title" runat="server"
>
            <%
=GetMessage("DefaultTitle")
%>
        </
spring:ContentPlaceHolder
>
    </
title
>
    <
link href="<%= Page.CssRoot %>/Css.css" rel="stylesheet" type="text/css" 
/>
    <
spring:ContentPlaceHolder id="head" runat="server"></spring:ContentPlaceHolder
>
  </
spring:Head
>
</
head
>

**Page.CssRoot 是Spring.Net配置文件Web.xml中的BasePage对象的属性。

<tr>
   
<td>
         <
asp:LinkButton ID="lnkChinese" runat="server" CommandArgument="zh-CN"
>
             <%=GetMessage("LanguageChinese")
%>
         
</asp:LinkButton
>
   </
td>

    
<td>
         <
asp:LinkButton ID="lnkEnglish" runat="server" CommandArgument="en-US"
>
            <%=GetMessage("LanguageEnglish")%></asp:LinkButton
>
   </
td>
  
</tr
>

**GetMessage为Spring.Net中的方法,在Spring.Web.UI.MasterPage和Spring.Web.UI.Page中都实现了该方法。
**作用是查找指定的资源,机制:首先在页面的本地资源中查找,如果没有知道会自动到全局资源文件中进行查找。


//Site.Master.cs
 CommandEventHandler(SetLanguage);
        }

//Default.aspx.cs
 value; }
}

**CustomerServices是Default.aspx页面暴露出来的属性,
**配置文件(Web.xml)中的对象的名称必须与该属性的名称相同。

OK,完了,Spring.Net的功能非常强大,有双向数据绑定,结果映射。对开发来是都是比较方便和使用的功能。
以上是我个人的一点研究,希望您看后有所收获。

相关文章:

  • 2022-12-23
  • 2022-12-23
  • 2021-06-04
  • 2021-09-13
  • 2021-07-26
  • 2021-09-15
猜你喜欢
  • 2021-12-18
  • 2022-01-07
  • 2021-06-02
  • 2022-01-15
  • 2021-05-21
相关资源
相似解决方案