Page 1 of 2
Move from Thymeleaf
Posted: Tue Sep 11, 2012 9:17 am
by Valdus
I am struggling with disabling the templating system from broadleaf I'm just wanting to go with a standard apache-tiles system. The problem is I am not sure where to disable the thymeleaf templating agent.
Re: Move from Thymeleaf
Posted: Tue Sep 11, 2012 9:52 am
by Valdus
On and updated note, I have actually figured out I can use Thymeleaf but what I cannot figure out is how exactly do I handle adding a variable validation.
I unhid quantity and when I went to load the page it gave me a no object associated to form error.
Re: Move from Thymeleaf
Posted: Tue Sep 11, 2012 9:57 am
by jfridye
Thymeleaf is configured to be a view resolver used by Spring in applicationContext-servlet.xml with the following code:
Code: Select all
<!-- Set up the view resolver to be used by Spring -->
<bean class="org.broadleafcommerce.common.web.BroadleafThymeleafViewResolver">
<property name="templateEngine" ref="blWebTemplateEngine" />
<property name="order" value="1" />
<property name="cache" value="false" />
<property name="fullPageLayout" value="layout/fullPageLayout" />
<property name="characterEncoding" value="UTF-8" />
<property name="layoutMap">
<map>
<entry key="account/" value="layout/accountLayout" />
<entry key="catalog/" value="NONE" />
<entry key="checkout/" value="layout/checkoutLayout" />
<entry key="checkout/confirmation" value="layout/fullPageNoNavLayout" />
<entry key="layout/" value="NONE" />
</map>
</property>
</bean>
By commenting out or removing this block you will disable Thymeleaf, but you will need to configure a different view resolver for JSP, or whichever templating framework you use.
Re: Move from Thymeleaf
Posted: Tue Sep 11, 2012 10:01 am
by Valdus
My main issue at this point is when I disable javascript I cannot get thymeleaf to render an error message about the quantity because it is not a registered variable to the form. What I guess I am asking is how do I get it to see quantity as the registered variable.
Re: Move from Thymeleaf
Posted: Tue Sep 11, 2012 10:09 am
by jfridye
Valdus wrote:My main issue at this point is when I disable javascript I cannot get thymeleaf to render an error message about the quantity because it is not a registered variable to the form. What I guess I am asking is how do I get it to see quantity as the registered variable.
Can you post the form you are using here?
Each form has a form backing object, similar to Spring, but is declared with a th:object attribute in the form tag. This corresponds to some POJO in your application code. You can extend this POJO and add the quantity property (with getters and setters, of course) to the new POJO.
Re: Move from Thymeleaf
Posted: Tue Sep 11, 2012 10:11 am
by Valdus
Here is the form
Code: Select all
<blc:form method="POST" th:action="@{/cart/add}">
<div class="product-option-nonjs" th:each="productOption : *{productOptions}" th:object="${productOption}">
<div class="form50">
<label th:text="*{label}"></label>
<select th:name="${'itemAttributes[' + productOption.attributeName + ']'}">
<option th:each="optionValue : *{allowedValues}" th:object="${optionValue}" th:text="*{attributeValue}"></option>
</select>
</div>
</div>
<div th:utext="*{longDescription}" id="description"></div>
<div th:class="*{'productActions' + id}">
<div class="in_cart" th:classappend="${!cart.containsSku(#object.defaultSku)}? ' hidden'"><a class="fancycart fancybox.ajax big-button gray-button" th:href="@{/cart}">In Cart!</a></div>
<div class="add_to_cart" th:classappend="${cart.containsSku(#object.defaultSku)}? ' hidden'" th:object="${AddToCartItems}">
<input type="hidden" name="productId" th:value="*{id}" />
<input th:field="${quantity}" type="number" name="quantity" value="1" />
<span th:if="${#fields.hasErrors('quantity')}" th:errors="${quantity}">Invalid Quantity</span>
<input type="submit" class="addToCart big red" value="Buy Now!"/>
</div>
</div>
</blc:form>
A taglibs library for for you blc:form stuff would be awesome as well but I can't seem to find it.
Re: Move from Thymeleaf
Posted: Tue Sep 11, 2012 10:24 am
by jfridye
A taglibs library for for you blc:form stuff would be awesome as well but I can't seem to find it.
In Thymeleaf, these are known as Processors. All our custom processors can be found in
org.broadleafcommerce.core.web.processor.*.
The processor for <blc:form ...> is FormProcessor.
For more documentation on our use of Thymeleaf Processors, see
http://docs.broadleafcommerce.org/curre ... Layer.html
Re: Move from Thymeleaf
Posted: Tue Sep 11, 2012 10:30 am
by Valdus
One last question to which processor is quantity tied?
Re: Move from Thymeleaf
Posted: Tue Sep 11, 2012 2:02 pm
by jfridye
Valdus wrote:One last question to which processor is quantity tied?
Quantity is not tied to a processor. In Thymeleaf, the th:field attribute on an <input> must correspond to a field on a form-backing bean. The form-backing bean is declared with the th:object attribute on the <form> tag, not a <div> tag.
We suggest getting familiar with how Spring 3 and Thymeleaf work together. Thymeleaf provides some documentation here:
http://www.thymeleaf.org/thymeleafspring3.html. About forms specifically, there is a section in that PDF called 'Creating A Form' which is relevant here.
Re: Move from Thymeleaf
Posted: Wed Sep 12, 2012 8:13 am
by Valdus
I'm adding this here so that people have the same issue I did won't run into the same roadblock. I discovered that Thymeleaf does not do well if javascript is not enabled, as far as this framework is concerned that is something that should be looked into. But that isn't the point of this post, I putting the workaround here for you guys. You should probably add this to the documentation because you don't take into account validation without javascript, anyways here is what is needed.
Product.html
Code: Select all
<span class="error" th:if="${#httpServletRequest.getParameter('quantityError')}">Invalid Quantity</span>
<input type="number" th:value="1" name="quantity"/>
CartController.java the add function with no ajax
Code: Select all
@RequestMapping(value = "/add", produces = "text/html")
public String add(HttpServletRequest request, HttpServletResponse response, Model model,
@ModelAttribute("addToCartItem") AddToCartItem addToCartItem, BindingResult results) throws IOException, PricingException, AddToCartException {
if(results.hasFieldErrors("quantity")){
Product product = catalogService.findProductById(addToCartItem.getProductId());
model.addAttribute("quantityError", "invalid");
return "redirect:" + product.getUrl();
}
try {
return super.add(request, response, model, addToCartItem);
} catch (AddToCartException e) {
Product product = catalogService.findProductById(addToCartItem.getProductId());
return "redirect:" + product.getUrl();
}
}
Hope this helps anyone else who has similar issues.