Thymeleaf if else [duplicate] - java

This question already has answers here:
How to do if-else in Thymeleaf?
(14 answers)
Closed 1 year ago.
I don't know how to wrote this kind of insruction using Thyeleaf.
I've got my view made with html.
<#if updateClient??>
<h2>Modifica del prodotto - ${updateClient.nome}</h2>
<div style="margin: 20px;">
<form method="POST" action="update" id="updateClient">
<input type="hidden" name="id" value="${updateClient.id}"/>
<div>
<label for="name">Name</label>
<input type="text" name="nome" id="nome" value="${updateClient.nome}" />
</div>
<div>
<input type="submit" name="invia" value="Update" />
</div>
</form>
</div>
<#else>
<h2>New Client</h2>
<div style="margin: 20px;">
<form method="POST" action="add" id="newDataClient">
<div>
<label for="nome">Nome</label>
<input type="text" name="nome" id="nome" value="" />
</div>
<div>
<input type="submit" name="invia" value="Add" />
</div>
</form>
</div>
</#if>
This was written using FreeMarker, but I need to use Thymeleaf.
I know simple Thymeleaf instructions, but I don't know how to do this. I read Thymeleaf instructions, and I find how to iterate a list of variables, but not how to create this structure using if and else. And if it's possible create something like this.

<div th:if="${updateClient.nome}">
<div>Edit Form</div>
</div>
<!-- ELSE -->
<div th:unless="${updateClient.nome}">
<div>New Form</div>
</div>

Related

Thymeleaf adding attribute to only the first item from the each loop

I'm trying to validate with parsley but by adding a data-parsley-min="1" to my input loop will add them to all of them.
For some reason it's not working now and I'm thinking it's because the multiple data-parsley-min="1" attributes.
Here's my code:
<form id="questionSubmitForm" data-parsley-validate="">
<div class="form-group">
<label class="font-weight-bold" for="prechecks">Pre-checks</label>
<div class="form-check" th:each="researchType : ${question.research}">
<input class="form-check-input" type="checkbox" name="prechecks[]" th:value="${researchType.value}" data-parsley-mincheck="1">
<label class="form-check-label" th:for="${researchType.value}" th:text="${researchType.key}"></label>
</div>
</div>
</form>
You can use th:attrappend (and the iteration status -- notice the , i in the th:each attribute) for this:
<form id="questionSubmitForm" data-parsley-validate="">
<div class="form-group">
<label class="font-weight-bold" for="prechecks">Pre-checks</label>
<div class="form-check" th:each="researchType, i : ${question.research}">
<input class="form-check-input" type="checkbox" name="prechecks[]" th:value="${researchType.value}" th:attrappend="data-parsley-mincheck=${i.index == 0 ? 1 : ''}" />
<label class="form-check-label" th:for="${researchType.value}" th:text="${researchType.key}"></label>
</div>
</div>
</form>

Retaining username in spring, when login fails

I have a jsp file with below code
<form:form modelAttribute="user" method="POST" name="loginForm" action="loginProcess" id="loginForm" autocomplete="off">
<input type='hidden' name='remember-me' value="true" />
<div id="login-container">
<spring:bind path="userName">
<c:if test="${status.error}">
<div id="formError" <span><form:errors path="userName" /></span></div>
</c:if>
</spring:bind>
<fieldset>
<ul id="login-form">
<li>
<label for="username"><spring:message code="login.UserName" /></label>
<input id="username" name="username" type="text" <spring:bind path="userName"> <c:if test="${not empty status.error}"> value='<%= (session.getAttribute(UsernamePasswordAuthenticationFilter.SPRING_SECURITY_FORM_PASSWORD_KEY) == null ? userName :session.getAttribute(UsernamePasswordAuthenticationFilter.SPRING_SECURITY_FORM_PASSWORD_KEY)) %>'</c:if> </spring:bind> />
</li>
<li>
<label for="password"><spring:message code="login.Password" /></label>
<input id="password" name="password" type="password" />
</li>
<li>
<div class="buContainer">
<a class="buOrange large" id="submit" href="#" onclick="validateForm()"><spring:message code="login.logIn" /></a>
</div>
</li>
</ul>
</fieldset>
<div class="buContainer">
<a id="forgot-link" href="forgotPassword"><spring:message code="login.forgotPassword" /></a>
</div>
</div>
</form:form>
Here when login fails i need to retain username.I tried
<form:form>
<form:input path="username" />
</form:form>
But i dont know where to use above code, Please help me on this.
We have this tag in our login.jsp, it seems to work OK:
<input id="j_username"
type="text"
name="j_username"
value="${sessionScope.SPRING_SECURITY_LAST_USERNAME}"
placeholder="Username"
class="form-control no-border">
The $sessionScope.SPRING_SECURITY_LAST_USERNAME seems to do the trick.

