ViewParam vs @ManagedProperty(value = "#{param.id}") [英] ViewParam vs @ManagedProperty(value = "#{param.id}")

查看:20
本文介绍了ViewParam vs @ManagedProperty(value = "#{param.id}")的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

像这样定义视图参数有什么区别:

What is the difference between defining View Params like this:

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

并像这样在 ManagedBean 中定义属性:

And defining the property in the ManagedBean like this:

@ManagedProperty(value = "#{param.id}")
private Integer id;

推荐答案

:

  • 仅在更新模型值阶段设置值(因为它扩展了 UIInput).

    @PostConstruct,所以你需要一个额外的 根据设定值进行初始化/预加载.从 JSF 2.2 开始,您可以使用 代替.

    允许嵌套 用于更细粒度的转换/验证.即使是 <h:message> 可以附加.

    Allows for nested <f:converter> and <f:validator> for more fine-grained conversion/validation. Even a <h:message> can be attached.

    可以使用 includeViewParams=true 任何 URL 中的请求参数.

    Can be included as GET query string using includeViewParams attribute of <h:link> or includeViewParams=true request parameter in any URL.

    可用于@RequestScoped bean,但它要求 bean 是 @ViewScoped 如果您希望视图参数在视图中包含的表单导致的任何验证失败中幸存下来,否则您需要手动保留所有请求参数命令组件中 的后续请求.

    示例:

    <f:metadata>
        <f:viewParam id="user_id" name="id" value="#{bean.user}"
            required="true" requiredMessage="Invalid page access. Please use a link from within the system."
            converter="userConverter" converterMessage="Unknown user ID."
        />
    </f:metadata>
    <h:message for="user_id" />
    

    private User user;
    

    和一个 @FacesConverter(";userConverter").通过 http://example.com/context/user.xhtml?id=123<调用页面/a> 将通过转换器传递 id 参数并将 User 对象设置为 bean 属性.

    and an @FacesConverter("userConverter"). Invoking page by http://example.com/context/user.xhtml?id=123 will pass the id parameter through the converter and set the User object as a bean property.

    • 在 bean 构建后立即设置值.

    • Sets the value immediately after bean's construction.

    设置值在@期间可用PostConstruct 允许根据设置值轻松初始化/预加载其他属性.

    Set value is available during @PostConstruct which allows easy initialization/preloading of other properties based on the set value.

    不允许在视图中进行声明性转换/验证.

    Doesn't allow for declarative conversion/validation in view.

    #{param} 的托管属性在比请求范围更广的 bean 上是不允许的,因此 bean 必须是 @RequestScoped.

    Managed property of #{param} is not allowed on beans with a broader scope than request scope, so the bean is required to be @RequestScoped.

    如果您依赖 #{param} 的托管属性出现在后续 POST 请求中,那么您需要将其包含为 <f:param>UICommand 组件中的 code>.

    If you rely a managed property of #{param} being present in the subsequent POST requests, then you need to include it as <f:param> in the UICommand components.

    示例:

    @ManagedProperty("#{param.id}")
    private Long id;
    
    private User user;
    
    @EJB
    private UserService userService;
    
    @PostConstruct
    public void init() {
        user = userService.find(id);
    }
    

    但是当 usernull 时,您必须自己管理验证,方法是摆弄 FacesContext#addMessage() 之类的.

    But you have to manage validation yourself whenever user is null by fiddling with FacesContext#addMessage() or something.

    @PostConstructincludeViewParams 是强制性的.您将无法再应用细粒度转换/验证.

    You can use them both when both @PostConstruct and includeViewParams are mandatory. You only won't be able to apply fine-grained conversion/validation anymore.

    这篇关于ViewParam vs @ManagedProperty(value = "#{param.id}")的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