Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
725 views
in Technique[技术] by (71.8m points)

jsf - How to pass a parameter along with h:commandButton

One of the most common approaches to change locale in JSF+Seam - with <h:selectOneMenu>:

<h:form  action="#{localeSelector.select}" rendered="false">
    <h:selectOneMenu value="#{localeSelector.language}" onchange="submit()">
        <f:selectItem itemLabel="English" itemValue="en" />
        <f:selectItem itemLabel="Francais" itemValue="fr" />
    </h:selectOneMenu>
</h:form>

I want to implement locale changes with buttons. So, the question is - how to pass the parameter (en, fr, etc.) to update the bean with <h:commandButton>? Maybe <h:inputHidden> would help?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Either pass as method argument (only if your environment supports EL 2.2),

<h:commandButton value="English" action="#{localeSelector.change('en')}" />
<h:commandButton value="Deutsch" action="#{localeSelector.change('de')}" />
<h:commandButton value="Fran?ais" action="#{localeSelector.change('fr')}" />

with

public void change(String language) {
    locale = new Locale(language);
    // ...
}

Or use <f:setPropertyActionListener>

<h:commandButton value="English" action="#{localeSelector.change}">
    <f:setPropertyActionListener target="#{localeSelector.language}" value="en" />
</h:commandButton>
<h:commandButton value="Deutsch" action="#{localeSelector.change}">
    <f:setPropertyActionListener target="#{localeSelector.language}" value="de" />
</h:commandButton>
<h:commandButton value="Fran?ais" action="#{localeSelector.change}">
    <f:setPropertyActionListener target="#{localeSelector.language}" value="fr" />
</h:commandButton>

with

private String language;

public void change() {
    locale = new Locale(language);
    // ...
}

Or use <f:param>

<h:commandButton value="English" action="#{localeSelector.change}">
    <f:param name="language" value="en" />
</h:commandButton>
<h:commandButton value="Deutsch" action="#{localeSelector.change}">
    <f:param name="language" value="de" />
</h:commandButton>
<h:commandButton value="Fran?ais" action="#{localeSelector.change}">
    <f:param name="language" value="fr" />
</h:commandButton>

with

public void change() {
    locale = new Locale(FacesContext.getCurrentInstance().getExternalContext().getRequestParameterMap().get("language"));
    // ...
}

(you can also let JSF automatically set it by a @ManagedProperty("#{param.language}"), but this requires the bean to be request scoped, or a <f:viewParam>, see also ViewParam vs @ManagedProperty(value = "#{param.id}"))


Enough ways to pass a parameter from view to controller. Take your pick. The <h:inputHidden> serves in JSF context a somewhat different purpose and it can only be manipulated by JavaScript in the onclick which is ugly.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...