tipsy / j2html

Java to HTML generator. Enjoy typesafe HTML generation.
https://j2html.com/
Apache License 2.0
765 stars 136 forks source link

How to solve encoding problem #216

Closed danieltoril closed 2 years ago

danieltoril commented 2 years ago

Hello, I'm writing a page using your library, it's amazing. I'm migrating my jsp's to your library, but i've a problem with encoding.

meta().withCharset("UTF-8") ... label("Configuración") encoding If i change charset to ISO-8859-1 it works fine, but my original jsp page was working with UTF-8. Thanks in advance!

rayrobdod commented 2 years ago

When calling new java.io.FileWriter (or new java.io.OuputStreamWriter or java.nio.file.Files.newBufferedWriter), the method has an optional java.nio.charset.Charset parameter, which has an unspecified default value but can be set to java.nio.charset.StandardCharsets.UTF_8 to tell the writer to encode the characters using UTF-8.

I don't think there is a straightforward way to use a explicit charset without specifying the charset independently in both the document's metadata and in the writer.

danieltoril commented 2 years ago

When calling new java.io.FileWriter (or new java.io.OuputStreamWriter or java.nio.file.Files.newBufferedWriter), the method has an optional java.nio.charset.Charset parameter, which has an unspecified default value but can be set to java.nio.charset.StandardCharsets.UTF_8 to tell the writer to encode the characters using UTF-8.

I don't think there is a straightforward way to use a explicit charset without specifying the charset independently in both the document's metadata and in the writer.

I'm render with HttpServletResponse.getWriter();

.render(FlatHtml.into(response.getWriter()))

How i can change the encoding of response writer?

danieltoril commented 2 years ago

I'm solved it, when adding:

response.setContentType("text/html");

previously to render to response