gpt4 book ai didi

java - ComboBox 的 Hibernate validator

转载 作者:行者123 更新时间:2023-11-30 10:47:02 25 4
gpt4 key购买 nike

如何验证 jComboBox 以供用户选择除默认项以外的任何项?

我需要检查用户是否没有选择任何项目。所以 jComboBox 值将是“请选择安全问题”

enter image description here

在我的模型类中,

@NotEmpty(message="Please fill the username field!")
public String getUsername() {
return this.username;
}

public void setUsername(String username) {
this.username = username;
}

@NotEmpty(message="Please fill the password field!")
public String getPassword() {
return this.password;
}

public void setPassword(String password) {
this.password = password;
}

public String getSeqQue() {
return this.seqQue;
}

public void setSeqQue(String seqQue) {
this.seqQue = seqQue;
}

添加到 getSeqQue() 中以验证我的 jComboBox 的 hibernate validator 注释是什么?

最佳答案

To validate your JComboBox with custom message simply you can make custom constraint validator.

请看下面的例子:

MyModel.java

public class MyModel {

@ValidComboBox //this is the annotation which validates your combo box
private String question;

//getter and setter
}

ValidComboBox.java//注解

import java.lang.annotation.*;
import javax.validation.*;

@Target({ ElementType.METHOD, ElementType.FIELD, ElementType.ANNOTATION_TYPE })
@Retention(RetentionPolicy.RUNTIME)
@Constraint(validatedBy = ComboBoxValidator.class)
@Documented
public @interface ValidComboBox {
String value = "Please select a security question";

String message() default "Please select a security question.";

Class<?>[]groups() default {};

Class<? extends Payload>[]payload() default {};
}

ComboBoxValidator.java

import javax.validation.*;
public class ComboBoxValidator implements ConstraintValidator<ValidComboBox, String> {

private String value;

@Override
public void initialize(ValidComboBox arg0) {
this.value = arg0.value;

}

@Override
public boolean isValid(String question, ConstraintValidatorContext arg1) {
if(question.equalsIgnoreCase(value)){
return false;
}
return true;
}
}

像这样将项目添加到您的 jComboBox 中:

JComboBox<String> jComboBox = new JComboBox<>();
jComboBox.addItem("Please select a scurity question");
jComboBox.addItem("Question 1");
jComboBox.addItem("Question 2");

在执行验证操作时需要添加以下行:

ValidatorFactory validatorFactory = Validation.buildDefaultValidatorFactory();
Validator validator = validatorFactory.getValidator();

String question = jComboBox.getSelectedItem().toString();
MyModel model = new MyModel();
model.setQuestion(model);

Set<ConstraintViolation<MyModel>> constraintViolations = validator.validate(model);

if (!constraintViolations.isEmpty()) {
String error = "";
for (ConstraintViolation<MyModel> constraintViolation : constraintViolations) {
error += constraintViolation.getMessage();
JOptionPane.showMessageDialog(null, error);
}
}

It will display Please select a security question message if you try to send request without choosing question.

关于java - ComboBox 的 Hibernate validator ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36276360/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com