Spring Web Flow validator not found

。_饼干妹妹 提交于 2019-12-12 02:26:17

问题


I am trying to do form validation in Spring Web Flow. For this I am using a validator class, which is named after the model. Just like it is stated in the documentation. The validator gets instantiated as a bean but is never called during validation. Any pointers on that issue?

flow config

<?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>

This is the action that is called in the view-state.

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");
        }
    }
}

This is the validator I am trying to use. EDIT renamed to match documentation.

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");
    }
}

I also tried different naming schemes such as TOTPKeyValidator or TotpKeyValidator. None of them worked.

The only thing that is working, is creating a validation method in the TOTPKey class, but I don't want to use that approach.

In addition this is the log file produced during the attempted validation

Log

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 org.springframework.webflow.mvc.servlet.MvcExternalContext@2b551393
Restoring [FlowVariable@3b66a2de name = 'key', valueFactory = [BeanFactoryVariableValueFactory@2fbc89 type = TOTPKey]]
Processing user event 'submit'
Resolved model twofa.core.domain.TOTPKey@505439d0
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 [ServletMvcView@19f8532f view = org.springframework.web.servlet.view.velocity.VelocityLayoutView: name '/templates/totp/create/create'; URL [/templates/totp/create/create.vm]]
Executing [Transition@2feb5361 on = submit, to = successfullyCreated]
Exiting state 'createTotpKeyView'
Entering state 'successfullyCreated' of flow 'totp/create'
Executing org.springframework.webflow.action.ViewFactoryActionAdapter@423fa131
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=[RequestControlContextImpl@70144045 externalContext = org.springframework.webflow.mvc.servlet.MvcExternalContext@2b551393, currentEvent = submit, requestScope = map[[empty]], attributes = map[[empty]], messageContext = [DefaultMessageContext@149807b4 sourceMessages = map[[null] -> list[[empty]]]], flowExecution = [FlowExecutionImpl@1c4b2c3e flow = 'totp/create', flowSessions = list[[FlowSessionImpl@6eea5d26 flow = 'totp/create', state = 'successfullyCreated', scope = map['key' -> twofa.core.domain.TOTPKey@73f32d0a]]]]], flowExecutionKey=e5s1, flowExecutionUrl=/totp/create?execution=e5s1, key=twofa.core.domain.TOTPKey@73f32d0a}]
Finished executing org.springframework.webflow.action.ViewFactoryActionAdapter@423fa131; 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

It says Validating Model but nothing happens...


回答1:


It came down to a wrong import statement in my validator class.

Using org.relaxng.datatype.ValidationContext instead of org.springframework.binding.validation.ValidationContext will not work.



来源:https://stackoverflow.com/questions/43206989/spring-web-flow-validator-not-found

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!