ViewParam 与 @ManagedProperty(value = "#{param.id}")

发布于 2024-10-15 21:42:14 字数 266 浏览 0 评论 0 原文

有什么区别:

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

像这样定义视图参数和像这样在 ManagedBean 中定义属性

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

What is the difference between defining View Params like this:

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

And defining the property in the ManagedBean like this:

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

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

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

发布评论

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

评论(2

握住你手 2024-10-22 21:42:14

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

  • 设置值在@期间不可用PostConstruct,因此您需要额外的 3.0/vdldoc/f/metadata" rel="nofollow noreferrer"> 根据设置的值进行初始化/预加载。从 JSF 2.2 开始,您可以使用 来代替。

  • 允许嵌套 进行更细粒度的转换/验证。甚至是 可以附加。

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

  • 可用于@RequestScoped< /code> bean,但它要求 bean 为 @ViewScoped 如果您希望视图参数能够承受由视图中包含的表单导致的任何验证失败,否则您需要手动保留 中的 < code>UICommand 组件,因为 将在每个请求上设置。

示例

<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 调用页面 将通过转换器传递id 参数,并将User 对象设置为bean 属性。


@ManagedProperty

  • 在 bean 构造后立即设置值。

  • 设置值在@PostConstruct 允许根据设置值轻松初始化/预加载其他属性。


  • 是否不允许允许在视图中进行声明性转换/验证。

  • 支持自动包含在结果目标 URL 中。

  • 可以在任何范围的 bean 上使用,但它只会在 bean 构造期间设置,而不是在每个请求时设置。

示例

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

private User user;

@EJB
private UserService userService;

@PostConstruct
public void init() {
    user = userService.find(id);
}

请注意,只要 usernull,您就必须通过摆弄 FacesContext#addMessage ()@PostConstruct 方法中。另请注意,当 #{param.id} 不是有效数字时,在命中 @PostConstruct 之前将引发异常。如果您想处理它,那么您最好将其设为私有字符串id。但更好的是使用


可以同时使用它们>@PostConstructincludeViewParams 是必需的。您将无法再应用细粒度的转换/验证。


另请参阅:

<f:viewParam>:

  • Sets the value during update model values phase only (since it extends UIInput).

  • The set value is not available during @PostConstruct, so you need an additional <f:event type="preRenderView" listener="#{bean.init}" /> inside the <f:metadata> to do initialization/preloading based on the set values. Since JSF 2.2 you could use <f:viewAction> for that instead.

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

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

  • Can be used on a @RequestScoped bean, but it requires the bean to be @ViewScoped if you want the view parameters to survive any validation failures caused by forms enclosed in the view, otherwise you need to manually retain all request parameters for the subsequent requests by <f:param> in the UICommand components, because the <f:viewParam> will be set on every request.

Example:

<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" />

with

private User user;

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.


@ManagedProperty:

  • Sets the value immediately after bean's construction.

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

  • Does not allow for declarative conversion/validation in view.

  • Does not support being automatically included in outcome target URLs.

  • Can be used on a bean of any scope, but it will be set only during bean's construction instead of on every request.

Example:

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

private User user;

@EJB
private UserService userService;

@PostConstruct
public void init() {
    user = userService.find(id);
}

Do note that you have to manage conversion and validation yourself whenever user is null by fiddling with FacesContext#addMessage() inside the @PostConstruct method. Also note that when #{param.id} is not a valid number, then an exception will be thrown before @PostConstruct is hit. If you want to deal with it, then you'd probably better make it a private String id. But much better is to use <f:viewParam>.


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.


See also:

近箐 2024-10-22 21:42:14

2 个其他区别:

  • @ManagedProperty 仅适用于由 JSF 管理的 Bean,不适用于由 CDI 管理的 Bean (@Named);
    • 仅适用于 GET 请求的参数。

2 other differences:

  • @ManagedProperty is usable only with beans managed by JSF, not with beans managed by CDI (@Named);
    • <f:viewParam> works only with parameters of GET requests.
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文