0
問題:我正在拋出此異常,而我正嘗試使用配置文件中的Sprint.NET驗證驗證名爲StudyEnvironment的業務對象。在Spring.NET驗證中加載正確的上下文
'StudyEnvironment' node cannot be resolved for the specified context [Validation.Models.StudyEnvironment].
下面是重現異常的步驟,它被引發到最後一行。
問題:我在這裏錯過了什麼嗎?我需要在配置文件中以某種xml格式聲明我的業務對象嗎?或者Spring.NET只是神奇地選擇它,我是Spring.NET的新手。
第1步:定義在web.config中一類
namespace Validation.Models
{
public class StudyEnvironment
{
private Guid id;
public String Name { get; set; }
public StudyEnvironment()
{
this.id = Guid.NewGuid();
}
}
}
新增確認部分
<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);
這工作,謝謝,它比我想它會更容易:) – Usman 2010-11-04 11:35:22