Spring.Net应用(WEB)
最近项目中用到Spring.Net,所以研究了一下,以下是我对Spring.Net的IOC,页面注入,本地化的一点见解,
希望能给感兴趣的同行一点启发。
示例的源文件:Test.rar
一.创建类库,源代码如下:
二.创建Web Application,添加引用Spring.Core,Spring.Web,Spring.Ao. 工程如图所示:
![01.bmp](http://images.cnblogs.com/cnblogs_com/zhaohwi/20080830/01.bmp)
三. 添加页面和资源文件
(1).Site.master,Default.aspx,CustomerDetail.aspx
(2).添加全局资源文件(Global)
(3).添加本地资源文件(Local)
添加完成后,项目工程如下:
![02.bmp](http://images.cnblogs.com/cnblogs_com/zhaohwi/20080830/02.bmp)
四. 添加Spring.net支持
(1).修改Web.Config
五.运行效果
(1).打开Default.aspx页面
![03.bmp](http://images.cnblogs.com/cnblogs_com/zhaohwi/20080830/03.bmp)
(2).点击"English"后的效果
![04.bmp](http://images.cnblogs.com/cnblogs_com/zhaohwi/20080830/04.bmp)
(3).点击"客户名称"进入CustomerDetail.aspx页面
![05.bmp](http://images.cnblogs.com/cnblogs_com/zhaohwi/20080830/05.bmp)
(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
private void SetLanguage( object sender , CommandEventArgs e )
{
//this.UserCulture = new System.Globalization.CultureInfo( (string)( e.CommandArgument ) );
string cultureString = e.CommandArgument as string;
if ( cultureString != null )
{
//UserCulture是Spring.Net的Spring.Web.UI.MasterPage中的属性,
//用于处理本地化。
UserCulture = new CultureInfo( cultureString );
}
}
protected override void OnInit( EventArgs e )
{
InitComponent( );
base.OnInit( e );
}
private void InitComponent( )
{
//添加本地化的
this.lnkChinese.Command += new CommandEventHandler(SetLanguage);
this.lnkEnglish.Command += new CommandEventHandler(SetLanguage);
}
//Default.aspx.cs
OK,完了,Spring.Net的功能非常强大,有双向数据绑定,结果映射。对开发来是都是比较方便和使用的功能。
以上是我个人的一点研究,希望您看后有所收获。
希望能给感兴趣的同行一点启发。
示例的源文件:Test.rar
一.创建类库,源代码如下:
namespace Customer
{
public interface ICustomer
{
string ID { get;set;}
string Name { get;set;}
string PhoneNumber { get;set;}
}
}
//Customer.cs
namespace Customer
{
public class Customer : ICustomer
{
private string id = string.Empty;
private string name = string.Empty;
private string phoneNumber = string.Empty;
public Customer( )
{ }
public Customer( string id , string name , string phoneNumber )
{
this.id = id;
this.name = name;
this.phoneNumber = phoneNumber;
}
public Customer( string name , string phoneNumber )
{
this.id = Guid.NewGuid( ).ToString( );
this.name = name;
this.phoneNumber = phoneNumber;
}
public string ID
{
get { return id; }
set
{
if ( value == string.Empty )
ID = Guid.NewGuid( ).ToString();
else
id = value;
}
}
public string Name
{
get { return name; }
set
{
if ( value != string.Empty && value != null )
{
name = value;
}
}
}
public string PhoneNumber
{
get { return phoneNumber; }
set { phoneNumber = value; }
}
}
}
//ICustomerServices.cs
namespace Customer
{
public interface ICustomerService
{
List<ICustomer> GetAllCustomer( );
List<ICustomer> Customers { get;set;}
}
}
//CustomerServices.cs
namespace Customer
{
public class CustomerService : ICustomerService
{
private List<ICustomer> customers = null;
public CustomerService( )
{ }
public List<ICustomer> GetAllCustomer( )
{
if ( customers == null )
{
customers = new List<ICustomer>( );
}
customers.Add( new Customer( "tester0001" , "110" ) );
customers.Add( new Customer( "tester0002" , "112" ) );
customers.Add( new Customer( "tester0003" , "114" ) );
customers.Add( new Customer( "adminstrator" , "911" ) );
return customers;
}
public List<ICustomer> Customers
{
get { return customers; }
set { customers = value; }
}
}
}
{
public interface ICustomer
{
string ID { get;set;}
string Name { get;set;}
string PhoneNumber { get;set;}
}
}
//Customer.cs
namespace Customer
{
public class Customer : ICustomer
{
private string id = string.Empty;
private string name = string.Empty;
private string phoneNumber = string.Empty;
public Customer( )
{ }
public Customer( string id , string name , string phoneNumber )
{
this.id = id;
this.name = name;
this.phoneNumber = phoneNumber;
}
public Customer( string name , string phoneNumber )
{
this.id = Guid.NewGuid( ).ToString( );
this.name = name;
this.phoneNumber = phoneNumber;
}
public string ID
{
get { return id; }
set
{
if ( value == string.Empty )
ID = Guid.NewGuid( ).ToString();
else
id = value;
}
}
public string Name
{
get { return name; }
set
{
if ( value != string.Empty && value != null )
{
name = value;
}
}
}
public string PhoneNumber
{
get { return phoneNumber; }
set { phoneNumber = value; }
}
}
}
//ICustomerServices.cs
namespace Customer
{
public interface ICustomerService
{
List<ICustomer> GetAllCustomer( );
List<ICustomer> Customers { get;set;}
}
}
//CustomerServices.cs
namespace Customer
{
public class CustomerService : ICustomerService
{
private List<ICustomer> customers = null;
public CustomerService( )
{ }
public List<ICustomer> GetAllCustomer( )
{
if ( customers == null )
{
customers = new List<ICustomer>( );
}
customers.Add( new Customer( "tester0001" , "110" ) );
customers.Add( new Customer( "tester0002" , "112" ) );
customers.Add( new Customer( "tester0003" , "114" ) );
customers.Add( new Customer( "adminstrator" , "911" ) );
return customers;
}
public List<ICustomer> Customers
{
get { return customers; }
set { customers = value; }
}
}
}
二.创建Web Application,添加引用Spring.Core,Spring.Web,Spring.Ao. 工程如图所示:
![01.bmp](http://images.cnblogs.com/cnblogs_com/zhaohwi/20080830/01.bmp)
三. 添加页面和资源文件
(1).Site.master,Default.aspx,CustomerDetail.aspx
(2).添加全局资源文件(Global)
(3).添加本地资源文件(Local)
添加完成后,项目工程如下:
![02.bmp](http://images.cnblogs.com/cnblogs_com/zhaohwi/20080830/02.bmp)
四. 添加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<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>
<?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>
<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页面
![03.bmp](http://images.cnblogs.com/cnblogs_com/zhaohwi/20080830/03.bmp)
(2).点击"English"后的效果
![04.bmp](http://images.cnblogs.com/cnblogs_com/zhaohwi/20080830/04.bmp)
(3).点击"客户名称"进入CustomerDetail.aspx页面
![05.bmp](http://images.cnblogs.com/cnblogs_com/zhaohwi/20080830/05.bmp)
(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中都实现了该方法。
**作用是查找指定的资源,机制:首先在页面的本地资源中查找,如果没有知道会自动到全局资源文件中进行查找。
private void SetLanguage( object sender , CommandEventArgs e )
{
//this.UserCulture = new System.Globalization.CultureInfo( (string)( e.CommandArgument ) );
string cultureString = e.CommandArgument as string;
if ( cultureString != null )
{
//UserCulture是Spring.Net的Spring.Web.UI.MasterPage中的属性,
//用于处理本地化。
UserCulture = new CultureInfo( cultureString );
}
}
protected override void OnInit( EventArgs e )
{
InitComponent( );
base.OnInit( e );
}
private void InitComponent( )
{
//添加本地化的
this.lnkChinese.Command += new CommandEventHandler(SetLanguage);
this.lnkEnglish.Command += new CommandEventHandler(SetLanguage);
}
//Default.aspx.cs
public CustomerService CustomerService
{
set { services = value; }
}
**CustomerServices是Default.aspx页面暴露出来的属性,
**配置文件(Web.xml)中的对象的名称必须与该属性的名称相同。
{
set { services = value; }
}
**CustomerServices是Default.aspx页面暴露出来的属性,
**配置文件(Web.xml)中的对象的名称必须与该属性的名称相同。
OK,完了,Spring.Net的功能非常强大,有双向数据绑定,结果映射。对开发来是都是比较方便和使用的功能。
以上是我个人的一点研究,希望您看后有所收获。