Thymeleaf forms vs Spring MVC Forms? [closed] - spring

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 1 year ago.
Improve this question
Here are 2 different forms, one - with Spring MVC forms, another - with Thymeleaf. But what is the difference, which one should I use?
Spring Forms
<form:form methid="POST" action="/showDetails" modelAttribute="employee">
Name <form:input path="name"/>
Surname <form:input path="surname"/>
Salary <form:input path="salary"/>
<input type="submit" value="OK">
</form:form>
Thymeleaf
<form action="#" th:action="#{/register}" method="post" th:object="${user}">
<label>Full name:</label>
<input type="text" th:field="*{name}" /><br/>
<label>E-mail:</label>
<input type="text" th:field="*{email}" /><br/>
<label>Password:</label>
<input type="password" th:field="*{password}" /><br/>
<label>Birthday (yyyy-mm-dd):</label>
<input type="text" th:field="*{birthday}" /><br/>
<label>Gender:</label>
<input type="radio" th:field="*{gender}" value="Male" />Male
<input type="radio" th:field="*{gender}" value="Female" />Female<br/>
<button type="submit">Register</button>
</form>

This SpringMVC Form is similar to HTML.
In SpringMVC <form:form> tag plays an important role here; it’s very similar to the regular HTLM tag but the modelAttribute attribute is the key which specifies a name of the model object that backs this form:
<form:form method="POST"
action="/SpringMVC/addEmp" modelAttribute="emp">
This will correspond to the #ModelAttribute later on in the controller.
Whereas,
Thymeleaf is a Java template engine for processing and creating HTML, XML, JavaScript, CSS, and text.
You can read more about it :- thvsjsp

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

Avoiding field prepopulation in a thymeleaf form with Spring-Boot

I am having a trouble getting rid of the field prepopulation in a thymeleaf form and displaying text placeholders instead.
Here is the snippet of the form:
<form action="#" th:action="#{/catch/save}" th:object="${catch}" method="post">
<input type="hidden" th:field="*{id}">
<input type="text" th:field="*{species}" th:placeholder="Species"
class="form-control mb-4 col-3">
<input type="text" th:field="*{length}" th:placeholder="Length"
class="form-control mb-4 col-3">
<input type="text" th:field="*{weigth}" th:placeholder="Weigth"
class="form-control mb-4 col-3">
and here is the image of the resulting form:
Prepopulated form
My goal is to have the two fields prepopulated with "Length" and "Weigth". These fields correspond to int and double object fields, so if I understand correctly they are initiated with 0 when the bean is created and therefore so are th:fields when the form is loaded.
Could you help me find a way to solve this?
Thank you in advance

thymeleaf 3 form action URL with parameters and get method not working

I am using Thymeleaf 3 in Spring Boot 2 web app. Below is the form code:
<form data-th-action="#{/props/r(pg=3)}" method="get">
<div class="form-check form-check-inline">
<input class="form-check-input" type="checkbox" name="pt" id="p1" value="pr">
<label class="form-check-label" for="p1">P1</label>
</div>
<div class="form-check form-check-inline">
<input class="form-check-input" type="checkbox" name="pt" id="p2" value="pr2">
<label class="form-check-label" for="p2">P2</label>
</div>
<button type="submit" class=" mb-4">Search</button>
</form>
Unfortunately when I used method get for the form, it does not append ?pg=3 in the submitted URL, the URL looks like /props/r? if no checkbox is selected. If checkbox is selected the URL looks like /props/r?pt=p1
the pg=3 part is missing.
How to fix this problem?
The problem is that you have an action #{/props/r(pg=3)} -- which translates to /props/r?pg=3 and your form is also method get. If you have parameters in both the action and the body of the form (and usemethod="get"), browsers will not combine them. Instead, the parameters of the action are removed and replaced with the paramters in the body of the form.
This is why ?pg=3 is removed and replaced with the checkbox parameters. Either use post instead, or include pg as a hidden form element.
Instead of putting pg as parameter at the form url, consider putting it inside a hidden field like below.
<input type="hidden" name="pg" value="3">

What are spring.status.value|expression|errorMessages?

I'm new to Freemarker and am trying to incorporate it with Spring. I see references to spring.status.expression and spring.status.value and spring.status.errorMessages, but no where have I found a description of what they are.
I'm not sure if they're specific to forms, but that is where I find the references. Example:
<form action="" method="POST">
Name:
<#spring.bind "command.name" />
<input type="text"
name="${spring.status.expression}"
value="${spring.status.value?default("")}" /><br>
<#list spring.status.errorMessages as error> <b>${error}</b> <br> </#list>
<br>
...
<input type="submit" value="submit"/>
</form>
All help appreciated!
Thanks.
Les
Yes, these are related to forms and field errors. It's exactly like the Spring binding in JSP (just better!): https://docs.spring.io/spring/docs/4.3.x/spring-framework-reference/html/spring-tld.html#spring.tld.bind

Multiple password forms + password completion

On a website I'm working we have an onsite login and a private login, the problem I'm having is that Firefox doesn't seem to be able to differentiate between these login forms.
Does anybody know how I can make clear that these are different logins?
I already tried giving the form fields different names and ids, ex: onsite_login and login but without success.
edit: my form-tags are not being mixed up, they aren't even on the same page
The two forms on the different pages
<form method="post" action="/en/login/1">
<fieldset>
<p>
<input type="hidden" value="login" name="form"/>
<input type="hidden" value="en" name="redirect"/>
<label for="onsite_username">Username<abbr title="Required ">*</abbr></label>
<input type="text" class="input-text" maxlength="255" value="" name="onsite_username" id="onsite_username"/>
<label for="onsite_password">Password<abbr title="Required ">*</abbr></label>
<input type="password" class="input-password" maxlength="255" value="" name="onsite_password" id="onsite_password"/>
<input type="submit" value="Log in" name="submit" class="input-submit"/>
</p>
</fieldset>
</form>
and
<form method="post" action="">
<fieldset>
<input type="hidden" value="login" name="form"/>
<div>
<label for="username">Username</label>
<input type="text" class="input-text" value="" name="username" id="username"/>
</div><div>
<label for="password">Password</label>
<input type="password" class="input-password" value="" name="password" id="password"/>
</div>
<input type="submit" value="Aanmelden" class="input-submit"/>
</fieldset>
</form>
Apparently this is not possible due to the way Firefox stores its passwords.
A password-manager entry is stored with the following data
The username (encrypted and secured with Firefox Master Password).
The password (encrypted and secured with Firefox Master Password).
The hostname of the webpage containing the login form.
The hostname of the webpage to which the form data has been submitted.
Thus Firefox does not distinguish between the two loginfields on my page.
I've not yet heard about a multiple form problem in Firefox.
But it could be that Firefox mixed up your 2 login forms if there is another tag around that is not closed properly.
I've had that problem myself with <p> tags and a not properly closed <div> around it.
I'm not sure.. but try to give them a different ID like <form method="POST" action="#" id="login1">

Resources