如何将参数与h:commandButton一起传递 [英] How to pass a parameter along with h:commandButton

查看:96
本文介绍了如何将参数与h:commandButton一起传递的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在JSF + Seam中更改语言环境的最常见方法之一-使用<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>

我想通过按钮实现语言环境更改.因此,问题是-如何传递参数(en,fr等)以使用<h:commandButton>更新bean?也许<h:inputHidden>会有所帮助?

解决方案

以方法参数传递(仅当您的环境支持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')}" />

使用

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


或使用<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>

使用

private String language;

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


或使用<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>

使用

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

(您还可以让JSF通过@ManagedProperty("#{param.language}")自动设置它,但这需要将bean限定在请求范围内,或者是<f:viewParam>,另请参见)


将参数从视图传递到控制器的足够方法.随便你吧. <h:inputHidden>在JSF上下文中的用途有所不同,并且只能由onclick中的JavaScript操纵,这很丑.

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?

解决方案

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.

这篇关于如何将参数与h:commandButton一起传递的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