使用 ASP.NET MVC 设置 Spring.NET 验证框架

发布于 2024-07-21 08:17:04 字数 3732 浏览 8 评论 0原文

我正在开始一个新的工作项目,我决定尝试一下 MVC。 这是一个小型内部站点,适合通勤挑战。

我想使用 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>

我的验证文件非常标准,基本上是从另一个项目复制和粘贴的,因此我没有包含它。

现在我遇到的问题是如何使用它? 如何获取应用程序上下文? 我的 Web 表单项目用户 Spring.Web.UI.Page,但我很担心,因为 MVC 中的默认页面派生自 System.Web.Mvc.ViewPage,因此是行不通的。

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

谢谢!

感谢您的任何帮助。

I'm starting a new project for work, and I decided I want to give MVC a shot. It's a small internal site for a commute challenge.

I want to use Spring.NET for Validation. I have used Spring.NET before in Web Forms, but with no code behind as in traditional ASP.NET, how do I use the Page Validation framework Spring.NET provides?

Edit 1:

In an attempt to try this myself, here is what I have:

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>

My validation file is very standard and basically a copy and paste from another project, therefore I didn't include it.

Now the problem I have is how do I use it? How do I get application context? My web forms project users Spring.Web.UI.Page, but I'm worried because the default pages in MVC derive from System.Web.Mvc.ViewPage, so that isn't going to work.

Or am I just not able to use Spring.NET's framework for MVC quite yet?

Thanks!

Thanks for any assistance.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

素罗衫 2024-07-28 08:17:04

您绝对可以将 Spring 与 ASP.Net MVC 结合使用。 您需要在 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);
    }
}

只要确保你的 spring 配置文件的路径是正确的! 此内容改编自此链接

更广泛地说,这种方法不允许您 Spring 页面类,并且作为 MVC 架构,其中视图是相当愚蠢的类,并不真正支持视图本身以您建议的方式进行丰富的验证。 查看在 JQuery 的模型(回发)中包含验证。

You definitely can use Spring with ASP.Net MVC. You need to register that you are using it in the Global.ascx class then the framework will create Controllers based on what you have defined in your config file.

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);
    }
}

Just make sure that the path to your spring config file is correct! This was adapted from this link.

On a wider note, this approach does not allow you to Spring the page classes, and being an MVC architecture, where views are pretty dumb classes, does not really support rich validation in the view itself in the manner you suggest. Look at either including the validation in the Model (post-back) in JQuery.

迷路的信 2024-07-28 08:17:04

据我所知,最新版本的 ASP.NET MVC (1.0) 和 Spring.NET Framework (1.3) 不支持 Spring 验证。

至于将 Spring.NET 与 MVC 结合起来,您可以使用 MvcContrib 项目并使用 Colin Desmond 发布的相同代码库(但您不必自己做这些肮脏的工作)。

In best of my knowledge, the Spring Validation is not supported up to the latest release of ASP.NET MVC (1.0) and Spring.NET framework (1.3).

As far as incorporating Spring.NET with MVC, you can use MvcContrib project and come to the same code-base as posted by Colin Desmond (but you don't have to do the dirty work yourself).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文