Struts2 result wildcard OGNL, struts2ognl

Source: Internet
Author: User
Tags argumentlist

Struts2 result wildcard OGNL, struts2ognl
Result:

1). result is a subnode of the action node.

2). result indicates a possible destination after the action method is executed.

3) An action node can be configured with multiple result subnodes.

4). The name attribute value of result corresponds to a possible return value of the action method. <result name = "index">/index. jsp </result>

5). result has two attributes, and another one is type: the response type of the result.

6). The type attribute value of result is defined in the name attribute of the result-types node in the struts-default package.

Commonly used include> dispatcher (default): Forwarding. It is the same as forwarding in Servlet.

> Redirect: Redirection

> RedirectAction: redirects to an Action. Note: The redirectAction function can be conveniently implemented through the redirect response type!

<result name="index" type="redirectAction"><param name="actionName">testAction</param><param name="namespace">/atguigu</param></result>

OR

<result name="index" type="redirect">/atguigu/testAction.do</result>

          > Chain: forward to an Action. Note: you cannot forward data to an Action by type = dispatcher.

<result name="test" type="chain"><param name="actionName">testAction</param><param name="namespace">/atguigu</param></result>

It cannot be:

<result name="test">/atguigu/testAction.do</result>
Wildcard ing

You can use the wildcard ing mechanism provided by struts to simplify multiple similar mappings into one ing relationship.

Wildcard ing rules

-If multiple matches are found, those without wildcards will win.

-If the specified action does not exist, Struts will try to match the URI with any action name containing the wildcard *.

-The substring Of The URI string matched by the wildcard can be referenced by {1}, {2}. {1} matches the first substring, {2} matches the second substring...

-{0} matches the entire URI

-If Struts finds more than one matching rule with a wildcard, it matches the rule in sequence.

-* It can match zero or multiple characters, but does not contain/characters. if you want to include/characters, you need to use **. to escape a character, use \.

Dynamic method call

Dynamic call of methods in Action through url

URI:-/struts-app2/Product. action: Struts call the execute-/struts-app2/Product of the Product class! Save. action: Struts calls the save () method of the Product class. By default, the dynamic method call of Struts is disabled (not recommended)
<! -- Enable the function to allow dynamic method calls. The default value is false --> <constant name = "struts. enable. DynamicMethodInvocation" value = "true"> </constant>
OGNL

On the JSP page, you can use OGNL (Object-Graph Navigation Language: Object-Graph Navigation Language) to access the Object attributes in the value stack (ValueStack.

To access data in ContextMap in the value stack, you must add a prefix character # To the OGNL expression. If no prefix character # exists, the search will be performed in the ObjectStack.

1. About Value Stack:

1) When helloWorld, $ {productName} reads the productName value. In fact, this attribute is not in the request or other domain objects, but obtained from the value stack.

2). ValueStack:

I. You can obtain value stack objects from ActionContext.

II. Value stack is divided into two logical parts

> Map Stack: OgnlContext is actually a Map and a reference to ActionContext.

Various maps are stored in: requestMap, sessionMap, applicationMap, parametersMap, and attr.

> Object Stack: it is actually a CompoundRoot stack defined by ArrayList. It stores various objects related to the current Action instance. It is a stack of data structures.

2. Struts2 uses the s: property tag and OGNL expression to read the attribute values in the value stack.

1). attribute values in the value Stack:

> For object Stack: attribute values of an object in the object Stack

> Map Stack: a request, session, application attribute value or a request parameter value.

2). Read the attributes of objects in the object Stack:

> To access the attributes of an Object in the object Stack, you can use one of the following methods: object. propertyName; object ['propertyname']; Object ["propertyName"]

> Objects in ObjectStack can be referenced by a subscript starting from scratch. the stack top object in ObjectStack can be referenced using [0], and the object below it can be referenced using [1.

> [N] indicates that the search starts from the nth object, instead of searching only the nth object.

> If you start searching from the top object of the stack, You can omit the subscript part: message

> Combined with the s: property Tag: <s: property value = "[0]. message"/> <s: property value = "message"/>

3) by default, the Action object is automatically put to the top of the value stack by Struts2.

Call fields and Methods

You can use OGNL to call

-Static fields or methods in any Java class.

-Public fields and Methods pushed to the ValueStack stack object.

By default, Struts2 does not allow calling any static Java method. You need to reset struts. ognl. allowStaticMethodAccess to true.

To call a static field or method, use the following syntax:

-@ FullyQualifiedClassName @ fieldName: @ java. util. Calendar @ DECEMBER

-@ FullyQualifiedClassName @ methodName (argumentList): @ app4.Util @ now ()

Call the syntax of an instance field or method. The object is a reference of an Object in the object Stack:

-. Object. fieldName: [0]. datePattern

-. Object. methodName (argumentList): [0]. repeat (3, "Hello ");

Access attribute of array type

Some attributes return an object array instead of a single object. They can be read as if they were read from any other object attributes. The elements of this array attribute are separated by commas without square brackets.

You can use subscript to access the specified Element in the array: colors [0]

You can call the length field to find out the number of elements in the given array: colors. length

Access List-type attributes

Some attributes return the type of java. util. List, which can be read as if they were read from any other attributes. Each element of this List is a string separated by commas (,) and is enclosed by square brackets.

You can use subscript to access the specified Element in the List: colors [0].

You can call the size method or the dedicated keyword size to find the length of the given List: colors. size or colors. size ()

You can use the isEmpty () method or the dedicated keyword isEmpty to determine whether the given List is empty. colors. isEmpty or colors. isEmpty ()

You can also use OGNL expressions to create a List. Creating a List is the same as declaring a Java array: {"Red", "Black", "Green "}

Access Map-type attributes

To retrieve the value of a Map, use the following format: map [key]

You can use size or size () to obtain the number of key-value pairs of a given Map.

You can use isEmpty or isEmpty () to check whether a given Map is empty.

You can use the following syntax to create a Map:

Use EL to access the attributes of objects in the value Stack

<S: property value = "fieldName"> you can also use jsp el: $ {fieldName}

Principle: Struts2 uploads the org. apache. struts2.dispatcher. StrutsRequestWrapper object that wraps the HttpServletRequest object to the page, and this class overrides the getAttribute () method.

Exception Handling: exception-mapping Element

Exception-mapping element: Configure declarative exception Handling for the current action

The exception-mapping element has two attributes.

-Exception: Specifies the exception type to be captured. Exception full class name

-Result: Specifies a response result. The result will be executed when a specified exception is caught. It can be either a declaration from the current action or a global-results declaration.

The declarative exception Handling Mechanism is handled by the ExceptionMappingInterceptor Interceptor. When an exception declared by an exception-mapping element is caught, the ExceptionMappingInterceptor interceptor adds two objects to ValueStack:

-Exception: Exception object for caught exceptions

-ExceptionStack: Stack containing caught exceptions

You can use the <s: property> label to display exception messages in the view.

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.