posts - 0,  comments - 0,  trackbacks - 0

Table 2-4. Predefined Objects in the Value Expression Language
Variable Name Meaning
header A Map of HTTP header parameters, containing only the first value for each name.
headerValues A Map of HTTP header parameters, yielding a String[]array of all values for a given name.
param A Map of HTTP request parameters, containing only the first value for each name.
paramValues A Map of HTTP request parameters, yielding a String[]array of all values for a given name.
cookie A Map of the cookie names and values of the current request.
initParam A Map of the initialization parameters of this web application. Initialization parameters are discussed in Chapter 10.
requestScope A Map of all request scope attributes.
sessionScope A Map of all session scope attributes.
applicationScope A Map of all application scope attributes.
facesContext The FacesContext instance of this request. This class is discussed in Chapter 6.
view The UIViewRoot instance of this request. This class is discussed in

 Finally, if the name is still not found, it is passed to the VariableResolver of the JSF application. The default variable resolver looks up managed-bean elements in a configuration resource, typically the faces-config.xml file.

Consider, for example, the expression

  #{user.password}


The term user is not one of the predefined(预定义) objects. When it is encountered(遇到) for the first time, it is not an attribute name in request, session, or application scope.

Therefore, the variable resolver processes the faces-config.xml entry:

  <managed-bean>
     <managed-bean-name>user</managed-bean-name>
     <managed-bean-class>com.corejsf.UserBean</managed-bean-class>
     <managed-bean-scope>session</managed-bean-scope>
  </managed-bean>


The variable resolver calls the default constructor of the class com.corejsf.User-Bean. Next, it adds an association to the sessionScope map. Finally, it returns the object as the result of the lookup.

When the term user needs to be resolved again in the same session, it is located in the session scope


Composite Expressions
    

You can use a limited set of operators inside value expressions:

Arithmetic operators + - * / %. The last two operators have alphabetic variants div and mod.

Relational operators < <= > >= == != and their alphabetic variants lt le gt ge eq ne. The first four variants are required for XML safety.

Logical operators && || ! and their alphabetic variants and or not. The first variant is required for XML safety.

The empty operator. The expression empty a is true if a is null, an array or String of length 0, or a Collection or Map of size 0.

The ternary ?: selection operator.

 Operator precedence follows the same rules as in Java. The empty operator has the same precedence as the unary - and ! operators.

 Generally, you do not want to do a lot of expression computation in web pages—that would violate the separation of presentation and business logic. However, occasionally, the presentation layer can benefit from operators. For example, suppose you want to hide a component when the hide property of a bean is true. To hide a component, you set its rendered attribute to false. Inverting the bean value requires the ! (or not) operator:

  <h:inputText rendered="#{!bean.hide}" ... />


 

Finally, you can concatenate plain strings and value expressions by placing them next to each other. Consider, for example,

  <h:outputText value="#{messages.greeting}, #{user.name}!"/>


 

The statement concatenates four strings: the string returned from #{messages. greeting}, the string consisting of a comma and a space, the string returned from #{user.name}, and the string consisting of an exclamation mark.


        
上文出自:《core JavaServer™ Faces, Second Edition》

posted on 2007-12-11 19:25 *一凡* 阅读(137) 评论(0)  编辑  收藏 所属分类: JSF