How to detect client locale in JSF application? - java

How can I use ServletRequest.getLocale() in JSF application, when Servlet is absent in my code and is provided by JSF implementation? I'm trying to use ServletContextListener, but is it possible to reach ServletRequest from ServletContextEvent?
Anyway, what is the correct approach?

FacesContext.getCurrentInstance().getExternalContext().getRequestLocale();
?

The correct way in my opinion is to provide the links on your home page for various locales your application needs to support. This is less intrusive for the end user. Otherwise the end user has to keep changing the locales browser specific way. Also different browsers send the locale different ways to the server which is also problematic depending on your usecase.
I strongly suggest you go through this tutorial.

Related

How to replace struts with ext.js 6?

We want to remove struts and introduce ExtJS. I have searched on internet and with whatever limited knowledge I have about ExtJS, I came to know that we can use ext direct
but, I don't think it will be allowed in the project.
We also don't want to go with the old route of directly interacting with java servlets.
My question may appear vague but, it is due to the limited knowledge I have on this subject.
I would like to know, should we go with Spring mvc or is there any better alternative?

Integrating Javascript Frameworks with Java Web Frameworks

I have what I think might be a strange question to ask.
Recently I was playing with some java web frameworks (jsf with primefaces) and I noticed that it's quite good at handling form data or when you play along with the jsf components. I also did a project using grails and again it was useful for form data.
But the moment you want to do something which requires a little deviation; then I found myself doing weird things (examples are tag clouds with large strength values using primefaces and single page webapp forms with grails).
This resulted in some very messy html+javascript code for my grails things(which I blame on my inexperience with javascript). This was done because I was using expression language (grails in this case) to populate some of the javascript. The worst part was that I had a bunch of custom javascript code inline with my server pages (I could of refactored a little out but I think one would still have alot of javascript calls inside a single page).
So now to the question(s) :)
[main question] Are there any resources that demonstrates how to design or at least implement maintainable javascript with server pages (gsp, xhtml, etc) ?
The reason is that I find that there are some neat javascript frameworks, but using them with server pages seems a bit unnatural if one takes into account the expression languages for server side frameworks? Unless it's normal to do this type of thing :) ?
Are there appropriate frameworks for Single page webapps using java?
My current answer is that GWT, Vaadin (based off GWT) and perhaps JavaFX qualify. Maybe ZK, Flex and (Grails/Roo + Flex/GWT) also qualify as well?
Are java web frameworks still useful for presentation layers?
My current answer is that they might be when you are dealing with portal type webapps or Web 1.0 apps for lack of a better word. The other case is that they could be when you use alot of role based security and you want to filter things out based on roles, but even that has counter arguments.
My other answer to this is that it might be better to use a java server to provide your web services and then rely on something else to do the front end?
This might be OP, but for single page apps, it's easier to serve up data from the server via JSON(P)/XML/whatever and then use things like Backbone to process and modify the view(s) accordingly. That way, the server can use REST/SOAP/websockets/whatever, and the browser app becomes a full-fledged app.
I haven't actually done this, so don't quote me, but I imagine that it should make sense.

GWT: Loading different UI's based on URL

Trying to get a GWT project off the ground and finding it difficult to do any basic routing.
Trying to fire up different UI's based on the URL. Thought I could set a string based on the getHash() and then switch off that, but seems cumbersome (and annoying since I can't do string-based switches in Java).
There's got to be a best practice for this. I know Gerrit uses hashes for determining this type of information but couldn't find where they do it in the source.
Or is this totally not GWT-related? Something I can handle in web.xml?
Any help is much appreciated.
If you want to load different UIs by using different URLs, instead of doing it directly from client side (gwt code), one way I can think of is to separate your application in several modules and have different host pages (dynamically generated from server side) load each module.
Not sure it's the best way, but I guess it depends on what type of application you're building.
So far, in GWT I only built desktop like applications so... once I loaded the GWT app I didn't cared about URL's and stuff.
Url's can be loaded using a "Place" manager. Support for this is handled through GWT presenter:
http://code.google.com/p/gwt-presenter/
import net.customware.gwt.presenter.client.place.PlaceManager

2 ways to get page url in java applets, what's the difference and/or benefits of each?

the two ways that I know to get the page url are:
1.through the applet class:
Applet.getDocumentBase()
2.through the netscape javascript library:
JSObject.getWindow(this).eval("location.href")
First, what are the differences between those two methods and advantages of each,
Secondly, can users trick the url of those methods in someway? to make the applet think it's embeded in http://www.stackoverflow.com/index.html for example?
If you look at the code for Applet.getDocumentBase() (sun's implementation of AppletStub), you'll see that it is also getting the location using JSObject. But it has a lot of additional code around it, and is guaranteed to work across all browsers and setups. So better use Applet.getDocumentBase()
The answer is easier than you think.
You should use Applet.getDocumentBase in case the implementation varies between platforms or JVMs, or is altered at a later date. Just because JSObject is the way that Sun's JVM gets this value doesn't mean that, say, IBM's JVM works that way.
Pretty sure users could spoof it:
Download your .jar and host on a server controlled by the user
Change their etc/hosts file so that stackoverflow.com points to the IP of their server
Configure the server to serve requests for stackoverflow.com
Visit the page on the server with the applet embedded
And the applet will believe it's on stackoverflow.com. There is probably a simpler way to spoof it though!
JSObject is buggy in some browsers (IE and Safari especially). Applet.getDocumentBase() is a lot safer.

java internationalization in local languages

I am staying India(has more than 20 languages) I am trying to implement java-internationalization in the website.
But in Locale.getAvailableLocales() the local languages are not available. is it possible to implement internationalization in my case?
If possible what will happen to the fonts? How am i going to load the proper fonts in the client browser?
Which framework to implement Struts or Spring?
I can't claim to be an expert in Java internationalization - all of my work which cared about localization dates back to when we had to do all that by hand.
If you don't want to do that (which is no harder now than it was back then), it appears that you can still make use of Java's automated I18N services.
It appears that in order to support languages, number formats, etc, which are for locales not yet supported, you'll have to create a Locale-sensitive service provider, which got much easier in 6
There's what looks like a good tutorial here.
I think you are asking multiple questions here, and they are orthogonal to each other.
For the Locale.getAvailableLocales() question. If the built-in locales are not enough, you can simply create more. The locale object just represent the locales that your program knows, it has nothing to do with you the system performs --- more specifically, no corresponding locale object does not mean you cannot support the locale.
For the font, typically you will use UTF-8 for a i18n site, so as long as your content type and charset is set properly it should render correctly in the client browser.
The framework question really depends on what you are trying to accomplish, and what do you want to gain from it.
I believe there are more locales available when using a JDK than a JRE.
Locale.getAvailableLocales() returns a list of locales whose LocalData is installed in sun.text.resources package of JRE.
This list may not matter to you. Currently, the LocalData only contains local number and date formatting information. If you don't have any special format, you can just pick a locale whose format is the same as yours, like "en_IN".
Charset is another issue. If you localized resources are in Unicode, you are all set. Otherwise, you need to make sure the charset/encoding is supported by Java so it can convert it correctly.
Don't worry about font. It has nothing to do with server. It's either specified in HTML or CSS. As long as the browser supports the fonts, you will be fine.
Java itself is I18N ready so it doesn't matter which framework you use. Struts provides taglib for I18N (bean:message) but you can also use JSTL (fmt:message).

Categories

Resources