使用 h:commandButton 传递参数
我有一个 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
请参阅这篇有关 JSF 通信的文章,作者: BalusC
f:param
仅适用于h:commandLink
和 <代码>h:输出链接。您可以使用隐藏的输入:
然后在您的 faces-config 中,我猜是请求范围。如果您使用 JSF2 的注释,只需将其转换为正确的注释即可。
显然,您需要在支持 bean 中为该字段提供 getter 和 setter。
或者尝试通过 CSS 将链接“绘制”为按钮。
Have a look at this article about communication in JSF, by BalusC
f:param
only works withh:commandLink
andh:outputLink
.You can use an input hidden:
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.
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.
除非我的 JSF 非常生疏,否则命令按钮或命令链接上的操作属性用于指定 faces-config-nav 文件中定义的结果字符串,或者它应该指向 bean 上将返回结果的方法(或重定向/无论如何)。
在您的情况下,如果您想重定向到另一个页面...您应该在配置文件中将其定义为导航链接(如有必要,使用
redirect
)。然后,在您的操作按钮中,您应该有类似...
顺便说一句,
标记将起作用,但它只会将属性设置到组件上。因此,如果您按住 bean 中的命令按钮,则可以通过名称获取属性值。要传递请求参数,请使用隐藏字段技术,如提到的 pakoreUnless 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...
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