I am working on a spring boot application and trying to convert jsp to thymeleaf. I am stuck converting the following jsp code to thymeleaf: I do not know how to convert the <c:import> part.
<c:forEach items="${requestScope.users}" var="user" varStatus="status">
<div <c:if test="${!status.last}"></c:if>>
<c:import url="/user/checkUser">
<c:param name="username" value="${user.username}" />
<c:param name="firstName" value="${user.firstName}" />
<c:param name="lastName" value="${user.lastName}" />
</c:import>
</div>
</c:forEach>
There is no direct replacement in Thymeleaf. The best way is to create a fragement that has the HTML that lives at the /user/checkUser URL.
For instance, create src/main/resources/templates/fragments.html:
<div th:fragment="show-user-info(username, firstName, lastName)">
<div th:text="${username}"></div>
<div th:text="${firstName}"></div>
<div th:text="${lastName}"></div>
</div>
Now, you can use the fragment:
<div th:each="user : ${users}">
<th:block th:if="${!status.last}">
<div th:replace="fragments :: show-user-info(${user.username},${user.firstName},${user.lastName})"></div>
</th:block>
</div>
Related
So I have a form created with Spring MVC tags and it looks like this
<form:form action="/languages" method="post" modelAttribute="language">
<p>
<form:label path="name">Name</form:label>
<form:errors path="name"/>
<form:input type="text" path="name"/>
</p>
<p>
<form:label path="creator">Creator</form:label>
<form:errors path="creator"/>
<form:input type="text" path="creator"/>
</p>
<p>
<form:label path="currentVersion">Version</form:label>
<form:errors path="currentVersion"/>
<form:input type="number" path="currentVersion"/>
</p>
<input type="submit" value="Submit"/>
</form:form>
The problem is that it is only showing the labels for the form and not the input where a user can enter things. Am I missing something here? I'm pretty new to Spring boot and Spring MVC tags so im not sure what's the problem.
I figured it out. I forgot to add
<%# taglib prefix="form" uri="http://www.springframework.org/tags/form"%>
to the jsp file.
I've been working on an example on conditional rendering from Spring in Action 4.
The code looks this way:
<sec:authorize access="hasRole('ROLE_SPITTER')">
<s:url value="/spittles" var="spittle_url" />
<sf:form modelAttribute="spittle" action="${spittle_url}">`
<sf:label path="text"><s:message code="label.spittle" text="Enter spittle:"/> </sf:label>
<sf:textarea path="text" rows="2" cols="40" />
<sf:errors path="text" />
<br/>
<div class="spitItSubmitIt">
<input type="submit" value="Spit it!" class="status-btn round-btn disabled" />
</div>
</sf:form>
</sec:authorize>
The question is what does the code attribute, namely label.spittle refers to? Does it refer to the modelAttribute from the form? Or does it mean that there should be message bundle with key label.spittle?
It means a key in a resource bundle (documentation):
<s:message code="label.spittle" text="Enter spittle:"/>
Im trynig to create a simple multiselect box but for some reason its not visible properly.
Here is my code:-
HTML
<input type="text" id="addRow" />
<input type="button" id="btn" value="Add" />
<form id="form1">
<div style="padding:20px">
<select id="chkveg" multiple="multiple"></select>
<br />
<br />
</div>
</form>
https://jsfiddle.net/04Lgnkqs/
After creating the select you should call the plugin on your select. something like this
$('#chkveg').multiselect();
Refer here
How to call two submit buttons in spring form,
one button calls j_spring_security_check and another button one calling controller method.
following is login form
<form action="../../j_spring_security_check" method="post" >
<div class="right">
<ul>
<li>
<spring:message code="label.userName" text="UserName" />
<input id="j_username" name="j_username" type="text" />
<form:errors path="firstName" cssclass="error"> </form:errors>
</li>
<li>
<spring:message code="label.password" text="Password" />
<input id="j_password" name="j_password" type="password" />
<form:errors path="password" cssclass="error"></form:errors>
</li>
<li>
<input type="submit" value="Login" />
</li>
</ul>
</div>
</form>
<form action="/portal/main/resetForgetPassword" method="POST">
<input type="submit" value="ForgetPassword">
</form>
thanks advance
I guess your problem is that for every URL it is going to Security form of spring.
So you need to permit for some url so that it should not go to j_spring_security_check
You can do this in the security.xml (whatever may be the name of that file).
<intercept-url pattern="/portal/main/forgetPassword*" access="permitAll" />
<intercept-url pattern="/*" access="hasRole('ROLE_ADMIN_ASSUMED')">
And also in your html code
Forget Password
public class Parent {
private Integer Id;
private String name;
private String age;
private List<Childrens> childrens;
// getters and setters
}
My JSP classs:
<spring:bind path="parent.*">
<c:if test="${not empty status.errorMessages}">
<div class="error">
<c:forEach var="error" items="${status.errorMessages}">
<img src="<c:url value="/images/iconWarning.gif"/>"
alt="<fmt:message key="icon.warning"/>" class="icon" />
<c:out value="${error}" escapeXml="false" />
<br />
</c:forEach>
</div>
</c:if>
<form:form commandName="parent" method="post"
action="parent .html" onsubmit="return onFormSubmit(this)"
id="parentForm">
<appfuse:label styleClass="desc" key="name" />
<form:errors path="name" cssClass="fieldError" />
<form:input path="name" id="name" cssClass="text medium"
readonly="true" cssErrorClass="text medium error" />
<appfuse:label styleClass="desc" key="age" />
<form:errors path="age" cssClass="fieldError" />
<form:input path="age" id="cage" cssClass="text medium"
readonly="true" cssErrorClass="text medium error" />
I want show list of Childrens as a table. How can I do it? A list inside form page.
Probably your best bet is to use the built-in jstl tag lib (which you already) to declare the bean and get the list of children and iterate over them with "forEach".
Using spring form tag doesn't prevent you from doing this as far as I know.