问题描述
我正在尝试在 Spring Web Flow 中进行表单验证.为此,我使用了一个以模型命名的验证器类.就像文档中所说的那样.验证器被实例化为一个 bean,但在验证过程中永远不会被调用.关于这个问题的任何指示?
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?
流程配置
<?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");
}
}
}
这是我尝试使用的验证器.EDIT 重命名以匹配文档.
This is the validator I am trying to use. EDIT renamed to match documentation.
密钥验证器
@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");
}
}
我还尝试了不同的命名方案,例如 TOTPKeyValidator
或 TotpKeyValidator
.他们都没有工作.
I also tried different naming schemes such as TOTPKeyValidator
or TotpKeyValidator
. None of them worked.
唯一有效的是在 TOTPKey
类中创建验证方法,但我不想使用这种方法.
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
日志
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
它说 Validating Model
但没有任何反应......
It says Validating Model
but nothing happens...
推荐答案
这归结为我的验证器类中的错误导入语句.
It came down to a wrong import statement in my validator class.
使用 org.relaxng.datatype.ValidationContext
而不是 org.springframework.binding.validation.ValidationContext
将不起作用.
Using org.relaxng.datatype.ValidationContext
instead of org.springframework.binding.validation.ValidationContext
will not work.
这篇关于未找到 Spring Web Flow 验证器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!