Facelets page with special characters causes MalformedByteSequenceException: Invalid byte 2 of 3-byte UTF-8 sequence at UTF8Reader.invalidByte - eclipse

I am merging an old and some new stuff into a webapplication. However when using swedish letters the page will fail. It does not seem to be a server issues since the old .jsp pages will load correctly.
What am I missing in the xhtml header?
mar 25, 2015 11:50:53 FM org.apache.catalina.core.StandardWrapperValve invoke
SEVERE: Servlet.service() for servlet [Faces Servlet] in context with path [/BowlingInfo] threw exception
com.sun.org.apache.xerces.internal.impl.io.MalformedByteSequenceException: Invalid byte 2 of 3-byte UTF-8 sequence.
at com.sun.org.apache.xerces.internal.impl.io.UTF8Reader.invalidByte(UTF8Reader.java:691)
<!DOCTYPE html>
<html lang="sv-SE"
xmlns="http://www.w3c.org/1999/xhtml"
xmlns:h="http://xmlns.jcp.org/jsf/html"
xmlns:f="http://java.sun.com/jsf/core"
xmlns:p="http://primefaces.org/ui"
xmlns:c="http://java.sun.com/jsp/jstl/core">
<h:head>
<link rel="stylesheet" href="bowling-style.css" />
<meta http-equiv="content-type" content="text/html" charset="ISO-8859-1" />
</h:head>
<h:body>
<!-- FAIL -->
<h1>Hallmästaren</h1>
</h:body>
</html>
Example of old page that will work
<%# page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%# taglib prefix="f" uri="http://java.sun.com/jsf/core"%>
<%# taglib prefix="h" uri="http://java.sun.com/jsf/html"%>
<%# taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Svalövs bowlinghall</title>
<script type="text/JavaScript">
<!--
var currentTime = new Date()
function AutoRefresh( t ) {
setTimeout("location.reload(true);", t);
}
function GetServerDate() {
var date = new Date();
dateNow = date;
document.write(dateNow);
return dateNow;
}
</script>
<link rel="stylesheet" type="text/css" href="bowling-style.css" />
</head>
<body onload="JavaScript:AutoRefresh(15000);" bgcolor="C2F2BD">
<f:view>
.........

Facelets uses by default UTF-8 encoding (as part of World Domination). You should be configuring all editors and layers to use UTF-8.
In your particular case, there are at least two probable causes:
Eclipse should via Window > Preferences > General > Workspace > Text File Encoding be configured to use UTF-8 to save files.
The HTTP/HTML Content-Type header should be specifying charset=UTF-8, exactly as you had in your JSP which you for some reason changed to the legacy ISO-8859-1 encoding.
<meta http-equiv="Content-Type" content="text/html;charset=UTF-8" />

Related

How to include <meta charSet="utf-8" /> in react-testing-library?

