2017-04-04 39 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