Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/jsf/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
JSF/Facelets:将'action'属性设置为动态计算的字符串_Jsf_Navigation_Facelets_Breadcrumbs - Fatal编程技术网

JSF/Facelets:将'action'属性设置为动态计算的字符串

JSF/Facelets:将'action'属性设置为动态计算的字符串,jsf,navigation,facelets,breadcrumbs,Jsf,Navigation,Facelets,Breadcrumbs,在我的JSF/Facelets应用程序中,我希望使用自定义标记从页面ID列表中动态生成面包屑轨迹: <foo:breadcrumbs trail="foo,bar,baz"/> 使用如下定义的虚拟类: public class Dummy { private String outcome; public String click() { return outcome; } public void setOutcome(Strin

在我的JSF/Facelets应用程序中,我希望使用自定义标记从页面ID列表中动态生成面包屑轨迹:

<foo:breadcrumbs trail="foo,bar,baz"/>
使用如下定义的虚拟类:

public class Dummy {

    private String outcome;

    public String click() {
        return outcome;
    }

    public void setOutcome(String outcome) {
        this.outcome = outcome;
    }

    public void getOutcome() {
        return outcome;
    }
}

这看起来很难看,我不知道它是否能工作。

为什么不创建一个自定义组件,以编程方式生成h:commandLink对象?这可能是“最干净”的解决方案。

我想到了几种方法

选择1

坚持使用commandLink并直接从操作绑定中的请求映射中读取
var

public String click() {
  FacesContext context = FacesContext.getCurrentInstance();
  ExternalContext extContext = context.getExternalContext();
  Map<String, Object> reqMap = extContext.getRequestMap();
  return (String) reqMap.get("uirepeatVar");
}
视图:


#{url}

自从提出这个问题以来,我发现了一个显而易见的解决方案,它非常容易实现

作为JSF操作目标的方法必须不接受任何参数并返回字符串。 原来String类已经有了一个与此签名匹配的方法-
toString()

因此,我将UI循环更改为以下内容:

<ui:repeat value="#{fn:split(trail, ',')}" var="key">
    <h:commandLink action="#{key.toString}" ... /> 
</ui:repeat>


这允许动态评估的
成为JSF操作的结果,并且不需要任何额外的类或丑陋的黑客行为。

Hmmm。。。我不想使用
outputLink
s。当我混合搭配
commandLink
s和
outputLink
s时,我似乎总是遇到麻烦。对于我的应用程序内部链接,我更愿意坚持使用
commandLink
s,然后使用第一个选项——直接从请求映射中读取var。这听起来是个好主意,但我不确定如何做到这一点。你能告诉我正确的方向吗?网上有很多教程。例如,IBM developerWorks教程::我认为在这种情况下,定制组件可能会有些过分,但是如果您这样做,一个对多个链接进行编码/解码的控件就可以了。
public class Dummy {

    private String outcome;

    public String click() {
        return outcome;
    }

    public void setOutcome(String outcome) {
        this.outcome = outcome;
    }

    public void getOutcome() {
        return outcome;
    }
}
public String click() {
  FacesContext context = FacesContext.getCurrentInstance();
  ExternalContext extContext = context.getExternalContext();
  Map<String, Object> reqMap = extContext.getRequestMap();
  return (String) reqMap.get("uirepeatVar");
}
public List<String> getViewUrls() {
  List<String> views = Arrays.asList("/index.xhtml", "/idtable.xhtml");

  List<String> urls = new ArrayList<String>();
  FacesContext context = FacesContext.getCurrentInstance();
  Application application = context.getApplication();
  ViewHandler viewHandler = application.getViewHandler();
  for (String view : views) {
    String url = viewHandler.getActionURL(context, view);
    urls.add(url);
  }
  return urls;
}
<ui:repeat value="#{breadcrumbBean.viewUrls}" var="url">
  <h:outputLink value="#{url}">#{url}</h:outputLink> <br />
</ui:repeat>
<ui:repeat value="#{fn:split(trail, ',')}" var="key">
    <h:commandLink action="#{key.toString}" ... /> 
</ui:repeat>