我想在点击其中一张图片时更改语言。有两个图像,其中一个是土耳其语,另一个是英语。如果我点击英语是英语。另一个是带有onclick的土耳其语。这是我的托管bean和xhtml;
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>
当我点击图片时,语言没有变化。如何通过图片点击事件更改语言?
答案 0 :(得分:2)
首先你应该有一个表格提交 第二:为什么在这里使用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>
托管bean的:
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;
}
}