aspose file tools*
The moose likes JSF and the fly likes Usage of inputTextarea in JSF Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » JSF
Bookmark "Usage of inputTextarea in JSF" Watch "Usage of inputTextarea in JSF" New topic
Author

Usage of inputTextarea in JSF

dwarakanathan thiru
Ranch Hand

Joined: Oct 14, 2009
Posts: 49
Hi,

I am new to JSF.

How to escape from "&" being converted into "& a m p ;" in the JSP when rendered. [NOTE: I am leaving space in amp intentionally so that is is displayed that way.. I know it is one of the 5 magic letters in xml ]

<h:inputTextarea cols="40" rows="5" value="#{bean.attrib1}" binding="#{bean.attrib2}"/>

I used & for unicode characters (to help with chinese, spanish etc..).
When they are displayed like " & # 3 6 8 2 5 ;" [again intentional ] , my JSP automatically converts it to the appropriate equivalent letter / charcter. But when they are displayed like "& a m p ;36825", my JSP does not decode the ASCII into normal character, instead it just display " & # 3 6 8 2 5;"

Hope I am clear. I have heard of ESCAPE attribs which needs to be set to FALSE. But in inputTextarea it is not available.

Any suggesstions would greatly help my learning in JSF tags.

Thanks!!


Thanks,
Dwarak T
Bart Kummel
author
Ranch Hand

Joined: Nov 30, 2007
Posts: 81
Hi,

Do I understand it right that your bean.attrib1 property contains Chinese characters? And you want to display those in a text area? Why don't you set the Chinese characters as Unicode characters in your attrib1 property? That way you don't have to worry about the &...

Best regards,
Bart


SCJP 1.4 | SCJD 1.6 | Visit my website | Author of the book Apache MyFaces 1.2 Web Application Development
dwarakanathan thiru
Ranch Hand

Joined: Oct 14, 2009
Posts: 49
Hi,

The attrib1 contains something like "& # 26412;". This is the codepoint of the chinese symbol, which is stored in a flat file.

