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
Related
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
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
}
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>
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().
Until now, I have dealt with PUT and DELETE HTTP methods with jQuery AJAX (or XMLHttp) and HiddenHttpMethodFilter including GET and POST in Spring (currently I'm working with Spring 3.2.0). I have one general question about using these methods.
Basically, I want to perform basic database operations like INSERT, EDIT, UPDATE and UPDATE on the same page (JSP) using a single form. In this scenario, is it possible to trigger these methods at appropriate actions?
For example, when a user presses the delete button, the DELETE method should be raised, when he presses the update button, the PUT method should be triggered, on pressing the insert button, the POST method should be invoked and while retrieving data, the GET method should be used and accordingly an appropriate method which is mapped in the controller should be invoked (again on the same page with a single form <form:form>...</form:form>).
Of course, it is possible by means of AJAX but using AJAX everywhere is not appropriate.
Maybe I'm missing some important aspects of RESTFul APIs. Apparently, it is not possible (unknowingly). What is the actual answer?
EDIT:
Suppose, I wanted to deal with the PUT method then the Spring form would look like the following.
<form:form id="mainForm" name="mainForm" method="put" action="Temp.htm" commandName="tempBean" enctype="multipart/form-data">
<input type="submit" id="btnSubmit" name="btnSubmit" value="Submit"/>
</form:form>
and its generated HTML would look like the following.
<form id="mainForm" name="mainForm" action="Temp.htm" method="post" enctype="multipart/form-data">
<input type="hidden" name="_method" value="put"/>
<input type="submit" id="btnSubmit" name="btnSubmit" value="Submit"/>
</form>
The hidden <input type="hidden" name="_method" value="put"/> field is automatically added to be dealt with the PUT method by the HiddenHttpMethodFilter
If I now needed to raise the DELETE method then the form and its generated HTML should respectively be modified as follows.
<form:form id="mainForm" name="mainForm" method="delete" action="Temp.htm" commandName="tempBean" enctype="multipart/form-data">
<input type="submit" id="btnSubmit" name="btnSubmit" value="Submit"/>
</form:form>
Generated HTML
<form id="mainForm" name="mainForm" action="Temp.htm" method="post" enctype="multipart/form-data">
<input type="hidden" name="_method" value="delete"/>
<input type="submit" id="btnSubmit" name="btnSubmit" value="Submit"/>
</form>
How likely is it to do so dynamically at run time?
You could do something like:
<form id="mainForm" name="mainForm" action="Temp.htm" method="post" enctype="multipart/form-data">
<input type="hidden" name="_method" value="delete"/>
<input type="submit" name="btnUpdate" value="Update"/>
<input type="submit" name="btnDelete" value="Delete"/>
<input type="submit" name="btnCreate" value="Create"/>
</form>
Then on your processing page just check to see which button submitted the form.