strtus2 the OGNL expression language and some labels

2010-04-23  来源:本站原创  分类:Java  人气:463 

OGNL expression language:
Relative EL expression, it provides some of the features we need in peacetime, such as:
? Support object method calls, such as xxx.sayHello ();
? Support class static method calls and the value of the visit, the expression of the format for the @ [type the full name] @ [method name | value name], for example: @ java.lang.String @ format ('foo% s',' bar ')
? Operations collection object.

Ognl one context (Context) concept, in fact, is a Map structure, which implements the java.utils.Map interfaces, Struts2 in the context of achieving the ActionContext.
Chart:

When Struts2 receiving a request, it will quickly create ActionContext, ValueStack, action. Then the action into ValueStack, so action can be OGNL instance variable access.

Access in the context of the objects need to use the # symbol annotation namespace, such as # application, # session

Another would be to set a root object OGNL (root object), the root object in Struts2 is ValueStack. If you visit the root object in the object's attributes, you can omit the # namespace.

The root object in Struts2 ValueStack the implementation class is OgnlValueStack, the object management is our only store a single value of imagination, but kept a group of objects. OgnlValueStack class in a List type of root variables, used to store a set of objects.
In the root variables in the first object is called the stack object. Usually OGNL expressions you can access directly through the root variable name properties. Search order from the top of the stack start, then goes looking for.
In Struts2, OGNL expression needs can only be used with Struts tags. Such as: <s:property value="name"/>

Access to other objects in Context.
? Application object: used to access the ServletContext, such as: # application.userName or # application ['userName], the equivalent of calling ServletContext.getAttribute ("userName");
? Session object: to access the HttpSession, such as: # session.userName or # session ['userName'] is equivalent to calling session.getAttribute ("username").
? Request object: used to access the HttpServletRequest attribute (attribute) of the Map, such as: # request.userName or # request ['userName'] is equivalent to calling request.getAttribute ("username").
? Parameters object: used to access the Http request parameters, such as: # parameters.userName or # parameters ['userName'] is equivalent to calling request.getParameter ("username").
? Attr object: for according to page-> request-> session-> application in order to access its properties.

Using OGNL expression to create the List / Map collection object

Build List object
<s:set name="list" value={'zhang san','li si','wang wu'}/>
<s:iterator values="#list">
<s:property/> <br/>
</ S: iterator>

[Note: iterator tag in Fu collection this iteration, the current iteration object will object on the stack of stack]

Scope: specifies the variable scope (application, session, request, page, action). Placed in the OGNL Context of default.
Value: variable, default value: ValueStack stack value.

Map generated object:
<s:set name="foobar" value="#{'foo1':'bar1','foo2':'bar2'}"/>
<s:iterator value="#foobar">
<s:property value="key"/> = <s:property value="value"/>
</ S: iterator>

property Tag:
property tag used to output a specified value:
<s:set name="name" value="kk"/>
<s:property name="#name"/>
default: Optional attribute, if the output value of the properties of null, then display the specified value.
escape: an optional attribute that specifies whether the formatted HTML code.
value: an optional attribute, attribute values need to output, the default value of the output ValueStack top of the stack.
id: an optional attribute, identity.

OGNL expression used to judge whether the object exists in the collection

in the expression:
<s:if test="'foo' in {'foo','bar'}">
Presence
</ S: if>
<s:else>
Does not exist
</ S: else>

not in the expression:
<s:if test="'foo' not in {'foo','bar'}">
Does not exist
</ S: if>
<s:else>
Presence
</ S: else>

OGNL expressions in the projection functions:
? : Get all the logical elements.
^: Be the first logical element.
$: Get the last element of logical.

Such as:
<s:iterator value="books.{?#this.price> 35) ">
<s:property value="title"/>
<s:property value="price"/>
</ S: iterator>

One. () For a subset of books out.

Iterator Tags:
<s:set name="list" value={'zhang san','li si','wang wu'}/>
<s:iterator values="#list" status="st">
<Font color = <s:if test="#st.odd"> red </ s: if> <s:else> blue </ s: else>>
<s:property/> <br/>
</ S: iterator>

