Spring validation @AssertTrue

為{幸葍}努か 提交于 2020-01-23 06:58:24

问题


How do I display on a view jsp validation error message that occurs as a result of @AssertTrue annotation? It isn't tied to a specific field, but I am using it to validate a combination of fields. If I use <form:errors path="*"/> that will display all the errors for that form?


回答1:


From what I have tested it is important HOW you name your test function. And you should name it properly.

You do not need field, getter or setter but your function HAVE TO start with 'is*' statement.

fe.

@AssertTrue
public boolean isConditionTrue() {
   ...
   ...


}

or

@AssertTrue
public boolean isSomethingElseOk() {
   ...
   ...
}

Though, you need a field and getter/setter if you need to use a error form with path, like:

<form:errors path="someFieldToDisplay" />

But i think this is quite obvious.


Some schema problem which I didn't step into but might be helpful:

This might be helpful as well: lack of error messages.

But if you use schema without version tag, it uses the newest version by default.




回答2:


Declaring a boolean property is what seems to work for this. So if there is:

@AssertTrue
public boolean isConditionTrue() {
   ...
   ...
}

then declaring a property like:

private boolean conditionTrue;

works.




回答3:


You should name your property like this:

@AssertTrue(message = "....")
private boolean conditionTrue; //***NOT isConditionTrue***

public boolean isConditionTrue() {
    return conditionTrue;
}

public void setConditionTrue(boolean conditionTrue) {
    this.conditionTrue= conditionTrue;
}

<form:errors path="*"/> or
<form:errors path="conditionTrue"/>


来源:https://stackoverflow.com/questions/2853826/spring-validation-asserttrue

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