JSF2:如何使用模板实现站点范围的 viewParam 处理策略

发布于 2024-12-03 15:34:09 字数 576 浏览 2 评论 0原文

XHTML Facelet 模板中似乎忽略了以下内容:(

<f:metadata>
    <f:viewParam name="id" value="#{backingBean.id}" />
</f:metadata>

至少按照 Glassfish2.2 使用 Mojarra)

Java Server Faces 2.0 Complete Reference 说第 540 页:

f:metadata 标签封装了用于指定的元素集 Facelet 视图的元数据,因此必须是 f:view 标签可能不会出现在模板中。从 JSF2.0 开始,唯一 该标签的目的是封装f:viewParam标签。

对于我当前的应用程序来说,必须在数百个 XHTML 页面中的每一个页面中都包含上述内容是很乏味的,但它在模板中失败了,我无法引入视图参数处理策略。事实上,我想要的是两者,所有页面通用的视图参数处理策略的混合,然后对需要额外查询参数的特定页面进行一些额外的处理。

很高兴提供建议,

韦贝尔

The following seems to be ignore inside XHTML facelet templates:

<f:metadata>
    <f:viewParam name="id" value="#{backingBean.id}" />
</f:metadata>

(At least with Mojarra as per Glassfish2.2)

Java Server Faces 2.0 Complete Reference says p.540:

The f:metadata tag encapsulates the set of elements used to specify
the metadata for a Facelet view, and therefore must be a child of the
f:view tag and may not appear in a template. As of JSF2.0, the only
purpose of this tag is to encapsulate f:viewParam tags.

It is tedious for my current application to have to include the above in every one of hundreds of XHTML pages, but it fails in the template, I can't introduce view parameter handling policies. Indeed what I would like is both, a mixture of view parameter handling policies common to all pages and then some extra handling for specific pages that take additional query parameters.

Glad for advice,

Webel

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

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

发布评论

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

评论(2

箜明 2024-12-10 15:34:09

不幸的是,这是规范 。最好的选择是使用 @ManagedProperty代替。

public class BackingBean {

    @ManagedProperty("#{param.id}")
    private Long id;

    // ...
}

唯一的缺点是这不提供使用 XHTML 声明性转换/验证的优势。如果需要,您可以在 @PostConstruct< 中完成此工作/code>方法。

Unfortunately, that's by spec. Your best bet is to use @ManagedProperty instead.

public class BackingBean {

    @ManagedProperty("#{param.id}")
    private Long id;

    // ...
}

The only disadvantage is that this doesn't offer the advantage of using declarative conversion/validation by XHTML. You can if necessary do this job in a @PostConstruct method.

盗琴音 2024-12-10 15:34:09

回答我自己的问题,以便可以与代码标记共享解决方案(我希望可以将代码标记放在注释中而不需要答案,或者如果可以请告知如何操作):

/** Unlike {@link Tracker}, only tracks within a single request.
 * 
 * This is useful for the idFrom request parameter tracking in combination
 * with an idFrom @ManagedProperty.
 * 
 * This can't be done in session scope.
 *
 * @author darrenkelly
 */
@ManagedBean
@RequestScoped
public class RequestTracker extends All_ {

    /** Creates a new instance of RequestTracker */
    public RequestTracker() {
    }

    @EJB private ElementQuery elementQuery;    

    @ManagedProperty("#{param.idFrom}")
    private Long idFrom;    
    /**
     * The id of the element page one came from to reach a tracked overview page.
     * 
     * @return 
     */
    public Long getIdFrom() {
        return idFrom;
    }    

    public void setIdFrom(Long idFrom) {
        if (idFrom != null) {
            this.idFrom = idFrom;
            elementFrom = elementQuery.find(idFrom);
            if (elementFrom == null) {
                String $error = "No element with identifier idFrom(" + idFrom + ") found as referring element";
                JsfUtil.addErrorMessage($error);
            }
        }
    }

    private Element elementFrom;

    /** The Element from whose page one came to a another page.
     */
    public Element getElementFrom() {
        return elementFrom;
    }

    public void setElementFrom(Element elementFrom) {
        this.elementFrom = elementFrom;
    }    
}

Answering my own question so can share solution with code markup (I wish one could put code markup in comments instead of needing an answer, or if one can please advise how):

/** Unlike {@link Tracker}, only tracks within a single request.
 * 
 * This is useful for the idFrom request parameter tracking in combination
 * with an idFrom @ManagedProperty.
 * 
 * This can't be done in session scope.
 *
 * @author darrenkelly
 */
@ManagedBean
@RequestScoped
public class RequestTracker extends All_ {

    /** Creates a new instance of RequestTracker */
    public RequestTracker() {
    }

    @EJB private ElementQuery elementQuery;    

    @ManagedProperty("#{param.idFrom}")
    private Long idFrom;    
    /**
     * The id of the element page one came from to reach a tracked overview page.
     * 
     * @return 
     */
    public Long getIdFrom() {
        return idFrom;
    }    

    public void setIdFrom(Long idFrom) {
        if (idFrom != null) {
            this.idFrom = idFrom;
            elementFrom = elementQuery.find(idFrom);
            if (elementFrom == null) {
                String $error = "No element with identifier idFrom(" + idFrom + ") found as referring element";
                JsfUtil.addErrorMessage($error);
            }
        }
    }

    private Element elementFrom;

    /** The Element from whose page one came to a another page.
     */
    public Element getElementFrom() {
        return elementFrom;
    }

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