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

问题描述

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

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

然后在ManagedBean中定义属性,如下所示:

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

<f:viewParam> :

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

  • ,因此您需要额外的 <f:metadata> 根据设置值进行初始化/预加载.从JSF 2.2开始,您可以改为使用<f:viewAction>.

  • 允许嵌套 <f:converter> <f:validator> 获取更细粒度的转换/验证.甚至 <h:message> 可以附加.

  • 可以使用 <h:link> includeViewParams=true请求参数.

  • 可以在 bean,但是它要求 @ViewScoped ,如果您希望视图参数能够承受由视图中包含的表单引起的任何验证失败,则否则,您需要通过<f:param>手动保留命令组件中后续请求的所有请求参数. /p>

示例:

 <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 允许根据设置值轻松初始化/预加载其他属性.

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

  • 范围大于请求范围的Bean上不允许
  • #{param}的托管属性,因此要求该Bean是 @RequestScoped .

  • 如果您依赖于随后的POST请求中存在的#{param}托管属性,则需要将其作为<f:param>包含在UICommand组件中.

示例:

 @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 includeViewParams是必需的.您将只能再进行细粒度的转换/验证.


另请参见:

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:

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

<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 included as GET query string 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 command components.

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.

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

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

  • 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.

Example:

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

private User user;

@EJB
private UserService userService;

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

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


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:

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

07-23 12:52