Accessing Tiles attribute as a variable from Thymeleaf template? - spring

I'm using Spring + Thymeleaf + Tiles combination.
I have a Tiles definition with a string attribute:
<definition name="myDef">
<put-attribute name="myAttr" value="foo"/>
</definition>
In a Thymeleaf template I want to use myAttr in expressions:
<div th:if="${myAttr == 'foo'}">...</div>
How whould I do it?
All I found so far is how to renger Tiles attributes directly to the output (<span tiles:string="myAttr" />), but wasn't able to figure out how to insert its content into a context variable.
Thank you.

I ended up doing a workaround - render Tiles attribute to a <span>, replace Thymeleaf conditions with JavaScript conditions (I'm using Angular), read that <span> value into a JS variable and let the client to do the rest.

Related

Which View implementation handles JSPs in Spring MVC?

In the SpringMVC documentation I see this for AbstractView:
Direct Known Subclasses:
AbstractExcelView, AbstractFeedView,
AbstractJackson2View, AbstractJExcelView,
AbstractPdfView, AbstractUrlBasedView,
AbstractXlsView, MarshallingView
Which implementation handles regular JSP Views?
The reason for my question is that I want to extend SpringMVC's JSP View, to support a Read-Only mode for a form. The regular view would be the normal JSP, but a Read-Only View would be an extension of the JSP where all fields are converted to labels, i.e. they can't be modified.
Any advice on this approach appreciated.
What I understand is you need to get a JSF component root, iterate over all elements, find input fields and replace them with non-input - labels instead?
JSP does not 'like' modifying it's components at runtime. In JSF I could suggest you implement a TagHandler to modify the component tree based on some parameter returned in the View Model.
In your case - a simple solution would be to either disable inputs based on parameter value
<h:inputText value="${inputValue}" disabled="${formDisabled}" />
or render different inputs based on parameter value
<c:if test="${formDisabled}">
<div><h:outputText value="${inputValue}" />
</c:if>
<c:if test="${!formDisabled}">
<h:inputText value="${inputValue}" />
</c:if>

how to avoid repetition in tiles definition in a Spring MVC project?

I'm starting with Spring MVC using JSP and Apache Tiles. I've learned that I can define views in a tile definition file like:
<definition name="index" extends="base.definition">
<put-attribute name="body" value="/WEB-INF/jsp/bodies/index.jsp" />
</definition>
However going forward like this, I'll need to repeat this simple pattern for every body template, replacing the two appearances of "index". Isn't there a way to avoid this repetition?
Tiles supports wildcards too. From the documentation here:
http://tiles.apache.org/framework/tutorial/advanced/wildcard.html
<definition name="bank/*" template="/layout.jsp">
<put-attribute name="body" value="/{1}.jsp"/>
</definition>
The {1} now refers to whatever the * matched in the view that was called. However, you may find it simpler to just keep the repetition.

Unable to use JSTL format taglib with Spring MVC form

I am changing some code from a home grown MVC to Spring 2.5 MVC. We have a form to edit an object, so I am using formBackingObject() in my controller to populate the form fields with the current values. In the old MVC, we used the JSTL fmt taglib to format date and money fields. This was nice because the formatting was in the presentation layer.
Now with Spring, the fields are populated correctly with formBackingObject(), but Spring doesn't recognize the the value attribute in the form:input element:
<%# taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %>
<form:form method="post" commandName="editProgramCommand" name="editTitleForm">
<fmt:formatNumber type="NUMBER" value="${program.price}" var="formattedPrice" minFractionDigits="2" />
<form:input path="price" id="price" value="${formattedPrice}" />
... other fields
</form:form>
Thoughts on how to properly format values in a Spring form? I'm not finding much on the web, so I figure its either a really simple syntax error, or I'm completely on the wrong track.
Spring form:input recognize the value of the input from its path attribute and not from the value attribute. If you see the spring form tld, there is no attribute value for the form input tag.
One way which i think is format the values in the back end and bring and set it in the front end.
Otherwise you can use the conventional spring:bind instead of spring form. Spring Bind Reference

Is it possible to add Html tags into Spring MVC3 I18N messages.properties file

Given the following MVC3 i18n use:
at a jsp file:
<s:message code="clickHere">
at message.properties file:
clickHere=Please click Here
a user's browser will display(and the word Here will be a link to http://abc.com):
Please click Here
Thanks
Did you try
<s:message code="clickHere" htmlEscape="false" />
In any case you use Thymeleaf, instead of th:text, you should use the th:utext.
Example below:
# Instead of this
<div th:text="#{message}">text to be replaced</div>
# Use this
<div th:utext="#{message}">text to be replaced</div>
You should always keep in mind that using the th:utext can cause XSS Attacks.
In the properties file, you will obviously have the following,
# In properties
message=Text to be displayed

spring validation: cleanest way to makeup accompanying labels of the validated input

I'm validating the input field that's bound to path. I'm using hibernate-validator 4 for this.
Now I'd like to highlight the age label so it pops out of the page (bold, red colour etc.).
However I'm wondering what the cleanest way to do this is.
<spring:hasBindErrors name="*"/> seems to be for the whole form object instead of for a specific field. Any input is appreciated.
Spring provides special jsp tags for forms, which support this task (highlighing in case of error):
For example this jsp
...
<%# taglib prefix='form' uri='http://www.springframework.org/tags/form'%>
...
<form:form method="post"
commandName="myCommand">
<form:input path="name"
cssClass="normalLayout"
cssErrorClass="normalLayout error"/>
<form:errors path="name"
cssClass="errorMessage"/>
</form:form>
...
In this case: the input field uses the css class "normalLayout" if every thing is ok, and the css classes "normalLayout" and "name" if there is a validation error for the field.
form:errors is to print the error message generated while validation.
#see http://static.springsource.org/spring/docs/3.0.x/spring-framework-reference/html/view.html#view-jsp-formtaglib

Resources