Я должен заполнить форму, проверить данные и, если они верны, отправить их на другую страницу, чтобы показать их. Но по какой-то причине я не могу заставить его работать, как только я нажимаю кнопку отправки формы, это мой код.
public class Formulario {
@NotEmpty
@NotNull
@Size(min = 2, max = 30)
private String nombre;
@NotEmpty
@NotNull
@Email
private String email;
private Integer movil;
private String sitioWeb;
@NotEmpty
@NotNull
private String consulta;
public String getNombre() {
return nombre;
}
public void setNombre(String nombre) {
this.nombre = nombre;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public Integer getMovil() {
return movil;
}
public void setMovil(Integer movil) {
this.movil = movil;
}
public String getSitioWeb() {
return sitioWeb;
}
public void setSitioWeb(String sitioWeb) {
this.sitioWeb = sitioWeb;
}
public String getConsulta() {
return consulta;
}
public void setConsulta(String consulta) {
this.consulta = consulta;
}
@Override
public String toString() {
return "Formulario{" + "nombre=" + nombre + ", email=" + email + ", movil=" + movil + ", sitioWeb=" + sitioWeb + ", consulta=" + consulta + '}';
}
}
@Controller
publi c реализует класс WebController WebMvcConfigurer {
@Override
public void addViewControllers(ViewControllerRegistry registry) {
registry.addViewController("/results").setViewName("results");
}
@GetMapping("/")
public String showForm(Model model) {
model.addAttribute("personForm", new Formulario());
return "formulario";
}
@PostMapping("/")
public String checkPersonInfo(@Valid @ModelAttribute("personForm") Formulario personForm, BindingResult bindingResult) {
if (bindingResult.hasErrors()) {
return "formulario";
}
return "redirect:/results";
}
} это форма
<!DOCTYPE html>
<!--
To change this license header, choose License Headers in Project Properties.
To change this template file, choose Tools | Templates
and open the template in the editor.
-->
<html xmlns:th="https://www.thymeleaf.org">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.4.1/css/bootstrap.min.css" integrity="sha384-Vkoo8x4CGsO3+Hhxv8T/Q5PaXtkKtu6ug5TOeNV6gBiFeWPGFN9MuhOf23Q9Ifjh" crossorigin="anonymous">
<title>Getting Started: Handling Form Submission</title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
</head>
<body style="background-color: black">
</br> </br>
<div class="container">
<div class="row">
<div class="col">
</div>
<div class="col-md-6 h-50" style="background-color:#fff;border: 25px solid green">
<div class="">
<fieldset>
<legend class="text-center header">Formulario sencillo</legend>
<p>Rellene los campos y pulse enviar</p>
<form action="#" th:action="@{/}" th:object="${personForm}" method="post">
<div class="form-group">
<span class="col-md-1 col-md-offset-2 text-center"><i class="fa fa-user bigicon"></i></span>
<div class="col-md-24">
<input th:field="*{nombre}" id="nombre" type="text" placeholder="nombre y apellidos" class="form-control">
<td th:if="${#fields.hasErrors('nombre')}" th:errors="*{nombre}">Error</td>
</div>
<span class="col-md-1 col-md-offset-2 text-center"><i class="fa fa-user bigicon"></i></span>
<div class="col-md-24">
<input th:field="*{email}" id="email" type="text" placeholder="Correo electronico" class="form-control">
<div th:if="${#fields.hasErrors('email')}" th:errors="*{email}">Error</div>
</div>
<span class="col-md-1 col-md-offset-2 text-center"><i class="fa fa-phone-square bigicon"></i></span>
<div class="col-md-24">
<input th:field="*{movil}" id="movil" type="text" placeholder="telefono (opcional)" class="form-control">
<div th:if="${#fields.hasErrors('movil')}" th:errors="*{movil}">Error</div>
</div>
<span class="col-md-1 col-md-offset-2 text-center"><i class="fa fa-phone-square bigicon"></i></span>
<div class="col-md-24">
<input th:field="*{sitioWeb}" id="sitioWeb" type="text" placeholder="sitio web (opcional)" class="form-control">
<div th:if="${#fields.hasErrors('sitioWeb')}" th:errors="*{sitioWeb}">Error</div>
</div>
<span class="col-md-1 col-md-offset-2 text-center"><i class="fa fa-pencil-square-o bigicon"></i></span>
<div class="col-md-24">
<textarea th:field="*{consulta}" class="form-control" id="consulta" placeholder="Escriba su consulta aqui" rows="3"></textarea>
<div th:if="${#fields.hasErrors('consulta')}" th:errors="*{consulta}">Error</div>
</div>
</div>
<div class="form-group">
<div class="col-md-22 text-center">
<button type="submit" value="Submit" class="btn btn-primary btn-lg btn-block bg-sucess" style="background-color: green" >Enviar</button>
</div>
</div>
</form>
</fieldset>
</div>
</div>
<div class="col">
</div>
</div>
</div>
</br> </br>
<!-- Optional JavaScript -->
<!-- jQuery first, then Popper.js, then Bootstrap JS -->
<script src="https://code.jquery.com/jquery-3.4.1.slim.min.js" integrity="sha384-J6qa4849blE2+poT4WnyKhv5vZF5SrPo0iEjwBvKU7imGFAV0wwj1yYfoRSJoZ+n" crossorigin="anonymous"></script>
<script src="https://cdn.jsdelivr.net/npm/popper.js@1.16.0/dist/umd/popper.min.js" integrity="sha384-Q6E9RHvbIyZFJoft+2mJbHaEWldlvI9IOYy5n3zV9zzTtmI3UksdQRVvoxMfooAo" crossorigin="anonymous"></script>
<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.4.1/js/bootstrap.min.js" integrity="sha384-wfSDF2E50Y2D1uUdj0O3uMBJnjuUD4Ih7YwaYd1iqfktj0Uod8GCExl3Og8ifwB6" crossorigin="anonymous"></script>
</body>
</html>
и это страница результата
<!DOCTYPE html>
<!--
To change this license header, choose License Headers in Project Properties.
To change this template file, choose Tools | Templates
and open the template in the editor.
-->
<html xmlns:th="https://www.thymeleaf.org">
<head>
<title>Getting Started: Handling Form Submission</title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
</head>
<body>
<h1>Result</h1>
<p th:text="'nombre ' + ${personForm.nombre}" />
<p th:text="'email ' + ${personForm.email}" />
<p th:text="'movil ' + ${personForm.movil}" />
<p th:text="'sitioWeb ' + ${personForm.sitioWeb}" />
<p th:text="'consulta ' + ${personForm.consulta}" />
<a href="/form">volver al formulario</a>
</body>
</html>
В «результате» должны отображаться данные, собранные в форме, но выдается ошибка. Произошла непредвиденная ошибка (тип = Внутренняя ошибка сервера, статус = 500). Исключительная ситуация при вычислении выражения SpringEL: "personForm.name" (шаблон: "results" - строка 14, столбец 12)