JSF graphicImage onclick problem
You know sites with internationalization flags that you click and get the language you want. This is the task I want to achieve with JSF tags.
EDIT: Nothing happens when I press the language icon.
This is my xhtml.
<!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"
xmlns:h="http://java.sun.com/jsf/html"
xmlns:f="http://java.sun.com/jsf/core"
xmlns:ui="http://java.sun.com/jsf/facelets">
<f:view locale="#{lang.locale}">
<h:head>
<h:outputStylesheet name="style.css" library="css"/>
</h:head>
<h:bo开发者_如何学Pythondy>
<div id="flags">
<!-- NOT WORKING !!! -->
<h:graphicImage name="#{lng.flag}" library="images" styleClass="flag">
<f:ajax event="click" execute="#{lang.swapLocale()}"/>
</h:graphicImage>
</div>
This is the Language class. As you will see is a simple class which swaps between 2 languages (Bulgarian and English).
import java.io.Serializable;
import java.util.Locale;
import javax.faces.context.FacesContext;
import javax.faces.event.ActionEvent;
public class Language implements Serializable{
private static final long serialVersionUID = -5964917570659286475L;
private boolean isBulgarian = true;
private final Locale BG = new Locale("bg");
private final Locale EN = Locale.ENGLISH;
public Locale getLocale() {
if(isBulgarian) {
return BG;
} else {
return EN;
}
}
public void swapLocale() {
System.out.println("SWAP LOCALE");
switchLocale();
}
private void switchLocale() {
isBulgarian = !isBulgarian;
Locale newLocale;
if (isBulgarian) {
newLocale = BG;
} else {
newLocale = EN;
}
FacesContext.getCurrentInstance().getViewRoot().setLocale(newLocale);
}
}
Here are the faces-config.xml and web.xml
faces-config:
<?xml version="1.0" encoding="UTF-8"?>
<faces-config version="2.0" xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xi="http://www.w3.org/2001/XInclude"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-facesconfig_2_0.xsd">
<managed-bean>
<managed-bean-name>lang</managed-bean-name>
<managed-bean-class>jsfDP.language.Language</managed-bean-class>
<managed-bean-scope>session</managed-bean-scope>
</managed-bean>
<application>
<resource-bundle>
<base-name>language</base-name>
<var>lng</var>
</resource-bundle>
</application>
</faces-config>
web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">
<display-name>DesignPatternsWithJSF</display-name>
<welcome-file-list>
<welcome-file>index.xhtml</welcome-file>
</welcome-file-list>
<servlet>
<servlet-name>Faces Servlet</servlet-name>
<servlet-class>javax.faces.webapp.FacesServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>Faces Servlet</servlet-name>
<url-pattern>*.faces</url-pattern>
</servlet-mapping>
</web-app>
Without knowing what your exact problem is, just a guess:
You're using AJAX to switch the language, but I don't see the view getting rerendered. In that case the switch might occur but if the view isn't updated you won't see it.
Assuming the whole page needs to be updated anyways, I'd suggest wrapping the image with a commandButton that executes the swap and doesn't use AJAX (which would be unnecessary here).
精彩评论