Issue
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?
Solution
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.
Answered By - jkfe