开发者

How to pass parameter to a method of validating the Spring Webflow?

How do I pass to the method validate {State} parameter other than a model?

I have two objects. MyForm form object and the object of User, who may modify MyForm.

View definition:

<view-state id="finish" view="finish" model="myform">       
<transition on="submit" to="goToFinish" bind="true" validate="true" />
</view-state>

My validation method

private MessageContext context;

public void validateSummary(MyForm myForm, MessageContext context) throws 
BusinessException开发者_JAVA技巧 {
        this.context = context;

        validateAddress(myForm);

....

How can I validate an attribiute from User? Without setting User to MyForm object?

Please help.


from Spring Webflow Documentation:

  • There are two ways to perform model validation programatically. The first is to implement validation logic in your model object. The second is to implement an external Validator. Both ways provide you with a ValidationContext to record error messages and access information about the current user.

Simply create a public method with the name validate${state}, where ${state} is the id of your view-state where you want validation to run (in your example):

<view-state id="finish" view="finish" model="myform">       
    <transition on="submit" to="goToFinish" bind="true" validate="true" />
</view-state>

A default validator would be a public method within a myform model object:

public void validateFinish( ValidationContext context ) {

     ...
     context.getUserPrincipal() // current user
     ...
}

A ValidationContext allows you to obtain a MessageContext to record messages during validation. It also exposes information about the current user, such as the signaled userEvent and the current user's Principal identity.

From ValidationContext API:

 // The current user event that triggered validation.
 java.lang.String  getUserEvent() 

 // The current user.   
 java.security.Principal  getUserPrincipal() 

 // Obtain the value entered by the current user in the UI field bound to the property provided.    
 java.lang.Object  getUserValue(java.lang.String property) 
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