Thymeleaf th:href get other input field value? - spring-boot

Here is my form, with input field email & password
<form class="form-signin" th:action="#{/login}" method="post">
<input type="email" name="email" id="inputEmail" class="form-control" placeholder="Email address" required="required" autofocus="autofocus" />
<input type="password" name="password" id="inputPassword" class="form-control" placeholder="Password" required="required" />
<button class="btn btn-lg btn-primary btn-block btn-signin" type="submit">Sign in</button>
</form>
I want to try Thymeleaf th:href to send get request with parameter,
here is my code:
Forgot the password?
My Spring-Boot controller as below:
#RequestMapping(value = "/forgot_password", params = {"email!="}, method = RequestMethod.GET)
public #ResponseBody RespCommon forgotPassword(
#RequestParam(value = "email", required = true) String email) {
userService.forgotPassword(email);
return new RespCommon(ResultCode.SUCCESS, "Send forget password mail succeed");
}
But it seems not working, can some share how to get other input field value, thanks a lot!

Using a link (and hence a GET method) for restoring a password doesn't look as good idea for me. It's possible that browser or e-mail client will follow it, for example.
Instead I suggest to use a form (with a POST method) that looks like a link. You can put the e-mail in the hidden field and add btn btn-link classes to a submit button. To populate e-mail value, you can export value from the controller with Model.addAttribute().

Related

Why isn't the thymeleaf object recognized?

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

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
}

how to generate a HTTP rquest in a controller spring mvc

I have a form created with bootstrap and I use spring MVC. I don't use spring security here.
<form role="form">
<fieldset>
<div class="form-group">
<input class="form-control" placeholder="Username" name="name" type="text" autofocus>
</div>
<div class="form-group">
<input class="form-control" placeholder="Password" name="password" type="password">
</div>
Login
</fieldset>
</form>
I want to send the username and password to the welcome url. How can I do that.
Now, when I use #RequestParam("name") annotation, it gives an
Required String parameter 'name' is not present
error.
Set the 'action' attribute of the form to the url that you want to submit the form to.
The url can be absolute (http ://...) or relative (some/path), depending on where the page is hosted.
You may need to set the 'method' attribute on the form to POST , if the server expects a POST request.
You also want a submit button too

Resources