Reputation: 781
I have two files.
An .html that contains a <input type="text">
and a .java that contains java methods.
In a java method I need to set the value of that html input.
What is the code to do that?
Here is the code I have.
View
<form action="personform" method="post" th:object="${personBind}">
<input type="text" name="name" th:field="*{name}">
<input type="text" name="year" th:field="*{year}">
Spring MVC Controller
ModelAndView modelAndView = new ModelAndView("Person.html");
modelAndView.addObject("personBind", person);
return modelAndView;
The value in the name field comes from the person object in the modelAndView. That works fine. But the value for the year field is not in the person object. I want to set the value in the year field coming from a different source. What code should I write to set the value in the year field?
Upvotes: 1
Views: 2851
Reputation: 781
I found the answer after some tests.
This is how the code I posted on the question needs to be updated.
View
<form action="personform" method="post" th:object="${personBind}">
<input type="text" name="name" th:field="*{name}">
<input type="text" name="year" th:value="${yearBind}">
Controller
ModelAndView modelAndView = new ModelAndView("Person.html");
modelAndView.addObject("personBind", person);
modelAndView.addObject("yearBind", "2020");
return modelAndView;
Note it's a th:value for the year instead of a th:field.
Upvotes: 1
Reputation: 332
If I understand your question completely you want the year field to be populated from data from a different object. Perhaps try implementing a Data Transfer Object to back the form instead of the actual object you're trying to modify. Something like this:
public class Person {
private String name;
private Year year;
public Person() {
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Year getYear() {
return year;
}
public void setYear(Year year) {
this.year = year;
}
}
public class PersonDTO {
private String name;
private String year;
public PersonDTO() {
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getYear() {
return year;
}
public void setYear(String year) {
this.year = year;
}
public Person getPerson() {
Person person = new Person();
person.setName(name);
person.setYear(Year.parse(year));
return person;
}
}
Then in your controller has something like this
PersonDTO dto = new PersonDTO();
// manipulate object as needed, setters ect.
ModelAndView modelAndView = new ModelAndView("Person.html");
modelAndView.addObject("personDto", dto);
return modelAndView;
And your thymeleaf view looks something like this
<form action="personform" method="post" th:object="${personDto}">
<input type="text" name="name" th:field="*{name}">
<input type="text" name="year" th:field="*{year}">
What this does is it decouples the Person object from the form so that if the year data is coming from somewhere else you can modify the DTO to facilitate that then in your @PostMapping
where you process the form data, save it ect. you can call personDto.getPerson()
and save the Person
object or do what ever you were planning to do.
Upvotes: 1
Reputation: 1737
Add the model object you want to populate in form in the Controller class using
Student s = new Student("job","tyson");
model.addAttribute("student",s);
Now you can use the same object value in the form by using form tags
<%@taglib prefix="form" uri="http://www.springframework.org/tags/form"%>
<html>
<head>
<title>form</title>
</head>
<body>
<form:form action="processForm" modelAttribute="student">'
First Name : <form:input path="firstName" />
<input type="submit">
</form:form>
</body>
</html>
The modelAttribute should match the model attribute passed to jsp by the controller, spring in the background will populate the form input by calling respective getter methods. so FirstName input field will be populated with student.getFirstName(), if it is null the input field will be blank.
Upvotes: 0