日期:2014-05-16  浏览次数:20371 次

JSF 要点

?

1. Beans are request scoped by default. One request one bean.

?

?Managed beans typically have three parts

1. ?Bean properties (i.e, pairs of getter and setter methods)

?

  • One pair for each input element
  • Setter methods called automatically by JSF when form methods called automatically by JSF when form?submitted. Called before action controller method.

2. Action controller methods

?

  • Often only one but could be several if the same form has only one, but could be several if the same form has?multiple buttons
  • Action controller method (corresponding to the button that?was was pressed) called automatically by JSF pressed) called automatically by JSF

?

3 . ?Placeholders for results data

?

  • Not automatically called by JSF: to be filled in by action?controller method based on results of business logic.
  • Needs a getter method so value can be output in result page, but not required to have a setter method



?@RequestScoped
– Default. Make a new instance for every HTTP request.?
Since beans are also used for initial values in input form,?
this this means bean is generally instantiated twice (once means bean is generally instantiated twice (once
when form is displayed, once when form is submitted).
? @SessionScoped
– Put bean in session scope. If same user with same cookie?
returns before session timeout, same bean instance is?
used. used. You should make bean Serializable. You should make bean Serializable.
? @ApplicationScoped
– Put bean in app p y pplication scope. Shared by all users. Bean?
either should have no mutable state or you must carefully?
synchronize access

HTTP?request object and response object
? Static methods
– If they are needed, use static method calls to get them
ExternalContext context =
FacesContext.getCurrentInstance().getExternalContext();
HttpServletRequest request =
(HttpServletRequest)context.getRequest();
HttpServletResponse response =
(HttpServletResponse)context.getResponse();


?You can instantiate beans at app load time
– @ManagedBean(eager=true)
@ApplicationScoped
public class SomePojo { … }
? This This is useful for the bean that you inject into the main bean. The injected is useful for the bean that you inject into the main bean. The injected
bean is often something shared like a loo