JSF/Facelets:将“action”属性设置为动态评估的字符串
在我的 JSF/Facelets 应用程序中,我想使用自定义标记从页面 ID 列表动态生成面包屑路径:
<foo:breadcrumbs trail="foo,bar,baz"/>
这应该生成类似
<h:commandLink action="foo" ... />
<h:commandLink action="bar" ... />
<!-- (etc.) -->
以下内容的内容: 我的代码如下所示:
<ui:repeat value="#{fn:split(trail, ',')}" var="key">
<h:commandLink action="#{key}" ... />
</ui:repeat>
此代码的问题是 < code>#{key} 被解释为方法绑定。 但是,我只想返回 #{key}
的字符串值作为导航结果。 我怎样才能实现这个目标?
我唯一能想到的就是创建一个虚拟托管bean,它有一个结果
字段和一个操作处理程序,并像这样调用它:
<h:commandLink action="#{dummy.click}" ...>
<f:setPropertyActionListener target="#{dummy.outcome}" value="#{key}" />
</h:commandLink>
使用虚拟类定义如下:
public class Dummy {
private String outcome;
public String click() {
return outcome;
}
public void setOutcome(String outcome) {
this.outcome = outcome;
}
public void getOutcome() {
return outcome;
}
}
虽然这看起来很难看,但我不知道它是否有效。
In my JSF/Facelets application, I want to dynamically generate a breadcrumb trail from a list of page IDs using a custom tag:
<foo:breadcrumbs trail="foo,bar,baz"/>
This should generate something like:
<h:commandLink action="foo" ... />
<h:commandLink action="bar" ... />
<!-- (etc.) -->
My code looks something like this:
<ui:repeat value="#{fn:split(trail, ',')}" var="key">
<h:commandLink action="#{key}" ... />
</ui:repeat>
The problem with this code is that #{key}
is interpreted as a method binding. However, I just want the string value of #{key}
to be returned as the navigation outcome. How can I achieve this?
The only thing I could think of was creating a dummy managed-bean that has an outcome
field and an action handler, and invoke it like so:
<h:commandLink action="#{dummy.click}" ...>
<f:setPropertyActionListener target="#{dummy.outcome}" value="#{key}" />
</h:commandLink>
with the dummy class defined like so:
public class Dummy {
private String outcome;
public String click() {
return outcome;
}
public void setOutcome(String outcome) {
this.outcome = outcome;
}
public void getOutcome() {
return outcome;
}
}
That seems ugly though, and I don't know if it would work.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我想到了几种方法。
选项 1
坚持使用 commandLink 并直接从操作绑定中的请求映射读取
var
:(其中中继器具有属性
var="uirepeatVar"
code>.)选项 2
切换到 outputLink 并在服务器上构建 GET 链接:
查看:
A couple of ways spring to mind.
Option 1
Stick with commandLink and read the
var
directly from the request map in the action binding:(Where the repeater has the attribute
var="uirepeatVar"
.)Option 2
Switch to an outputLink and build GET links on the server:
View:
为什么不创建一个以编程方式生成 h:commandLink 对象的自定义组件? 这可能是“最干净”的解决方案。
Why don't you create a custom component which generates the h:commandLink objects programmatically? It would probably be the 'cleanest' solution.
自从提出这个问题以来,我发现了一个显而易见的解决方案,而且实施起来非常简单。
作为 JSF 操作目标的方法必须不接受任何参数并返回一个字符串。
事实证明,String 类已经有一个与此签名匹配的方法 -
toString()
!因此,我将 UI 循环更改为以下内容:
这允许动态评估的
key
成为 JSF 操作结果,并且不需要任何额外的类或丑陋的黑客行为。Since asking this question, I've discovered an obvious solution that is very simple to implement.
A method that is the target of a JSF action must accept no arguments and return a String.
It turns out that the String class already has a method that matches this signature -
toString()
!So, I've changed my UI loop to the following:
This allows the dynamically evaluated
key
to be the JSF action outcome, and doesn't require any additional classes or ugly hackery.