使用 h:commandButton 传递参数

发布于 2024-09-10 10:29:03 字数 688 浏览 9 评论 0原文

我有一个 a4j:commandButton ,它应该根据 Id 将我重定向到适当的“编辑”页面,我想将其作为参数传递,如下所示:

<h:commandButton action="/details.jsf?faces-redirect=true" value="details">
    <f:attribute name="id" value="#{bean.id}" />
</h:commandButton>

问题是,它不起作用。我还尝试将 f:attribute 替换为 "f:param name="id" value="#{bean.id}" ",但也失败了。我唯一需要做的就是一个outputLink:

<h:outputLink  value="/details.jsf">
    link
    <f:param name="id" value="#{bean.id}" />
</h:outputLink>

但是我对链接不太满意,那么有没有办法让commandButton 工作?

哦,我还有一个 bean,它应该在重定向后获取“id”:

@PostConstruct
public void init(){
    id= resolve("id");
}

I have a a4j:commandButton which is supposed to redirect me to an appropriate "Edit" page based on an Id, which I wanted to pass as a parameter, something like this:

<h:commandButton action="/details.jsf?faces-redirect=true" value="details">
    <f:attribute name="id" value="#{bean.id}" />
</h:commandButton>

The problem is, it doesn't work. I also tried replacing f:attribute with "f:param name="id" value="#{bean.id}" ", but it also failed. The only thing I got to work is an outputLink:

<h:outputLink  value="/details.jsf">
    link
    <f:param name="id" value="#{bean.id}" />
</h:outputLink>

But I'm not really happy with a link, so is there a way to make the commandButton work?

Oh and I also have a bean which is supposed to get that "id" after the redirect:

@PostConstruct
public void init(){
    id= resolve("id");
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

眼泪淡了忧伤 2024-09-17 10:29:03

请参阅这篇有关 JSF 通信的文章,作者: BalusC

f:param 仅适用于 h:commandLink 和 <代码>h:输出链接。

您可以使用隐藏的输入:

<h:form>
    <h:commandButton action="/details.jsf?faces-redirect=true" value="details"/>
    <input type="hidden" name="id" value="#{bean.id}" />
</h:form>

然后在您的 faces-config 中,我猜是请求范围。如果您使用 JSF2 的注释,只需将其转换为正确的注释即可。

<managed-bean>
    <managed-bean-name>bean</managed-bean-name>
    <managed-bean-class>mypackage.Bean</managed-bean-class>
    <managed-bean-scope>request</managed-bean-scope>
    <managed-property>
        <property-name>id</property-name>
        <value>#{param.id}</value>
    </managed-property>
</managed-bean>

显然,您需要在支持 bean 中为该字段提供 getter 和 setter。

或者尝试通过 CSS 将链接“绘制”为按钮。

Have a look at this article about communication in JSF, by BalusC

f:param only works with h:commandLink and h:outputLink.

You can use an input hidden:

<h:form>
    <h:commandButton action="/details.jsf?faces-redirect=true" value="details"/>
    <input type="hidden" name="id" value="#{bean.id}" />
</h:form>

And then in your faces-config, I guess is request scoped. If you use the annotations of JSF2, just translate this to the proper annotations.

<managed-bean>
    <managed-bean-name>bean</managed-bean-name>
    <managed-bean-class>mypackage.Bean</managed-bean-class>
    <managed-bean-scope>request</managed-bean-scope>
    <managed-property>
        <property-name>id</property-name>
        <value>#{param.id}</value>
    </managed-property>
</managed-bean>

You need obviously to have getters and setters for that field in the backing bean.

or try to "paint" the link as a button through CSS.

安静 2024-09-17 10:29:03

除非我的 JSF 非常生疏,否则命令按钮或命令链接上的操作属性用于指定 faces-config-nav 文件中定义的结果字符串,或者它应该指向 bean 上将返回结果的方法(或重定向/无论如何)。

在您的情况下,如果您想重定向到另一个页面...您应该在配置文件中将其定义为导航链接(如有必要,使用 redirect )。然后,在您的操作按钮中,您应该有类似

<h:commandButton action="showDetails" value="details">

...

<navigation-case>
        <from-outcome>showDetails</from-outcome>
        <to-view-id>/details.jsf?faces-redirect=true</to-view-id>
</navigation-case>

顺便说一句, 标记将起作用,但它只会将属性设置到组件上。因此,如果您按住 bean 中的命令按钮,则可以通过名称获取属性值。要传递请求参数,请使用隐藏字段技术,如提到的 pakore

Unless my JSF is extremely rusty, the action attribute on a command button or command link is used to specify the outcome string defined in your faces-config-nav file, or it should point to a method on the bean which will return an outcome (or redirect/whatever).

In your case, if you want to redirect to another page... you should define that in your config file, as a navigation link (with redirect if necessary). Then in your action button you should have something like

<h:commandButton action="showDetails" value="details">

...

<navigation-case>
        <from-outcome>showDetails</from-outcome>
        <to-view-id>/details.jsf?faces-redirect=true</to-view-id>
</navigation-case>

As an aside, the <f:atribute> tag will work, but it will only set the attribute onto the component. So if you got a hold of the command button in your bean, you could be able to get the attribute value by name. To pass a request param, use the hidden field technique like pakore mentioned

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文