I'm using jest-image-snapshot with react-testing-library so that I can run some visual regression tests. The issue I'm having is that I need to define <meta charSet="UTF-8" /> (otherwise my image-snapshots don't render correctly), however react-testing-library strips out all <html>, <head> and <body> tags.
If I wrap my component like so:
<html>
<head>
<meta charSet="utf-8" />
</head>
<body>
<ComponentToTest />
</body>
</html>
Then it ends up rendering as:
<DocumentFragment>
<meta charset="UTF-8" />
<ComponentToTest />
</DocumentFragment>
Which of course doesn't set the encoding to UTF-8 as it's not within <head>.
The result of this is that any components that include a unicode character will render with a weird character instead.
For example, using ​ (zero width space) will render as â€<. This results in my image-snapshots looking like this: https://i.imgur.com/fSK5gJ0.png
Is there any workaround here to define the character encoding?
EDIT: It seems I can change the container element (https://testing-library.com/docs/react-testing-library/api#container), but I can't define the container as { container: document.documentElement } as apparently it needs an .appendChild() to work. Is it possible to define the container element as the <html> itself? That would solve my issue if it's possible.

Displaying XML using google-code-prettify

I can't seem to get google prettify to work with basic XML: anyone got this to work, or can see what I am doing wrong: here is my code:
<html>
<head>
<meta charset="utf-8" />
<script src="https://google-code-prettify.googlecode.com/svn/loader/run_prettify.js?autoload=true&skin=sunburst&lang=xml"></script>
</head>
<body>
<h1>XML Output</h1>
<pre class="prettyprint" id="quine" style="border:4px solid #88c">
<Rest_appt_pull licenseKey="123" passWord="456" start="30-oct-2014 00:00:00" finish="31-oct-2014 23:59:59" p_method="event">
<timings>
<entry label="Read"
time=".03"
segment=".03" />
<entry label="Processing XML"
time=".04"
segment=".01" />
</timings>
</Rest_appt_pull>
</pre>
</body>
</html>
Any help would be gratefully appreciated
The root cause is html tag symbol.
Change < to <
Change > to >
You need to html encode your xml code example first. You can use one of available online tools to do that. Then just wrap it in pre/code
<pre class="prettyprint lang-xml"> ... your html encoded xml code ... </pre>
and attach js code on the site.

declaration regarding the content variable

I am new playwork. I have been going through the tutorials and samples provided the playframework.
I could successfully render helloworld application provided by playframework samples.
I have few doubts regarding the rendering part of main.scala.html.#
This is the default program which I got from samples/helloworld
#(title: String)(content: Html)
<!DOCTYPE html>
<html>
<head>
<title>#title</title>
<link rel="stylesheet" media="screen" href="#routes.Assets.at("stylesheets/main.css")">
<link rel="shortcut icon" type="image/png" href="#routes.Assets.at("images/favicon.png")">
<script src="#routes.Assets.at("javascripts/jquery-1.6.4.min.js")" type="text/javascript"></script>
</head>
<body>
<header>
#title
</header>
<section>
#content
</section>
</body>
</html>
Here when I commented out the #content under section tag , I am not able to see the the fields.
Now my question is, where is #content is mapped to the Form field?
I created another structure for my layout and added the #content to the content section. but it does not fit into that
so now my question is #content where is that defined that it is div container and has got some height and weight and all?
I could not understand. Please help me.
Pleae find my customized code below
#(title: String)(content: Html)
<!DOCTYPE html>
<html>
<head>
<title>#title</title>
<link rel="stylesheet" media="screen" href="#routes.Assets.at("stylesheets/main.css")">
<link rel="shortcut icon" type="image/png" href="#routes.Assets.at("images/favicon.png")">
<script src="#routes.Assets.at("javascripts/jquery-1.7.1.min.js")" type="text/javascript"></script>
</head>
<body>
<div id="container" style="width:500px">
<div id="header" style="background-color:#FFA500;">
<h1 style="margin-bottom:0;">Main Title of Web Page</h1></div>
<div id="menu" style="background-color:#FFD700;height:200px;width:100px;float:left;">
<b>Menu</b><br>
HTML<br>
CSS<br>
JavaScript</div>
<div id="content" style="background-color:#EEEEEE;height:200px;width:400px;float:left;">
#content</div>
<div id="footer" style="background-color:#FFA500;clear:both;text-align:center;">
Copyright © W3Schools.com</div>
</body>
</html>
Introduction
From Template parameters in the Play Template documentation, the meaning of the first line is described. Here, we see that two parameter groups are required.
The two parameter groups are:
a String parameter containing the title
a Html parameter containing some HTML content
Usage
To use this template, two parameter groups have to be supplied. In the context of the helloworld application, it is called from app/views/index.scala.html like this:
#main(title = "The 'helloworld' application") {
<h1>Configure your 'Hello world':</h1>
... more HTML elided
}
This pattern is described in http://www.playframework.com/documentation/2.2.x/ScalaTemplateUseCases, where HTML is injected into a template.
main.scala.html contains the template (content contains the HTML to be injected).
index.scala.html contains an example of injection into this template.
Note that calling #main(...) calls the template that is defined in main.scala.html.Similarly, calling #my_template(...) would call the template defined in my_template.scala.html.
In this case, the HTML for the form is defined inside index.scala.html.
Calling the Template
Finally, the root template is called from a controller. For the helloworld application, the template defined in index.scala.html is invoked by the code
def index = Action {
Ok(html.index(helloForm))
}
This is where the form object is injected into the template.

<jsp:root and <html xmlns

In a JSP, do I need to provide a jsp:root directive and an XML namespace declaration. Or only the later. That is, if I have the following:
<jsp:directive.page language="java"
contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"
/>
<jsp:root xmlns:c="http://java.sun.com/jsp/jstl/core"
xmlns:s="http://www.springframework.org/tags"
/>
<!DOCTYPE html>
<html xmlns:c="http://java.sun.com/jsp/jstl/core"
xmlns:s="http://www.springframework.org/tags">
<head>
... remainder of my HTML page
should I remove the jsp:root element? The information seems redundant. Removing the namespace declaration from the html element makes Eclipse complain.
You need the jsp:root element to set up the rest of the document to understand tags beginning with s: or c:. So don't remove it.

Can JSTL transform XML encoded in UTF-8?

I am making a simple JSP application to transform XML data into HTML.
I use JSTL and my XML data is encoded in UTF-8. It works, but the danish characters look strange in the browser.
Like this:
Danish characters written directly in jsp: ÆØÅ æøå
Same danish characters transformed with jstl:
character: Æ character: æ
character: Ø character: ø
character: Å character: å
However, if I manually change the xml definition like so:
<?xml version="1.0" encoding="ISO-8859-1" ?>
The output is transformed properly.
Should I set up JSTL in some way to handle UTF-8, or is it,that my file is actually latin1 encoded by mistake? I do not know how to check this...
Here is my test xml file:
<?xml version="1.0" encoding="UTF-8" ?>
<rows>
<row>
<name>character: Æ</name>
<surname>character: æ</surname>
</row>
<row>
<name>character: Ø</name>
<surname>character: ø</surname>
</row>
<row>
<name>character: Å</name>
<surname>character: å</surname>
</row>
</rows>
Here is my xsl:
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="2.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/">
<table border="0">
<xsl:for-each select="rows/row">
<tr>
<td>
<xsl:value-of select="name" />
</td>
<td>
<xsl:value-of select="surname" />
</td>
</tr>
</xsl:for-each>
</table>
</xsl:template>
</xsl:stylesheet>
My index.jsp:
<?xml version="1.0" encoding="UTF-8" ?>
<%# page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%# taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<%# taglib prefix="x" uri="http://java.sun.com/jsp/jstl/xml"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org /TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>Insert title here</title>
</head>
<body>
Written directly in jsp: ÆØÅ æøå
<h3>xml transformed with jstl:</h3>
<c:import url="/Test.xsl" var="xsltdoc" />
<c:import url="/Test.xml" var="xmldoc" />
<x:transform xml="${xmldoc}" xslt="${xsltdoc}" />
</body>
</html>
I am using JSTL libraries (Implementation-Version: 1.2) on JBOSS AP 4.2.3.
Ok, I checked the encoding of my xml data here, and it is correct, that it is UTF-8 encoded.
Apparently, in index.jsp JSTL must be set to use UTF-8 like so
<c:import url="/Metadata1.xsl" var="xsltdoc" charEncoding="UTF-8" />
<c:import url="/Metadata1.xml" var="xmldoc" charEncoding="UTF-8" />
This solves my problem.