英文:
How can I write only long number as String on form input?
问题
我正在使用Spring Data JPA在Spring Boot中进行注册。我在将长整型值放入表单字段时遇到了问题。
**模型类:** 我在`private long nsuID`之前使用了`@NotBlank`注解,但是当表单重定向时,`nsuID`字段的值被填充为0。
@Entity
@Table(name="alumnies",uniqueConstraints = @UniqueConstraint(columnNames = "email"))
public class Alumni {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private long id;
@Column(name="NSU_ID")
@NotBlank
private long nsuID;
public long getNsuID() {
return nsuID;
}
public void setNsuID(long nsuID) {
this.nsuID = nsuID;
}
}
**HTML文件:**
<div class="form-group"
th:classappend="${#fields.hasErrors('nsuID')}? 'has-error':''">
<label for="nsuID" class="control-label">NSU ID</label> <input
id="nsuID" class="form-control" th:field="*{nsuID}" />
<p class="error-message"
th:each="error : ${#fields.errors('nsuID')}"
th:text="${error}">Validation error</p>
</div>
**控制器类:**
@Controller
public class RegistraionController {
@Autowired
private UserService us;
@GetMapping("/")
public String showRegistration(Model model) {
Alumni alumni = new Alumni();
model.addAttribute("alumni", alumni);
return "registration";
}
@PostMapping("/registration")
public String saveAlumni(@ModelAttribute("alumni") @Validated Alumni alumni, BindingResult bindingResult) {
Alumni existing = us.findByEmail(alumni.getEmail());
if (existing != null) {
bindingResult.rejectValue("email", null, "There is already an account registered with that email");
}
if (bindingResult.hasErrors()) {
return "registration";
}
us.saveAlumni(alumni);
return "redirect:/";
}
}
英文:
I am making a registration in Spring Boot using Spring Data JPA. I'm facing a problem in placing long values in a form field.
Model class: I have used @NotBlank
annotation before private long nsuID
, but when the form redirects the nsuID
field has filled up by 0.
@Entity
@Table(name="alumnies",uniqueConstraints = @UniqueConstraint(columnNames = "email") )
public class Alumni {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private long id;
@Column(name="NSU_ID")
@NotBlank
private long nsuID;
public long getNsuID() {
return nsuID;
}
public void setNsuID(long nsuID) {
this.nsuID = nsuID;
}
}
HTML file:
<div class="form-group"
th:classappend="${#fields.hasErrors('nsuID')}? 'has-error':''">
<label for="nsuID" class="control-label">NSU ID</label> <input
id="nsuID" class="form-control" th:field="*{nsuID}" />
<p class="error-message"
th:each="error : ${#fields.errors('nsuID')}"
th:text="${error}">Validation error</p>
</div>
Controller class:
@Controller
public class RegistraionController {
@Autowired
private UserService us;
@GetMapping("/")
public String showRegistration(Model model) {
Alumni alumni = new Alumni();
model.addAttribute("alumni", alumni);
return "registration";
}
@PostMapping("/registration")
public String saveAlumni(@ModelAttribute("alumni") @Validated Alumni alumni, BindingResult bindingResult) {
Alumni existing = us.findByEmail(alumni.getEmail());
if (existing != null) {
bindingResult.rejectValue("email", null, "There is already an account registered with that email");
}
if (bindingResult.hasErrors()) {
return "registration";
}
us.saveAlumni(alumni);
return "redirect:/";
}
}
答案1
得分: 1
如@Dip Halani所说,long类型的变量不能保存null值,因为它是原始类型变量,而且long类型变量的默认值是0。
而您在nsuID
上的@NotBlank
约束是错误的,因为它检查字符序列的修剪长度不为空,通常用于验证给定的字符串值不得为null,并且修剪后的长度必须大于零。
如果您想要验证非null值,请使用@NotNull
,并将long nsuID
替换为Long nsuID
。
详见Bean验证中@NotNull、@NotEmpty和@NotBlank约束之间的区别。
英文:
As @Dip Halani said, long type variable can't hold null value since it's primitive type variable and long type variable's default value is 0.
And your @NotBlank
constraint on nsuID
is wrong because it checks that a character sequence's trimmed length is not empty it's usually used for validate given string value must be not null and the trimmed length must be greater than zero.
If you want to validate for non-null value then use @NotNull
and replace long nsuID
to Long nsuID
.
See Difference Between @NotNull, @NotEmpty, and @NotBlank Constraints in Bean Validation
答案2
得分: 0
long无法保存空值,因此可能会转换为0。尝试将其更改为Long(包装类)。
@NotBlank
private Long nsuID;
英文:
long can not hold null values so it might be converting to 0. try changing it to Long (wrapper class).
@NotBlank
private Long nsuID;
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论