Answer the question
In order to leave comments, you need to log in
How to pass date from jsp page through spring to mysql database?
Please help me figure out how to pass the date from the jsp page to the database.
It would be desirable that the date could be selected and then sent to the database. Used
In the database, the column birthDate is of type date. When sending other values, they are all written to the database, but when I enter the date, it gives an error.
<input type="date" name="birthDate">
The request sent by the client was syntactically incorrect.
<%@ page language="java" contentType="text/html" pageEncoding="utf8"%>
<!DOCTYPE html>
<html lang="ru">
<body>
<form action="${pageContext.servletContext.contextPath}/addclient" method="post">
<table >
<th><input type="text" name="surname" placeholder="Фамилия" ></th>
<th><input type="text" name="name" placeholder="Имя" ></th>
<th><input type="text" name="patronymic" placeholder="Отчество" required></th>
<th><input type="date" name="birthDate" ></th>
</table>
<input type="submit" value="Создать">
</div>
</form>
</body>
</html>
@Entity
@Table(name = "client")
public class Client {
@Id
@Column(name = "id")
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Integer id;
@Basic
@Column(name = "surname")
private String surname;
@Basic
@Column(name = "name")
private String name;
@Basic
@Column(name = "patronymic")
private String patronymic;
@Basic
@Column(name = "birthDate")
private Date birthDate;
public Client() { }
@Controller
public class ClientController {
@Autowired
private ClientService clientService;
@RequestMapping(value = "/addclient", method = RequestMethod.POST)
public String addClient(@ModelAttribute Client client) {
clientService.addClient(client);
return "redirect:/client";
}
}
@Repository
public class ClientDaoImpl implements ClientDao{
@Autowired
private SessionFactory sessionFactory;
public void addClient(Client client){
sessionFactory.getCurrentSession().save(client);
}
}
Answer the question
In order to leave comments, you need to log in
As far as I understand your problem is related to the fact that you are using java.util.Date and trying to store it in mysql. There are several solutions:
1) In the Client class, change the field type to java.sql.Date and do the conversion somewhere before saving to the database
2) Add @Temporal annotation
@Temporal(TemporalType.DATE)
private Date birthDate;
java.text.SimpleDateFormat sdf =
new java.text.SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String birthDateTime = sdf.format(birthDateInput);
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question