Register Firs-6ren">
gpt4 book ai didi

java - Spring 4 验证不起作用

转载 作者:行者123 更新时间:2023-12-01 12:05:45 25 4
gpt4 key购买 nike

我创建了表单,我想验证从此表单发布的值。这是我的代码:

registerForm.jsp

<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<%@ page session="false" %>
<html>
<head>
<title>Spittr</title>
<link rel="stylesheet" type="text/css"
href="<c:url value="/resources/style.css" />" >
</head>
<body>
<h1>Register</h1>
<form method="POST">
First Name: <input type="text" name="firstName" /><br/>
Last Name: <input type="text" name="lastName" /><br/>
Username: <input type="text" name="username" /><br/>
Password: <input type="password" name="password" /><br/>
<input type="submit" value="Register" />
</form>
</body>
</html>

maven 依赖:

<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-validator</artifactId>
<version>5.1.3.Final</version>
</dependency>

bean :

package spitter;

import org.apache.commons.lang3.builder.EqualsBuilder;
import org.apache.commons.lang3.builder.HashCodeBuilder;
import org.hibernate.validator.constraints.NotEmpty;

import javax.validation.constraints.NotNull;
import javax.validation.constraints.Size;

public class Spitter {

private Long id;

@NotNull
@Size(min=5, max=16)
private String username;

@NotNull
@Size(min=5, max=25)
private String password;

@NotNull
@Size(min=2, max=30)
private String firstName;

@NotNull
@Size(min=2, max=30)
private String lastName;

private String email;

public Spitter() {}

public Spitter(String username, String password, String firstName, String lastName, String email) {
this(null, username, password, firstName, lastName, email);
}

public Spitter(Long id, String username, String password, String firstName, String lastName, String email) {
this.id = id;
this.username = username;
this.password = password;
this.firstName = firstName;
this.lastName = lastName;
this.email = email;
}

public String getUsername() {
return username;
}

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

public String getPassword() {
return password;
}

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

public Long getId() {
return id;
}

public void setId(Long id) {
this.id = id;
}

public String getFirstName() {
return firstName;
}

public void setFirstName(String firstName) {
this.firstName = firstName;
}

public String getLastName() {
return lastName;
}

public void setLastName(String lastName) {
this.lastName = lastName;
}

public String getEmail() {
return email;
}

public void setEmail(String email) {
this.email = email;
}

@Override
public boolean equals(Object that) {
return EqualsBuilder.reflectionEquals(this, that, "firstName", "lastName", "username", "password", "email");
}

@Override
public int hashCode() {
return HashCodeBuilder.reflectionHashCode(this, "firstName", "lastName", "username", "password", "email");
}
}

请求:

@RequestMapping(value="/register", method=POST)
public String processRegistration(@Valid Spitter spitter, BindingResult errors) {
if (errors.hasErrors()) {
return "registerForm";
}

repository.save(spitter);
return "redirect:/spitter/" + spitter.getUsername();
}

我的问题是,无论表单验证中的输入如何,它总是通过。你知道我该如何解决它吗?

顺便说一句:我使用 Spring 4 和完全无 xml 的配置。

最佳答案

在 registerForm.jsp 中的输入中,您需要适本地标记每个输入。另外,如果有的话,您需要为实际错误添加某种标签。例如:

First Name: <input type="text" name="firstName" th:field="*{firstName)">
<p th:if="${#fields.hasErrors('firstName')}" th:errors="*{firstName}">First Name Error</p>

查看 Spring 验证文档! (http://spring.io/guides/gs/validating-form-input/)

关于java - Spring 4 验证不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27626865/

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