When I push this to the JSP, it looks like this (NOTE: The chinese character you are seeing is actually & # 26412;)
<html>
<body>本</body>
</html>

But If I have the "&" replaced by "& amp ;" (NOTE: The & you are seeing is actually & a m p;)
<html>
<body>&#26412</body>
</html>


Hope this clears. I will have to maintain the UNICODED string in a flat file.

Thanks!!
Bart Kummel
author
Ranch Hand

Joined: Nov 30, 2007
Posts: 81
So you have a "flat file" with Unicode text, right?
What do you want to display in the text area? Do you want to see Chinese characters in your text area? (本) Or do you want to see Unicode code points? (&#26412;)
Tim Holloway
Saloon Keeper

Joined: Jun 25, 2001
Posts: 16305
    
  21

When you use a value reference in an inputText or inputTextArea control, you don't return HTML or XML from the "get" method of the value, you return a character string. The JSF framework will worry about any escaping you need. The "escape" method on outputText is so that you can override its normal verbatim text output and manually insert HTML display. Which is something best done carefully, and mostly in regards to stored HTML (I don't recommend generating HTML in program code - that's what "skinning" is for).

However, if you want to allow entry/display of non-ASCII characters in input controls, the "get" and "set" methods deal with java.lang.String objects, which, by definition are Unicode. What you need to do is ensure that the controls in question understand what character set (code page) those Unicode strings expects to use, which is part of the I18N settings for the page display.

Customer surveys are for companies who didn't pay proper attention to begin with.
dwarakanathan thiru
Ranch Hand

Joined: Oct 14, 2009
Posts: 49
Yes Bart!! I wanted to see the chinese characters in the text area. I can see it if the "&" was not DECODED into "& A M P ;".
Is there any solution to ensure the "&" was not converted as "& a m p;" in the JSF INPUTTEXTAREA?

Tim,
I am using codepointAt() method from String class to convert the actual chinese characters into a format ("& # 2345;") which will be stored in a flat file. And then I read the information ("& # 2345;") and put it in the TEXT AREA where there is no ESCAPE attrib like in OUTPUT TEXT. If these SPECIAL CODE POINTS are placed in between the HTML tags, they are good enough to tell the browser what characters they are. Thats why I went for this approach
dwarakanathan thiru
Ranch Hand

Joined: Oct 14, 2009
Posts: 49
To add on, I do not actually add HTML codes. I only put the CODE POINTS for example " & # 2 6 4 1 2 ;" (represents a chinese charcter) which the browser understands.
Tim Holloway
Saloon Keeper

Joined: Jun 25, 2001
Posts: 16305
    
  21

dwarakanathan thiru wrote:To add on, I do not actually add HTML codes. I only put the CODE POINTS for example " & # 2 6 4 1 2 ;" (represents a chinese charcter) which the browser understands.


I understand that, but inputTextArea converts to an HTML TEXTAREA control, and TEXTAREA doesn't work that way. It's not really that your browser "understands" your codepoint entities, so much as that you've got a font that can handle it for display purposes. To make it work for input, you need to actually truly tell your browser to understand what input codepage you're using.

We do have a fair number of people who do Asian webapps in this forum, and I keep hoping one of them will join it, since I'm out of practice on this kind of stuff. All I can really say is that there are mechanisms for it, even if I'm fuzzy on the details, and you'll have better results using those mechanisms than you will by trying to force the standard mechanisms to do what you want.
Bart Kummel
author
Ranch Hand

Joined: Nov 30, 2007
Posts: 81
Hi,

I get the feeling you are making things too complicated. If you have a Unicode String, why would you want to convert it to code points and then let the browser convert it to readable characters again? I think you should just pass the Unicode String to the text area directly. Then if you get the encodings right, you have your Chinese characters in your text area and you don't need the intermediate flat file.

Best regards,
Bart
dwarakanathan thiru
Ranch Hand

Joined: Oct 14, 2009
Posts: 49
Bart,
In the context of the application, our ADMIN user pastes the chinese words (Also any other words in russian, spanish) into the text area and submits it initially. I need to persist this some where. Thats why I use the text file / flat file. (Here i do codepointAt() and store the characters it as unicode)

This information is used by a different page to display the information pasted by the ADMIN user.
The flat file is needed bcos, the user might log off. I need the information from somewhere. Thats why I use a flat file.
dwarakanathan thiru
Ranch Hand

Joined: Oct 14, 2009
Posts: 49
Also, If i do not use code point, the chinese characters are stored as boxes or garbled chars in the file (verified by using WORDPAD since the WORDPAD supports the UTF-8 chars). And the same is displayed, which is not correct.

I hope I am keeping it easy to understand. Sorry If i am confusing.
Bart Kummel
author
Ranch Hand

Joined: Nov 30, 2007
Posts: 81
Okay, so you don't have a database and you use a text file for persistence. That all fine, but why do you call codepointAt()? Can't you just save the pasted text into the file "as is"? If you make sure that you use the right character encoding, this should be possible.

dwarakanathan thiru
Ranch Hand

Joined: Oct 14, 2009
Posts: 49
Bart, I guess you are correct !!

I have once used URLDecoder.decode and URLEncoder.encode methods. This might be handy.
Let me try this and keep you posted on this. Thanks a bunch !!
dwarakanathan thiru
Ranch Hand

Joined: Oct 14, 2009
Posts: 49
And the good news is, The most simplest solution always works good.

I have replaced the usage of codePointAt method with the following:
URLEncoder.encode(STRING, "UTF-8");

This means I do not need to worry abt "& A M P ;"

The encoding is pretty simple too

Thanks to Bart and Tim !!
 
jQuery in Action, 2nd edition
 
subject: Usage of inputTextarea in JSF