2

I'm using Thymeleaf 2.1.2.RELEASE with Spring MVC 4.0.4.RELEASE

I have a dynamic form that I use to add new order lines to an order.

The problem I am facing is that each time I add a line and the content is re-rendered then an extra symbol is added before the currency symbol on the price column of each of the previous lines.

So if I add three rows i get

  1. £22.00
  2. £22.00
  3. £22.00

The price field is BigDecimal with @NumberFormat(style = NumberFormat.Style.CURRENCY) so Spring should handle the conversion.

<div>
    <label th:text="#{order.lines}">Order Lines</label>
    <table id="addTable" dt:table="true" dt:sort="false" dt:paginate="false" dt:info="false" dt:lengthchange="false">
        <thead>
        <tr>
            <th th:text="#{order.lines.head.linenum}">line</th>
            <th th:text="#{order.lines.head.product}">Product</th>
            <th th:text="#{order.lines.head.description}">Description</th>
            <th th:text="#{order.lines.head.quantity}">Quantity</th>
            <th th:text="#{order.lines.head.price}">Price</th>
            <th>
                <button type="submit" name="addLine" th:text="#{order.line.add}">Add line</button>
            </th>
        </tr>
        </thead>
        <tbody>
        <tr th:each="line,lineStat : *{lines}">
            <td th:text="${lineStat.count}">1</td>

            <td>
                <input type="text" th:field="*{lines[__${lineStat.index}__].productIdentifier}"
                       th:errorclass="fieldError"/>
            </td>
            <td>
                <input type="text" th:field="*{lines[__${lineStat.index}__].description}"
                       th:errorclass="fieldError"/>
            </td>
            <td>
                <input type="text" th:field="*{lines[__${lineStat.index}__].quantity}"
                       th:errorclass="fieldError"/>
            </td>
            <td>
                <input type="text" th:field="*{{lines[__${lineStat.index}__].price}}"

                       th:errorclass="fieldError"/>
            </td>
            <td>
                <button type="submit" name="removeLine" th:value="${lineStat.index}"
                        th:text="#{order.line.remove}">Remove line
                </button>
            </td>
        </tr>
        </tbody>
    </table>
</div>

This is then backed by class with

public class OrderLine implements Serializable {
    @NotEmpty
    private String description;

    @NotNull
    @NumberFormat(style = NumberFormat.Style.CURRENCY)
    private BigDecimal price;

    @NotEmpty
    private String productIdentifier;

    @NotNull
    @Min(value = 1)
    private Integer quantity;

and then in my controller

@RequestMapping(value="/customer/orders", params={"addLine"})
public String addLine(final Order order, final BindingResult bindingResult) {
    order.getLines().add(new OrderLine());
    return "customer/orders";
}

The html page aleady includes

<meta http-equiv="Content-Type" content="text/html;charset=utf-8" />

and the character encoding servlet filter is set up as below

@Override     
protected Filter[] getServletFilters() {
    CharacterEncodingFilter characterEncodingFilter = new CharacterEncodingFilter();              
    characterEncodingFilter.setEncoding("UTF-8");         
    characterEncodingFilter.setForceEncoding(true);         
    return new Filter[] {characterEncodingFilter};     
}

Further to this from using Fiddler I can see that the response header to the dandelion datatables ajax request is incorrectly encoded as ISO-88591. I'm using datatables-thymeleaf 0.93 with datatables 1.9.4

From experimenting if I set thymeleaf encoding, the spring servlet filter and the html meta tag to ISO-88591 then the currency symbol appears correctly rendered although I would like this to work with UTF-8

Eventually I found an answer in this post CharacterEncodingFilter don't work together with Spring Security 3.2.0 provided by @Christian Nilsson. Basically I needed to force the character encoding filter to be registered using the onStartup method rather than the usual getServletFilters.

Gagravarr
  • 47,320
  • 10
  • 111
  • 156
Magnus Smith
  • 827
  • 2
  • 11
  • 28
  • Please check this... http://stackoverflow.com/questions/1461907/html-encoding-issues-character-showing-up-instead-of-nbsp – bgth Apr 05 '14 at 15:48
  • @bgth The page already includes utf-8 meta tag and the servlet filter for character encoding is set to utf-8 – Magnus Smith Apr 07 '14 at 12:23
  • 1
    is the response Header also being sent with content type as UTF-8? – bgth Apr 07 '14 at 12:30
  • @bgth following your advice I checked with Fiddler and the ajax response from datatables is being incorrctly encoded as ISO-88591 – Magnus Smith Apr 07 '14 at 15:10
  • Check the advice from digz6666. There is a special case in spring that the content type is set by the view. I will check and post the spring docs link later. http://stackoverflow.com/questions/3616359/who-sets-response-content-type-in-spring-mvc-responsebody/3617594#3617594 – bgth Apr 08 '14 at 05:29
  • 1
    Please check the below link which says (This filter can either apply its encoding if the request does not already specify an encoding, or enforce this filter's encoding in any case ("forceEncoding"="true"). In the latter case, the encoding will also be applied as default response encoding on Servlet 2.4+ containers (although this will usually be overridden by a full content type set in the view). ) http://docs.spring.io/spring/docs/4/api/org/springframework/web/filter/CharacterEncodingFilter.html – bgth Apr 08 '14 at 06:21

0 Answers0