Attributes: id, name, value, status.
Suatus: the method:
int getcount (), returns the current iteration of several elements.
int getIndex (), returns the current iteration index of the element.
boolean isEven (), returns whether the current iteration element is even.
boolean isOdd (), returns whether the current iteration element is odd.
boolean isFirst (), returns whether the current iteration element is the first element.
boolean isLast (), returns the current iteration is the first element is the last element.

if / elseif / else tags:
<s:set name="age" value="21"/>
<s:if test="#age==23">
23
</ S: if>
<s:elseif test="#age==21">
21
</ S: else>
<s:else>
Not
</ S: else>

url Tags:
<s:url action="helloworld" namespace="test">
<s:param name="persionId" value="18"/>
</ S: url>
The resulting path is as follows:
/ Test / helloworld.action? Personid = 18

[Note: When the value value as the guest handling, use "%" to calculate the value of OGNL expression]
<s:set name="myurl" value="http://www.apache.org" />
<s:url value="#myurl"/>
<s:url value="%{#myurl}"/>
Output:
# Myurl
http://www.apache.org

Form tag checkboxlist:
List by:
<s:checkboxlist name="list" list="{'java','.Net','Php'}" value="{'java' ,'.Net'}"/>
Generated code:
<input type="checkbox" name="list" value="Java""list-1" checked="checked"/>
<label for="list-1"> Java </ label>
<input type="checkbox" name="list" value=".Net""list-2" checked="checked"/>
<label for="list-1">. Net </ label>
<input type="checkbox" name="list" value="Php""list-3" />
<label for="list-1"> Php </ label>

Map by:
<S: checkboxlist name = "map" list = "('1 ':' java ', '2': '. Net', '3 ':' Php ')" listKey = "key" listValue = "value" value = "(1,2)" />
One listKey generate value property, listValue generated label attribute
<input type="checkbox" name="map" value="1" checked="checked"/>
<label> Java </ label>

token tag to prevent duplicate form submission
1. In the form to add <s:token/> label.
<s:form action="helloworld" method="post"/>
<s:textfield name="person.name"/>
<s:token/>
</ S: form>
2. Struts2 in the configuration file
<action name="helloworld""…" method="execute">
<interceptor-ref name="defaultStacke/>
<interceptor-ref name="token"/>
<result name="invalid.token"> / WEB-INF/page/message.jsp </ result>
<result> WEB-INF/page/result.jsp>
</ Action>
When the "token" interceptor in the session token and request token is inconsistent, it will directly return called "invalid.token" view.

