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

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

问题描述

在 JSF+Seam 中更改语言环境的最常见方法之一 - 使用 :

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>

我想通过按钮实现区域设置更改.所以,问题是 - 如何传递参数(en、fr 等)以使用 更新 bean?也许 会有所帮助?

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?

推荐答案

要么作为方法参数传递(仅当您的环境支持 EL 2.2 时),

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')}" />

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

<小时>

或者使用

<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);
    // ...
}

<小时>

或者使用

<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>,另见ViewParam vs @ManagedProperty(value = "#{param.id}"))

(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}"))

有足够的方法将参数从视图传递到控制器.随你挑. 在 JSF 上下文中的作用有些不同,它只能在丑陋的 onclick 中由 JavaScript 操作.

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天全站免登陆