在 Spring.NET 验证中加载正确的上下文
问题:当我尝试使用配置文件中的 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
试试这个:
try this :