【问题标题】:Changing language with h:graphicImage click event使用 h:graphicImage 点击事件更改语言
【发布时间】:2013-09-15 10:09:19
【问题描述】:

我想在单击其中一张图片时更改语言。有两张图片,一张是土耳其语,另一张是英语。如果我点击英语是英语。另一个是带有 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>

当我点击图片时,语言没有变化。如何通过图片点击事件更改语言?

【问题讨论】:

    标签: jsf-2 managed-bean graphicimage


    【解决方案1】:

    首先,您应该有一个要提交的表单 第二:为什么在这里使用 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;
       }
    }
    

    【讨论】:

      猜你喜欢
      • 2013-06-08
      • 1970-01-01
      • 2022-07-05
      • 1970-01-01
      • 1970-01-01
      • 2018-08-20
      • 2017-08-08
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多