JSP enctype="multipart/form-data" get null data [duplicate]

This question already has answers here:
How can I upload files to a server using JSP/Servlet?
(14 answers)
Closed 7 years ago.
When I use enctype=“multipart/form-data” for upload and input many data get null, if delete enctype=“multipart/form-data” it can get data but upload null. My code html
<form action="createvendor" method="post" enctype="multipart/form-data">
<div>
<label> <input placeholder="Username:" type="text"
name="username" tabindex="1" >
</label>
</div>
<div>
<label> <input placeholder="Password" type="password"
name="password" tabindex="2" >
</label>
</div>
<div>
<label> <input placeholder="Firstname:" type="text"
name="firstname" tabindex="3" >
</label>
</div>
<div>
<label> <input placeholder="Lastname:" type="text"
name="lastname" tabindex="4" >
</label>
</div>
<div>
<label> <input placeholder="ID card:" type="text"
name="personalID" tabindex="5" maxlength="13"
onKeyUp="if(isNaN(this.value)){ alert('กรุณากรอกตัวเลข 13 หลักเท่านั้น'); this.value='';}"
>
</label>
</div>
<div class="registration_form">
<div style="position: relative;">
<a class='btn btn-primary'> Choose your ID card picture <input
required class="form-control" type="file"
style='position: absolute; z-index: 2; top: 0; left: 0; filter: alpha(opacity = 0); -ms-filter: "progid:DXImageTransform.Microsoft.Alpha(Opacity=0)"; opacity: 0; background-color: transparent; color: transparent;'
name="picturePersonalID"
onchange='$("#upload-file-info").html($(this).val());'>
</a> <span class='label label-info' id="upload-file-info"></span>
</div>
</div>
</form>
I want to upload and input many data and send data to servlet.
If you need in order to upload a file by which you intend to add later on, then you should put #MultipartConfig annotation on your servlet, so that request.getParameter() will work and that all uploaded files can be retrieved by request.getPart().#MultipartConfig(maxFileSize = 1073741824)
#MultipartConfig(maxFileSize = 1073741824) put this annotation on method

Spring Boot with Freemarker - form tags

I would like to use spring Boot MVC with Freemarker and display a form in a similar way to how it is done with JSP tags. E.g. this form:
<form:form method="post" action="save" modelAttribute="form" class="form-horizontal">
<form:hidden path="id"/>
<div class="form-group">
<label for="name" class="col-sm-2 control-label">Name</label>
<div class="col-sm-10">
<form:input id="name" path="name" class="form-control" />
</div>
</div>
</form:form>
Naturally, the tags form:form, form:input, form:hidden etc. are not supported. Is there a way to bind the model to the view in Freemarker?
Here it is:
http://docs.spring.io/spring/docs/current/spring-framework-reference/html/view.html#view-simple-binding
<!-- freemarker macros have to be imported into a namespace. We strongly
recommend sticking to spring -->
<#import "/spring.ftl" as spring />
<html>
...
<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>
...
</html>

getText() in WebDriver using Java

The following is the HTML code:
<div id="TITLE" class="text">
<label for="widget_polarisCommunityInput_113_title">Title</label>
<input type="text" name="field(TITLE)" id="widget_polarisCommunityInput_113_title">
<div class="error">This field must not be empty</div>
</div>
I tried to get the text "This field must not be empty" as follows by using WebDriver with Java:
driver.findElement(By.xpath("//div[#id='TITLE']/div")).getText();
driver.findElement(By.xpath("//div[#class='error']")).getText();
But, no text was retrieved. How can I do it? What's the wrong with my code?
Yes, there is a form. HTMl code including form as belows:
<form action="http://community.sandbox.no/content/save.do;jsessionid=F2BF733599D8D9F812B89ACBC20D37C5" method="post">
<div id="widget_polarisCommunityInput_113_leftcolumn">
<input type="hidden" value="article" name="articleType">
<input type="hidden" value="published" name="state">
<input type="hidden" value=" " name="successUrl">
<input type="hidden" value="" name="articleId">
<input type="hidden" value="http://sandbox.no/community/article/" name="redirect_url">
<input type="hidden" value="113" name="widget_id">
<div id="TITLE" class="text">
<label for="widget_polarisCommunityInput_113_title">Title</label>
<input type="text" name="field(TITLE)" id="widget_polarisCommunityInput_113_title">
<div class="error">This field must not be empty</div>
</div>
</form>
You can try as:
driver.findElement(By.xpath("//form/div[#id='widget_polarisCommunityInput_113_leftcolumn']/div[#id='TITLE']/div")).getText();

Categories