相关文章
  • strtus2 the OGNL expression language and some labels 2010-04-23

    OGNL expression language: Relative EL expression, it provides some of the features we need in peacetime, such as: ? Support object method calls, such as xxx.sayHello (); ? Support class static method calls and the value of the visit, the expression o

  • OGNL expression language introduced 2010-09-29

    OGNL expression language introduced OGNL Introduction OGNL Object-Graph Navigation Language is the abbreviation , It is a powerful expression language (Expression Language, Referred to as EL), Expression through its simple and consistent syntax, you

  • About OGNL expression language 2010-10-19

    OGNL is the abbreviation of the object graph navigation language, he is an open source project. Struts2 framework uses OGNL expression language as the default. Relative EL expression, it provides the usual features we need, such as: Support for objec

  • OGNL expression language 2010-12-07

    Expression commonly used to access the page in a variety of variables, the results output. struts2 CPC supports the following expression language (the default is OGNL): OGNL: Object properties can easily operate the open source expression language; J

  • Struts 2 of the OGNL expression 2010-03-30

    Struts 2 of the OGNL expression Will set a standard OGNL root object (root object). Assumptions using standard OGNL expressions to seek value (not using Struts 2 in OGNL expressions), if OGNL context (OgnlContext Map type) has two objects: foo object

  • Struts 2 in the expression language: OGNL 2011-05-04

    Struts 2 in the expression language: OGNL Struts 2 supports the following expression language: 1.OGNL (Object-Graph Navigation Language), can easily operate the open source object property expression language; 2.JSTL (JSP Standard Tag Library), JSP 2

  • Struts2 OGNL expression used 2010-03-24

    OGNL full name of the Object Graph Navigation Language, can be considered a more perfect EL expressions. It can truly replace a traditional jsp server script (<%%>)。 This article does not set out the advantages of OGNL, aiming at to help us to under

  • JSP2.1 and unified expression language 2010-05-11

    JSP2.1 new features 1, the new unified expression language, an extension request to allow the value of the expression, the expression can be assigned either value, you can also call the method. 2, can be injected through the annotation of resources,

  • struts2 OGNL expression 2010-06-04

    OGNL is the Object Graph Navigation Language of short, detailed information can be related to: http://www.ognl.org . Here we are concerned only Struts2 framework of the basic support for OGNL. Struts 2 default expression language is OGNL, because it

  • Struts2 OGNL expression is one of the 2010-06-30

    1, OGNL concept OGNL is the Object-Graph Navigation Language acronym, it is a powerful expression language (Expression Language, referred to as EL), through its simple and consistent expression syntax, you can access any property of the object, call

  • Web Summary --- Servlet, JSP, filters and listeners, the scope and nine four built-in objects, EL expression language, custom tags, JSTL, an international collection of the Chinese character encoding 2011-01-01

    1, Servlet, ServletConfig: The former corresponds to a Servlet class, which corresponds to the configuration information in the web.xml (1) Servlet: init (ServletConfig): only then loaded and instantiated class is called once. service (ServletRequest

  • EL Expression Language 2010-03-25

    Basic syntax 1. EL Introduction 1. Grammatical structure ${expression} 2.[] And. Operator EL Provided. And [] Two operators to access data . When you want to access the property name contains special characters, such as . Or? And other symbols not le

  • struts2 + displaytag flip Inappropriate OGNL expression when the solution (solution) 2010-05-11

    Believe that there are some people encounter this problem: struts2 + displaytag next page will appear the following stack of information, although the process can run up, but still makes people very uncomfortable. ognl.InappropriateExpressionExceptio

  • ognl expression usage 2010-05-11

    ognl expression usage User object property access, such as a username and password in the User field for username attribute <s:property value="user.username" /> Get password attribute <s:property value="user.password" /> If

  • Proficient in the ognl expression struts2 2010-09-29

    Proficient in the ognl expression struts2 Struts2 OGNL expressions in the built-in support , Made with more than Struts2 Struts1 More powerful data access features. This article explains OGNL To use, and the principle of not going to explain some thi

  • struts2 + displaytag Inappropriate OGNL expression when you turn the solution (solution) 2010-10-23

    Sure other people must experience this problem: struts2 + displaytag page will appear the following stack information, although the program can run up, but still makes people very uncomfortable. ognl.InappropriateExpressionException: Inappropriate OG

  • Jsp / Servlet review notes ----- JSP Expression Language Chapter 8 2010-10-26

    About 8.1 expression language JSP used to display dynamic content on the page, usually embedded in the JSP page, Java scripts to perform complex functions. But a lot of Java script JSP pages makes it difficult to maintain. A language similar to JavaS

  • JSTL Introduction: Expression Language (a) 2010-11-10

    JavaServer Pages (JSP) is a standard platform for J2EE presentation layer technology. JSP technology provides for the implementation of calculations (these calculations is used to dynamically generate page content) and scripting elements and actions.

  • ognl.InappropriateExpressionException: Inappropriate OGNL expression 2011-01-14

    ognl.InappropriateExpressionException: Inappropriate OGNL expression: 1 October 13, 2010 Wednesday 14:26 The following information found online. Note here: Inappropriate OGNL expression: 1, Your question: The "1", you will have a form submission

  • ognl.InappropriateExpressionException: Inappropriate OGNL expression: 1 2014-04-15

    WARN OgnlValueStack:49 - Error setting expression '1' with value '[Ljava.lang.String;@11c7eb2' ognl.InappropriateExpressionException: Inappropriate OGNL expression: 1 at ognl.SimpleNode.setValueBody(SimpleNode.java:312) at ognl.SimpleNode.evaluateSet