2009-05-29 35 views
0

我開始一個新的工作項目,我決定給MVC一個鏡頭。這是通勤挑戰的小型內部網站。使用ASP.NET MVC的Spring.NET驗證框架設置

我想使用Spring.NET進行驗證。我之前在Web Forms中使用過Spring.NET,但在傳統的ASP.NET中沒有代碼,我該如何使用Spring.NET提供的頁面驗證框架?

編輯1:

在嘗試這種嘗試我,這裏是我有:

的Web.Config

<?xml version="1.0"?> 
<configuration> 
    <configSections> 
     <sectionGroup name="spring"> 
      <section name="context" type="Spring.Context.Support.WebContextHandler, Spring.Web" /> 
      <section name="objects" type="Spring.Context.Support.DefaultSectionHandler, Spring.Core"/> 
      <section name="parsers" type="Spring.Context.Support.NamespaceParsersSectionHandler, Spring.Core"/> 
     </sectionGroup> 
    </configSections> 
    <appSettings> 
     <add key="RouteValidator" value="RouteValidator"/> 
     <add key="UserValidator" value="UserValidator"/>   
    </appSettings> 
    <spring> 
     <context> 
      <resource uri="config://spring/objects"/> 
      <resource uri="~/Config/Spring.Web.cfg.xml" /> 
      <resource uri="~/Config/Spring.Validation.cfg.xml" /> 
     </context> 
     <parsers> 
      <parser type="Spring.Validation.Config.ValidationNamespaceParser, Spring.Core" /> 
     </parsers> 
    </spring> 
    <system.web> 
      <httpModules> 
       <add name="Spring" type="Spring.Context.Support.WebSupportModule, Spring.Web" /> 
      </httpModules> 
    </system.web> 
</configuration> 

Spring.Web.Cfg.xml

<?xml version="1.0" encoding="utf-8" ?> 
<objects xmlns="http://www.springframework.net" 
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
       xsi:schemaLocation="http://www.springframework.net http://www.springframework.net/xsd/spring-objects.xsd"> 

    <description> 
     Foo MVC Controller declarations. 
    </description> 

    <object id="HomeController" type="Foo.MVC.Web.Controllers.HomeController, Foo.MVC.Web"></object> 
    <object id="AccountController" type="Foo.MVC.Web.Controllers.RouteController, Foo.MVC.Web"></object> 
    <object id="RouteController" type="Foo.MVC.Web.Controllers.RouteController, Foo.MVC.Web"></object> 

    <object id="Spring.Web.UI.Controls.ValidationError" abstract="true"> 
     <property name="Renderer"> 
      <object type="Spring.Web.UI.Validation.IconValidationErrorsRenderer, Spring.Web"> 
       <property name="IconSrc" value="validation-error.gif"/> 
      </object> 
     </property> 
    </object> 

    <object id="Spring.Web.UI.Controls.ValidationSummary" abstract="true"> 
     <property name="Renderer"> 
      <object type="Spring.Web.UI.Validation.DivValidationErrorsRenderer, Spring.Web"> 
       <property name="CssClass" value="validationError"/> 
      </object> 
     </property> 
    </object> 

    <object id="standardPage" abstract="true"> 
     <property name="MasterPageFile" value="~/Views/Shared/Site.master"/> 
     <property name="CssRoot" value="~/Content/"/> 
     <property name="ImagesRoot" value="~/Content"/> 
    </object> 
</objects> 

我的驗證文件非常標準,基本上來自另一個項目的複製和粘貼,因此我沒有包括e it。

現在我遇到的問題是如何使用它?我如何獲得應用程序上下文?我的網站形成項目用戶Spring.Web.UI.Page,但我很擔心,因爲MVC中的默認頁面從System.Web.Mvc.ViewPage派生,所以這是行不通的。

或者我只是不能夠使用Spring.NET的MVC框架呢?

謝謝!

感謝您的任何幫助。

回答

0

你絕對可以在ASP.Net MVC中使用Spring。您需要註冊您在Global.ascx類中使用它,然後框架將根據您在配置文件中定義的內容創建控制器。

public class MvcApplication : System.Web.HttpApplication 
{ 

    ...Routes stuff... 

    protected void Application_Start() 
    { 
     ControllerBuilder.Current.SetControllerFactory(typeof(ControllerFactory)); 

     RegisterRoutes(RouteTable.Routes);  
    } 
} 
public class ControllerFactory : IControllerFactory 
{ 
    public IController CreateController(RequestContext requestContext, string controllerName) 
    { 
     return IoC.Resolve<IController>(controllerName); 
    } 

    public void ReleaseController(IController controller) 
    { 
     //This is a sample implementation 
     //If pooling is used write code to return the object to pool 
     if (controller is IDisposable) 
     { 
      (controller as IDisposable).Dispose(); 

     } 
     controller = null; 
    } 
} 
public static class IoC 
{ 
    static readonly IObjectFactory Factory 
     = new XmlObjectFactory(new FileSystemResource 
      (HttpContext.Current.Server.MapPath("~/Config/Spring.config"))); 

    public static T Resolve<T>(string name) 
    { 
     return (T)Factory.GetObject(name); 
    } 
} 

只要確保您的彈簧配置文件的路徑是正確的!這是從this link改編的。

從更廣泛的角度來看,這種方法不允許你彈出頁面類,並且作爲一個MVC體系結構,其中視圖是非常笨的類,並不真正支持視圖本身的豐富驗證。查看JQuery中包含模型(後置)中的驗證。

0

據我所知,Spring Validation不支持ASP.NET MVC(1.0)和Spring.NET框架(1.3)的最新版本。

就將Spring.NET與MVC結合使用而言,您可以使用MvcContrib項目,並使用與Colin Desmond發佈的代碼相同的代碼(但您不必自己執行骯髒的工作)。