ViewParam vs @ManagedProperty(value = “#{param.id}”)

℡╲_俬逩灬. 提交于 2019-11-25 21:53:09

问题


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;

回答1:


<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:

  • What can <f:metadata>, <f:viewParam> and <f:viewAction> be used for?
  • Communication in JSF 2.0 - Processing GET request parameters



回答2:


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.


来源:https://stackoverflow.com/questions/4888942/viewparam-vs-managedpropertyvalue-param-id

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!