Why isn't the thymeleaf object recognized? - spring-boot

The first code snipped is me adding(or rather trying) the attribute 'NewUser' to use in html.
The error pops up for 'th:field="*{name}"'.
Error:
Error during execution of processor 'org.thymeleaf.spring5.processor.SpringInputGeneralFieldTagProcessor'
#RequestMapping("home")
public String newUser(Model model) {
User newUser = new User();
newUser.setId(100l);
newUser.setName("Test");
newUser.setEmail("test#what.com");
newUser.setPermission(0);
model.addAttribute(newUser);
return "index";
}
<form action="#" th:action="#{/home/add}" th:object="${newUser}" method="get">
<p>Id: <input type="text" th:field="*{name}" /></p>
<p>Message: <input type="text" th:field="*{email}" /></p>
<p>Message: <input type="text" th:field="*{permission}" /></p>
<p><input type="submit" value="Submit" /> <input type="reset" value="Reset" /></p>
</form>

I think the cause is you didn't create the getter method for the fileds: name, email, and permission. So, when the thymeleaf try to render the page, it cannot read the value of the name field, or email field, or permission field.

I was adding another model and it seems like it was causing issues, so I ended up doing this and everything is working fine.
The 'userList' model is used elsewhere in the code.
#GetMapping("home")
public String index(Model model) {
User user = new User();
model.addAttribute("user", user);
model.addAttribute("userList", userService.getUsers());
return "index";
}
<form th:object="${user}" th:action="#{/home/add}" method="get">
<p>Id: <input type="text" th:field="*{name}" /></p>
<p>Message: <input type="text" th:field="*{email}" /></p>
<p>Message: <input type="text" th:field="*{permission}" /></p>
<p><input type="submit" value="Submit" /> <input/></p>
</form>

maybe this will help someone...the issue is that you didn't provide the model that thymleaf will use to map your form input value. so you just have to pass as parameter "user model" like that .....
public String newUser(User model) {
}
.....thymleaf will use it to map your form field ...it work fine for me

Related

Spring accepting data from form getting 404 error

I am trying to take an input from a form made in a jsp called "start.jsp".
<body>
<form action=/addResults"method="post" modelAttribute="results" required="true">
<label for="email">Email Address:</label><br>
<input type="text" id="email" name="email" maxlength="50"required><br>
<label for="fullname">Full Name:</label><br>
<input type="text" id="fullname" name="fullname" maxlength="100"required>
<label for="age">Age (0-120):</label><br>
<input type="text" id="age" name="age" min="0" max="120"required>
<label for="gender">Gender:</label><br>
<input type="text" id="gender" name="gender" maxlength="45"required>
<input type="submit" value="Submit">
</form>
</p>
</body>
I'm then using code in my Controller to add this to a Class. Below is my controller code.
#RequestMapping("/addResults")
public String newHotel(Model model) {
model.addAttribute("results", new TestResults());
return "start";
}
But when I execute my Spring project it gives me an error of 404-not found. I've tried checking and rechecking my links and I can't understand how the links can't find the other pages etc. Any help would very great as I'm quite new to spring and the franework. Fred

Thymeleaf form action don‘t skip to

controller
#GetMapping("/form")
public ModelAndView createForm(Model model) {
model.addAttribute("user", new User());
model.addAttribute("title", "创建用户");
return new ModelAndView("users/form","userModel",model);
}
form
<form action="/users" th:action="#{/users}" method="POST" th:object="${userModel.user}">
<input type="hidden" name="id" th:value="*{id}">
name:
<input type="text" name="name" th:value="*{name}">
<br>
email:
<input type="text" name="email" th:value="*{email}">
<br>
<input type="submit" value="submit">
</form>
The form did not jump while I clicked submit.
It returned an error.
I want to know how to modify it here to make it right
After remove action="/users",rebuild the project,
submit the form,the error still there.
The action attribute appears twice. You need to remove action='/users' and keep the th:action
Also update your question with the stacktrace in the console when the error happens

