I'm new at JSF projects. What I would like my project to display UTF-8 characters correctly. When I fill an inputText with UTF-8 characters and then I press commandButton, web site doesn't display it correctly. But I can insert a word that contains utf-8 into database. I use Apache Derby(JavaDB) and EclipseLink. How can I fix this issue ?
You could encode your HTTPHeader with UTF-8 in your servlet:
resource.setContentType ("text/html;charset=utf-8");
Also you could put this at the top of your page:
<%# page pageEncoding="UTF-8" %>
OR (The following within the header on your xHTML)
<meta http-equiv="content-type" content="text/html; charset=utf-8">
And also make sure your databases are encoding UTF-8:
ALTER DATABASE dbname DEFAULT CHARACTER SET utf8;
Related
Is there any way to setup Firefox and Chrome to work with escape=false attribute in h:outputText tag. When there is some html that needs to be shown in the browser, Firefox and Chrome show parsed string correctly, but any other links in application are freezed (??).
The example html from db:
<HEAD>
<BASE href="http://"><META content="text/html; charset=utf-8" http-equiv=Content-Type>
<LINK rel=stylesheet type=text/css href=""><META name=GENERATOR content="MSHTML 9.00.8112.16434">
</HEAD>
<BODY><FONT color=#000000 size=2 face="Segoe UI">läuft nicht</FONT></BODY>
Parsed HTML on the page:
läuft nicht
What is very weird, is that in IE everything works (usually it is opposite).
I use primefaces components (v2.2), .xhtml, tomcat 7 and JSF 2.0
You end up with syntactically invalid HTML this way:
<html>
<head></head>
<body>
<head></head>
<body>...</body>
</body>
</html>
This is not right. There can be only one <head> and <body>. The browsers will behave unspecified. You need to remove the entire <head> and the wrapping <body> from that HTML so that you end up with only
<FONT color=#000000 size=2 face="Segoe UI">läuft nicht</FONT>
You'd need to either update the DB to remove unnecessary HTML, or to use Jsoup to parse this piece out on a per-request basis something like as follows:
String bodyContent = Jsoup.parse(htmlFromDB).body().html();
// ...
Alternatively, you could also display it inside a HTML <iframe> instead with help of a servlet. E.g.
<iframe src="htmlFromDBServlet?id=123"></iframe>
Unrelated to the concrete problem:
Storing HTML in a DB is a terrible design.
If the HTML originates from user-controlled input, you've a huge XSS attack hole this way.
The <font> tag is deprecated since 1998.
It seems to me that you're trying to do something that JSF was not really meant to do. Rather than try to insert HTML in your web page, you ought to try having the links already on your page and modifying the "rendered" attribute through an AJAX call.
I am programming a Web-Application with Java EE and JSF.
I want to give alert (javascript) messages with Turkish characters (such as Ç,ü and ö etc.) on my app. But, after alert function is fired, I see the alert message with unrelated characters such as <?>, instead of Turkish characters on .xhtml pages on any browser (I tried it on IE10 and Chrome).
I need an advice to handle the problem. If I find a solution, I can also use this to set values of element (ex./h:commandButton) by javascript.
EDIT:
Additional information - Static strings that includes Turkish letters on html or jsf tags are shown properly. However, when I change them via some javascript functions, if there is any Turkish characters in strings that are changed, these Turkish letters cannot be shown properly.
It is too old question but I want to answer for maybe someone need.
If your problem with alert messages, open your javascript file on notepad and save as with Utf-8 options.
Or you can use same technic for others js file you need encoding char.
If its just specific to chrome may be you could try changing the page encoding.
Got Control->Tools->Encoding and change that to Auto Detect or Unicode or turkish etc..
If its a general issue then add the meta info
<META HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=windows-1254">
<META HTTP-EQUIV="Content-language" CONTENT="tr">
or
<META HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=iso-8859-9">
<META HTTP-EQUIV="Content-language" CONTENT="tr">
Example
<!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=windows-1254">
<META HTTP-EQUIV="Content-language" CONTENT="tr">
<title>Sample</title>
<script>
function alertText() {
alert(document.getElementById("turkish").innerHTML);
}
</script>
</head>
<body>
<div id="turkish">Türkçe olarak hellow dünya</div>
<input type="Button" onclick="alertText()" value="Say" />
</body>
</html>
I really need help for this because when I write my code in source view, I have no problem but when I view it in design view it shows square instead of farsi characters.
how can I solve this problem?
There is no problem here:
make sure you have saved the final file with utf-8 with signature (file menu-> advanced save options)
add <meta charset="utf-8" /> to the head of the html page or your master page.
also you need these meta tags too: <meta http-equiv="Content-Language" content="fa" /> and <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
Trying to make JSF web application multilingual. For that purpose cover all the HTML code with <f:view> tags:
<?xml version='1.0' encoding='UTF-8' ?>
<!DOCTYPE html>
<html xmlns= ... >
<f:view locale="#{actionsContainer.languageDetails.locale}">
<head>
<meta charset="utf-8" />
<f:loadBundle basename="messages.Messages" var="key" />
</head>
<body>
<h:outputText value="#{key.myText}" />
</body>
</f:view>
</html>
Than in resources I have several 'Messages.properties' files with a translations to some languages:
Messages_en.properties
Messages_es.properties
Messages.properties // default
The sample content of this files 'Messages_es.properties' is:
myText=España
Let's take, my locale is "es", and than Spanish translation file loads it's value and renders to the screen. However, the special characters (eg 'ñ') doesn't display properly in a browser output. Instead of special Spanish letters I am getting something like this "ó".
I have tried to save .properties file with UTF-8 encoding, without BOM. And the output has been changed to something like this "".
And so the question is how do I get this special any language letters in an output?!
Properties files are by default read using the ISO-8859-1 encoding. You need the JDK native2ascii tool to convert UTF-8 properties files to ISO-8859-1 properties files and then use those instead.
In JSF you can however also specify a custom ResourceBundle with a Control wherein you overridde the reading of properties files to use UTF-8 instead. See also this article.
when working with resource bundles, consider using ResourceBundle Editor plugin for eclipse (http://sourceforge.net/projects/eclipse-rbe/).
What is the solution to correctly display french characters in all browsers? now i get in some browsers this: (pr�sent�s)
Thanks.
One solution is to learn how to use Unicode.
The Absolute Minimum Every Software Developer Absolutely, Positively Must Know About Unicode and Character Sets (No Excuses!)
Try UTF-8 encoding on all your pages and encoding all your strings to UTF-8.
You should specify a content type for the page, and specify what encoding you use for creating the page. Example:
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
You need to select a character encoding for your page. In your editor, save to that encoding, and specify in the HTML <head> section:
<meta http-equiv="Content-Type" content="text/html; charset=The_Chosen_Encoding" />
Suitable encodings for French include ISO-8859-1 (aka Latin-1) and UTF-8.
Alternatively, you may stick to plain ASCII and use HTML entities for the accentuated letters, such as:
é for é
è for è
etc.
Just use this in you html
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
and if you're using php additionally this in your php-script
header ('Content-type: text/html; charset=utf-8');