Ognl and struts2 tag_personal Learning Log, ognlstruts2

Source: Internet
Author: User

Ognl and struts2 tag_personal Learning Log, ognlstruts2

2016-08-31

I. ognl principle in struts2

Struts2 default expression Language ognl: Object Graph Navigation Language

1> type conversion is required when data in and out of Struts2 is migrated. ognl is used for processing.

2> the ognl can be regarded as a map key-value pair. One of the key-value pairs is the root object, which is transformed into a value Stack in struts2 ),

Non-root objects are called StackContext.

3> In struts2, vlue Stack can store multiple objects, which is equivalent to a Stack space (first in and then out). Search for objects from the top to the bottom.

If it is found, return; if it is not found at the bottom, return null;

4> struts2 provides a type converter for internal data types in Java

· Ognl access value Stack

Action instances are all placed in the value stack, so ognl can directly access * the setter/getter method is required

Ex: <s: property value ="User. name"/>

· Ognl accesses other StackContext

The request, session, application, and page are stored in StackContext #

Ex: <s: property value ="# Request. MSG"/>

Ii. ognl type conversion error message

1> Start StrutsConversionErrorInterceptor interceptor

The Interceptor is already included in the defaultStack interceptor stack.

2> Action must inherit ActIonSupport class

In fact, it is necessary to implement the ValidationAware Interface

3> Configure input result ing

** If an internal error occurs, the INPUT is returned.

4> use the Struts 2 form tag or <s: fielderror filedName ="Xxx"/> Tag

Struts 2 form labels embedded with the output error message function

<S: fielderror> label output conversion error is required for common HTML tags.

5> display Chinese Characters

1) configure the resource file base name in struts. xml:

<Constant name ="Struts. custom. i18n. resources"Value ="Msg"/>

2) Create the msg. properties file under src

The value of xwork. default. invalid. fieldvalue = field "{0}" is invalid.

 

* ** Different prompts for each field

1) Write a proprietary resource file for each Action. The file name is the same as the Action name and is in the same directory:

DoSubmitAction. properties

2) invalid. fieldValue. user. birthday = prompt message

Iii. Implement custom type converter

1> inherit from org. apache. struts2.util. StrutsTypeConverter abstract class override

Public Object convertFromString (Map arg0, String [] values, Class clz );

Public String convertToString (Map arg0, Object loc );

2> Configuration

Create a xwork-conversion.properties In the src directory

Add: conversion class full name = type converter class full name

Iv. struts2 tag (I suggest some common instance usage)

· Struts labels are generally used in combination with ognl. Common tags: Common tags (data tags, control tags), UI tags, and Ajax tags

First, use the Strtuts label on the jsp page to add: <% @ taglib prefix ="S"Uri ="/Struts-tags"%>

-- Data tag

<S: set name ="Fruits"Value ="{'Apple','Bananas','Pear'}"> </S: set>

<S: set name ="Countrys"Value ="# {'Cn': 'China', 'us': 'America ', 'uk': 'British '}"> </S: set>

Can be used to create list and map

<S: property value ="# Fruits [1]"/> <Br/>

<S: property value ="# Countrys.cn"/>

Access the created list and map

<S: property value ="@ Java. lang. Math @ floor (3.1 )"/> Call static methods

** You must set <constant name = in struts. xml."Struts. ognl. allowStaticMethodAccess"Value ="True"/>

<S: url value =Http://www.sohu.com"Var ="Sohu">

<S: param name ="Age"> 18 </s: param>

<S: param name ="Uname"> Z3 </s: param>

</S: url>

Set the url address and pass the parameter <s: a href ="% {# Sohu }"> Sohu news </s: a>

<S: include value =""> </S: include>

Include other pages, which is equivalent to <jsp: include>

<S: date name ="The formatted value is required."Format ="Yyyy-MM-dd"/> Specify the format and date output

<S: property value ="'<Hr/>'"EscapeHtml ="True"/> Escape?

<S: debug/> is used to observe the value stack and StackContext content.

-- Control label

<S: if test =""> </S: if> <s: elseif test =""> </S: elseif> <s: else> </s: else>

Equal to if () {} else {}

<S: iterator value =""Status ="">

<S: property/>

</S: iterator> iterator

-- UI tag (struts2 built-in themes: simple, xhtml, css_xhtml, ajax)

<S: textfield name =""Value =""Maxlength ="Maximum length"Readonly =""> </S: textfield> single-line text box

<S: textarea name =""Value =""Cols ="Number of Columns"Rows ="Number of rows"Readonly =""> </S: textarea> multi-line text box

<S: submit> </s: submit> submit button

<S: select list =""> </S: select> drop-down menu

<S: form name ="F"> ** The form alias must be declared.

<S: doubleselect formName ="F"List =""DoubleList =""DoubleName =""> </S: doubleselect> List cascade drop-down box

</S: form>

* ** Note: when entering the value, if the value may be ambiguous (for example, if the name or name is unknown to the String "name" or variable name) struts2 first recognizes the content as a String. To identify it as a variable, add %: % variable name. However, in <s: property value ="Name"/>, Except for the variable name. If you want it to be recognized as a string: <s: property value ="'Name '"/>

 

The above is just a summary of the new study ***

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.