Jsf 2 使用h:graphicImage单击事件更改语言
我想在单击其中一个图像时更改语言。有两个图像,其中一个是土耳其语,另一个是英语。如果我点击到英语语言是英语。另一个是土耳其语和onclickJsf 2 使用h:graphicImage单击事件更改语言,jsf-2,managed-bean,graphicimage,Jsf 2,Managed Bean,Graphicimage,我想在单击其中一个图像时更改语言。有两个图像,其中一个是土耳其语,另一个是英语。如果我点击到英语语言是英语。另一个是土耳其语和onclick public class DilSecimBean { private boolean isTurkey = true; private final Locale TR = new Locale("tr"); private final Locale EN = Locale.ENGLISH; public Locale getLocale() {
public class DilSecimBean {
private boolean isTurkey = true;
private final Locale TR = new Locale("tr");
private final Locale EN = Locale.ENGLISH;
public Locale getLocale() {
if (isTurkey) {
return TR;
} else {
return EN;
}
}
public void swapLocale() {
System.out.println("SWAP LOCALE");
switchLocale();
}
private void switchLocale() {
isTurkey = !isTurkey;
Locale newLocale;
if (isTurkey) {
newLocale = TR;
} else {
newLocale = EN;
}
FacesContext.getCurrentInstance().getViewRoot().setLocale(newLocale);
}
}
这是我的xhtml
<h:panelGrid columns="3" border="0">
<h:outputText value="Dil seçimi : " />
<h:graphicImage alt="JSF"
url="/resimler/tb.png"
width="20" height="20">
<f:ajax event="click" execute="#{dilSecimBean.swapLocale()}"/>
</h:graphicImage>
<h:graphicImage alt="JSFS"
url="/resimler/ib.png"
width="20" height="20">
<f:ajax event="click" execute="#{dilSecimBean.swapLocale()}"/>
</h:graphicImage>
</h:panelGrid>
单击图像时,语言没有更改。如何使用图像单击事件更改语言?首先,您应该提交一份表单 第二:为什么要在这里使用ajax? 这是一个有效的例子
<h:form>
<h:commandLink action="#{localeChanger.turkishAction}">
<h:graphicImage library="images" name="de_flag.gif"
style="border: 0px; margin-right: 1em;"/>
</h:commandLink>
<h:commandLink action="#{localeChanger.englishAction}">
<h:graphicImage library="images"
name="en_flag.gif" style="border: 0px"/>
</h:commandLink>
</h:form>
import java.io.Serializable;
import java.util.Locale;
import javax.inject.Named;
// or import javax.faces.bean.ManagedBean;
import javax.enterprise.context.SessionScoped;
// or import javax.faces.bean.SessionScoped;
import javax.faces.context.FacesContext;
@Named // or @ManagedBean
@SessionScoped
public class LocaleChanger implements Serializable {
public String TurkishAction() {
FacesContext context = FacesContext.getCurrentInstance();
context.getViewRoot().setLocale(new Locale("tr"));
return null;
}
public String englishAction() {
FacesContext context = FacesContext.getCurrentInstance();
context.getViewRoot().setLocale(Locale.ENGLISH);
return null;
}
}