2017-04-04 87 views
0

我试图在Spring Web Flow中进行表单验证。为此,我使用一个验证器类,它以该模型命名。就像它在文档中所述。 验证程序被实例化为一个bean,但在验证过程中从未被调用。在这个问题上的任何指针?未找到Spring Web Flow验证程序

流配置

<?xml version="1.0" encoding="UTF-8"?> 
<flow xmlns="http://www.springframework.org/schema/webflow" 
     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
     xsi:schemaLocation="http://www.springframework.org/schema/webflow http://www.springframework.org/schema/webflow/spring-webflow-2.4.xsd"> 

    <view-state id="createTotpKeyView" view="/templates/totp/create/create" model="key"> 
     <on-entry> 
      <evaluate expression="createTotpKeyAction"/> 
     </on-entry> 
     <transition on="submit" to="successfullyCreated" bind="true" validate="true"/> 
    </view-state> 

    <end-state id="successfullyCreated" view="/templates/totp/create/success"/> 
</flow> 

这是被称为在视图状态的动作。

createTotpKeyAction

@Component 
public class CreateTotpKeyAction implements Action 
{ 
    String uid = "random"; 

    @Override 
    public Event execute(RequestContext context) throws Exception 
    { 
     try 
     { 
      // Create a TOTP key and put it in the view scope 
      TOTPKey totpKey = client.createTotpKeyForUid(uid, null); 
      context.getViewScope().put("key", totpKey); 

      return new Event(this, "success"); 
     } 
     catch (Exception e) 
     { 
      log.error("Error while creating TOTP key for user: " + uid + ".\n" + e.getMessage()); 
      // Put response message in flash scope to show it once 
      context.getFlashScope().put("fetchingError", true); 
      return new Event(this, "error"); 
     } 
    } 
} 

这是我试图使用验证。 编辑改名为匹配文档。

KeyValidator

@Component 
public class KeyValidator 
    { 
     [...] 

    public void validateCreateTotpKeyView(TOTPKey key, ValidationContext context) 
    { 
     System.out.println("VALIDATE VIEW STATE"); 
    } 

    public void validate(TOTPKey key, ValidationContext context) 
    { 
     System.out.println("DEFAULT VALIDATE"); 
    } 
} 

我还尝试了不同的命名方案如TOTPKeyValidatorTotpKeyValidator。他们都没有工作。

唯一可行的是在TOTPKey类中创建验证方法,但我不想使用该方法。

此外,这是试图验证过程中产生的

登录

Mapping request with URI '/totp/create' to flow with id 'totp/create' 
Resuming flow execution with key 'e5s1 
Locking conversation 5 
Getting flow execution with key 'e5s1' 
Getting FlowDefinition with id 'totp/create' 
Resuming in [email protected]393 
Restoring [[email protected] name = 'key', valueFactory = [[email protected] type = TOTPKey]] 
Processing user event 'submit' 
Resolved model [email protected] 
Binding to model 
Adding default mapping for parameter 'execution' 
Adding default mapping for parameter 'totpKeyId' 
Adding default mapping for parameter 'token' 
Adding empty value mapping for parameter 'eventId_submit' 
Validating model 
Event 'submit' returned from view [[email protected] view = org.springframework.web.servlet.view.velocity.VelocityLayoutView: name '/templates/totp/create/create'; URL [/templates/totp/create/create.vm]] 
Executing [[email protected] on = submit, to = successfullyCreated] 
Exiting state 'createTotpKeyView' 
Entering state 'successfullyCreated' of flow 'totp/create' 
Executing [email protected]a131 
Rendering MVC [org.springframework.web.servlet.view.velocity.VelocityLayoutView: name '/templates/totp/create/success'; URL [/templates/totp/create/success.vm]] with model map [{currentUser=null, flashScope=map[[empty]], flowRequestContext=[[email protected] externalContext = [email protected]393, currentEvent = submit, requestScope = map[[empty]], attributes = map[[empty]], messageContext = [[email protected] sourceMessages = map[[null] -> list[[empty]]]], flowExecution = [[email protected] flow = 'totp/create', flowSessions = list[[[email protected] flow = 'totp/create', state = 'successfullyCreated', scope = map['key' -> [email protected]]]]]], flowExecutionKey=e5s1, flowExecutionUrl=/totp/create?execution=e5s1, [email protected]}] 
Finished executing [email protected]a131; result = success 
Completed transition execution. As a result, the flow execution has ended 
Removing flow execution '[Ended execution of 'totp/create']' from repository 
Ending conversation 5 
Unlocking conversation 5 

它说Validating Model但没有任何反应的日志文件...

+0

我建议阅读[documentation](http://docs.spring.io/autorepo/docs/webflow/2.4.2.RELEASE/reference/html/views.html#view-validation-programmatic-validator )。验证器的名称应该匹配以下模式'$ {model} Validator'。你的模型被命名为'key'。您的验证器应该命名为'KeyValidator'。 –

+0

我试过了,它不起作用。 – lamemate

+0

确保它实际上被检测到(它必须位于Spring Boot所涵盖的包中,否则它不会构造并且永远不会工作)。 –

回答

2

它来到了一个错误的进口在我的验证器类中的声明。使用org.relaxng.datatype.ValidationContext代替org.springframework.binding.validation.ValidationContext将不起作用。

+0

我之前做过这种事情,没有注意到在IDE自动完成中选择了错误的类:-) – dbreaux