在 Spring.NET 验证中加载正确的上下文

发布于 2024-09-30 11:46:49 字数 2323 浏览 5 评论 0原文

问题:当我尝试使用配置文件中的 Sprint.NET 验证来验证名为 StudyEnvironment 的业务对象时,我遇到了此异常。

'StudyEnvironment' node cannot be resolved for the specified context [Validation.Models.StudyEnvironment].

以下是重现异常的步骤,它在最后一行引发。

问题:我在这里遗漏了什么吗?我是否需要在配置文件中以某种 xml 格式声明我的业务对象?或者 Spring.NET 只是神奇地选择了它,我是 Spring.NET 的新手。

步骤 1:定义一个类

namespace Validation.Models
{
    public class StudyEnvironment
    {
        private Guid id;
        public String Name { get; set; }


        public StudyEnvironment()
        {
            this.id = Guid.NewGuid();
        }   
    }
}

在 web.config 中添加验证部分

<configuration>
  <configSections>
    <sectionGroup name="spring">      
      <section name="parsers" type="Spring.Context.Support.NamespaceParsersSectionHandler, Spring.Core"/>
      <section name="context" type="Spring.Context.Support.ContextHandler, Spring.Core" />
      <section name="objects" type="Spring.Context.Support.DefaultSectionHandler, Spring.Core" />
    </sectionGroup>
  </configSections>
  <system.web>
    <compilation debug="true" targetFramework="4.0" />
  </system.web>
    <spring>
      <context>
        <resource uri="config://spring/objects" />
      </context>            
      <objects xmlns="http://www.springframework.net" xmlns:v="http://www.springframework.net/validation">        
        <v:group id="envValidator">
          <v:required id="environmentValidator" test="StudyEnvironment.Name">
            <v:message id="error.environmentName.required" providers="errors"/>
          </v:required>
        </v:group>
      </objects>
      <parsers>
        <parser type="Spring.Validation.Config.ValidationNamespaceParser, Spring.Core" />
      </parsers>    
    </spring>
</configuration>

步骤 3:验证调用

StudyEnvironment env = new StudyEnvironment();
env.Name = "test";

IValidator validator = ContextRegistry.GetContext().GetObject("envValidator") as IValidator;
IValidationErrors validationErrors = new ValidationErrors();
bool isValid = validator.Validate(env, validationErrors);

Problem : I am having this exception being thrown, while I am trying to Validate a Business Object named StudyEnvironment using Sprint.NET Validation from config file.

'StudyEnvironment' node cannot be resolved for the specified context [Validation.Models.StudyEnvironment].

Below are steps to reproduce exception, it's thrown on last line.

Question : Am I missing something in here? Do I need to declare my Business Object in some xml format in config file? Or Spring.NET just picks it magically, I am a newbie to Spring.NET.

Step 1 : Defined a class

namespace Validation.Models
{
    public class StudyEnvironment
    {
        private Guid id;
        public String Name { get; set; }


        public StudyEnvironment()
        {
            this.id = Guid.NewGuid();
        }   
    }
}

Added Validation Section in web.config

<configuration>
  <configSections>
    <sectionGroup name="spring">      
      <section name="parsers" type="Spring.Context.Support.NamespaceParsersSectionHandler, Spring.Core"/>
      <section name="context" type="Spring.Context.Support.ContextHandler, Spring.Core" />
      <section name="objects" type="Spring.Context.Support.DefaultSectionHandler, Spring.Core" />
    </sectionGroup>
  </configSections>
  <system.web>
    <compilation debug="true" targetFramework="4.0" />
  </system.web>
    <spring>
      <context>
        <resource uri="config://spring/objects" />
      </context>            
      <objects xmlns="http://www.springframework.net" xmlns:v="http://www.springframework.net/validation">        
        <v:group id="envValidator">
          <v:required id="environmentValidator" test="StudyEnvironment.Name">
            <v:message id="error.environmentName.required" providers="errors"/>
          </v:required>
        </v:group>
      </objects>
      <parsers>
        <parser type="Spring.Validation.Config.ValidationNamespaceParser, Spring.Core" />
      </parsers>    
    </spring>
</configuration>

Step 3 : Validation Call

StudyEnvironment env = new StudyEnvironment();
env.Name = "test";

IValidator validator = ContextRegistry.GetContext().GetObject("envValidator") as IValidator;
IValidationErrors validationErrors = new ValidationErrors();
bool isValid = validator.Validate(env, validationErrors);

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

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

发布评论

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

评论(1

木格 2024-10-07 11:46:49

试试这个:

<v:group id="envValidator">
   <v:required id="environmentValidator" test="Name">
      <v:message id="error.environmentName.required" providers="errors"/>
   </v:required>
</v:group>

try this :

<v:group id="envValidator">
   <v:required id="environmentValidator" test="Name">
      <v:message id="error.environmentName.required" providers="errors"/>
   </v:required>
</v:group>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文