Can't load page in Spring Boot using th:object

It contains a line error: "An error happened during template parsing (template: "class path resource [templates//register.html]")".
I can't open register html page to add new object into db. why?
<form th:action="#{/register-user}" th:object="user" method="post">
<div class="form-group">
<label for="email">Email:</label>
<input type="email" class="form-control" id="email" placeholder="Enter email" name="email">
</div>
<div class="form-group">
<label for="password">Password:</label>
<input type="password" class="form-control" id="password" placeholder="Enter password" name="pswd">
</div>
<div class="form-group form-check">
</div>
<button type="submit" class="btn btn-primary">Register</button>
</form>
My controller
#RequestMapping(path="/register-user", method = RequestMethod.POST)
public String registerNewUser(#Valid #ModelAttribute("user") User user){
userService.register(user);
return "redirect:/login";
}
Your "user" is a model attribute, hence, to access it (and use it as the th:object) you gotta use the ${...} syntax. The result would look like this:
<form th:action="#{/register-user}" th:object="${user}" method="post">

How to transfer data from view (twig file) to controller

I'm using Laravel 5. I have a function in my Controller, which use some of my params. This params I save in my view, while the user enters them in the input. And I need this data to transfer to my controller. Is it possible? Maybe anybody has had the same problem, I hope You can help.
Thank You!
Here is my controller:
$databaseLogin = new DatabaseController();
$dbRequest = $databaseLogin->loginReq($login, $passwd);
return view('login');
And here is my view:
<input class="form-control input-lg" type="text" id="login" name="login" placeholder="Login">
<input type="password" name="passwd" id="pass" autocomplete="off" class="form-control input-lg" placeholder="Password">
You can do if from post request
1.First create a route
Route::POST('your route’, 'yourController#add');
2.Then create a form with csrf token in view
<form action="{{url(‘your route’)}}" method="post">
{{csrf_field()}}
<input type=”text” name=”username”>
<input type=”text” name=”password”>
<input type=”submit” value=”login”>
</form>
3.Then you can get the values in your controller
Public function add(Request $request){
$name = $request.username;
$password= $request.password;
//code continues
}

Spring4 + Thymeleaf3 Form Validation : bean name #fields not available in templates

I get the below error in my spring4 + thymeleaf3 application when I try to show validation errors in my form template.
Neither BindingResult nor plain target object for bean name '#fields' available as request attribute
My form is as below.
<form th:action="#{/user/save}" method="post" th:object="${user}">
<ul th:if="${#fields.hasErrors()}">
<li th:each="err : ${#fields.errors('*')}" th:text="${err}"></li>
</ul>
<div>
<label>Name</label>
<div>
<input type="text" th:field="*{firstName}" placeholder="First Name">
<input type="text" th:field="*{lastName}" placeholder="Last Name">
<div th:if="${#fields.hasErrors('firstName')}" th:errors="${#fields.errors('firstName')}"></div>
<div th:if="${#fields.hasErrors('lastName')}" th:errors="${#fields.errors('lastName')}"></div>
</div>
</div>...
The form is rendered well for the following get request mapping.
#GetMapping("/create")
public String create(ModelMap model) {
model.put("user", new User());
return VIEW_DIR.concat("form");
}
But it gives the above error when the form is submitted with some invalid fields to the following method.
#PostMapping("/save")
public String save(#Valid User user, BindingResult bindingResult, ModelMap model) {
if(bindingResult.hasErrors()) {
return VIEW_DIR.concat("form");
}
userService.save(user);
return "redirect:list";
}
Can you please show me where the error is.
You are setting wrong values for th:errors inside your form element div. th:errors should contain field name. Update your form with this:
<div>
<input type="text" th:field="*{firstName}" placeholder="First Name">
<input type="text" th:field="*{lastName}" placeholder="Last Name">
<div th:if="${#fields.hasErrors('firstName')}" th:errors="*{firstName}"></div>
<div th:if="${#fields.hasErrors('lastName')}" th:errors="*{lastName}"></div>
</div>

Resources