How to pass session attribute to a ThymeLeaf form field for saving into database

问题: I have a Thymeleaf form that sends data to a database table when the user is logged in. The ThymeLeaf template is able to capture the session of the logged in user from it'...


I have a Thymeleaf form that sends data to a database table when the user is logged in. The ThymeLeaf template is able to capture the session of the logged in user from it's assigned controller. When saving data to the database.

I need to accompany the form data alongside the captured session username, such that the said session username is saved in the database table as well. However I get an error that says

org.springframework.beans.NotReadablePropertyException: Invalid property 'userName' of bean class [com.example.jobonics.Persistence.model.NewJobs]: Bean property 'userName' is not readable or has an invalid getter method: Does the return type of the getter match the parameter type of the setter?

I have a controller that picks the user session and sends it to the thymeleaf template as well as a hidden input user field in my form that holds the session object from the controller. My code is pasted below for some clarity:

@RequestMapping(value = "/new_job", method = RequestMethod.GET)
public String newRegistration(ModelMap model) {
    NewJobs newJobs = new NewJobs();
    Authentication auth = SecurityContextHolder.getContext().getAuthentication();
    ServletRequestAttributes attr = (ServletRequestAttributes)

   //extract user session from HttpSession
    HttpSession session = attr.getRequest().getSession(true);
    //initialize user service to get authenticated user
    User user = userService.findUserByEmail(auth.getName());

    model.addAttribute("newjobs", newJobs);
    //add session captured name as attribute to view
    model.addAttribute("userName", "Welcome " + user.getFullName() + "");
    return "new_job";

@RequestMapping(value = "/save", method = RequestMethod.POST)
public String saveRegistration(@Valid NewJobs newJobs, BindingResult result, 
ModelMap model,
        RedirectAttributes redirectAttributes) {
    if (result.hasErrors()) {
     System.out.println("has errors");
        return "new_job";

    return "redirect:/new_job";

My controller

<form class="form-horizontal" th:action="@{/save}" th:object="${newjobs}" method="post">

<div class="form-group">
    <div class="col-sm-7">
        <input type="text" class="form-control" th:value="*{userName}" style="display:none">


@Table(name = "newjobs")
public class NewJobs {
@GeneratedValue(strategy = GenerationType.AUTO)
@Column(name = "id")
private int job_id;

@Column(name = "Employer")
private String Employer;

@Column(name = "JobTitle")
@NotEmpty(message = "Please provide the job title")
private String jobTitle;

@Column(name = "job_summary", length = 1000)
private String jobSummary;

@Column(name = "job_description", length = 1000)
private String jobDescription;

@Column(name = "location")
private String Location;

@Column(name = "career_level")
@NotEmpty(message = "Please provide career level required")
private String careerLevel;

@Column(name = "industry")
private String industry;

@Column(name = "profession")
@NotEmpty(message = "Please provide the profession")
private String profession;

@Column(name = "jobtype")
@NotEmpty(message = "Please provide your job type")

private String jobType;
@Column(name = "min_experience")
@NotEmpty(message = "Please provide the Min years of experience")

private String minExperience;
@Column(name = "min_qualification")
@NotEmpty(message = "Please provide the Min Qualifications")
private String minQualification;

@Column(name = "salary")
@NotEmpty(message = "Please provide the salary")
private String salary;

@Column(name = "deadline_date")
@NotEmpty(message = "Please provide the Applications Deadline")
private String deadlineDate;

@Column(name = "created_at", columnDefinition = "TIMESTAMP DEFAULT CURRENT_TIMESTAMP", insertable = false, nullable = false)
private Date created_at;

My expected output is to send the logged in user session object to db as part of a submitted form. What can I do to correct the said error above?


I think the mistake is you got username from wrong location:
th:value="*{userName}" mean newjobs.username
but in controller, you do not set username to newjobs, you set to model attribute

Let change code in controller (with additional getter)
newjobs.setUserName("Welcome " + user.getFullName())

th:value="${userName}" ($ but not *)


There is an important difference though: the asterisk syntax evaluates expressions on selected objects rather than on the whole context. That is, as long as there is no selected object, the dollar and the asterisk syntaxes do exactly the same.

  • 发表于 2019-02-19 01:02
  • 阅读 ( 1232 )
  • 分类:sof


请先 登录 后评论


作家榜 »

  1. 小编 文章