[Java Web] STRUTS2 Basic Summary (III.)

Source: Internet
Author: User
Tags i18n naming convention try catch custom name

OGNL-expression


OGNL (object-graph Navigation Language) is a convenient way to manipulate the open-source expression language of object properties, making the page more concise. The main uses are:

Accessing the properties of an action in the value stack

Username = <s:property value= "username"/>
Accessing the properties of an object in the value stack (requires a Get Set method)

<s:property value= "User.age"/> | <s:property value= "user[' age '"/> | <s:property value= "user[\" age\ "]"/>

Methods for accessing objects in the value stack

<s:property value= "password.length ()"/>
Ways to access action in the value stack

<s:property value= "Add ()"/>
Accessing static methods

<s:property value= "@[email protected] ()"/>
accessing static properties

<s:property value= "@[email protected]"/>
Access Construction Methods

<s:property value= "New Struts.action.User (8)"/>

Visit list/set/map

<s:property value= "Users"/>

Accessing an element in List/set

<s:property value= "users[1]"/>
Access a collection of attributes for an element in list

<s:property value= "users. {Age} "/>
Accessing the properties of an element in a list

<s:property value= "users. {Age} [0] "/> | <s:property value= "Users[0].age"/>
Accessing an element in a map

<s:property value= "Users.zhangsan"/> | <s:property value= "users[' Zhangsan ']"/> | <s:property value= "users[\" zhangsan\ "]"/>
Access all keys in the map

<s:property value= "Users.keys"/>
Access all the value in the map

<s:property value= "Users.values"/>
Size of the Access container

<s:property value= "users.size ()"/> | <s:property value= "Users.size"/>
Projection? Select all elements that meet the criteria)

<s:property value= "users. {? #this. Age==1} [0] "/>
Projection^ Select the first element that satisfies a condition)

<s:property value= "users. {^ #this. age>1}. {Age}
Projection$ Select the last element that satisfies the criteria)

<s:property value= "users. {$ #this. age>1}. {Age} "/>

Declarative Exception Handling


Discard the try catch handling exception in the program, which can be placed in the <action> tab by <exception-mapping> processing in struts.xml, or as the default processing scheme in the < Global-exception-mapping>, for example:

<global-results>    <result name= "error" >/error.jsp</result></global-results>    < global-exception-mappings>    <exception-mapping result= "error" exception= "Java.lang.Exception" ></ exception-mapping></global-exception-mappings>     <action name= "user" class= " Struts.action.UserAction ">       <result>/admin.jsp</result>       <exception-mapping result=" Error "exception=" Java.sql.SQLException "/>         <result name=" error ">/error.jsp</result></ Action>

i18n of international treatment

For the internationalization of the project, the front page can be removed from the configuration file by <s:text> or <s:i18n> through the Actionsupport GetText method, and the configuration file has three kinds of wording

    1. Action level, naming convention is "class name _ language name _ Domain name"
    2. Package level, naming convention is "Package_ language name _ field domain Name"
    3. Global level, the naming convention is "Custom name _ language name _ Domain name", and also in the Struts.xml file to configure
      <constant name= "struts.custom.i18n.resources" value= "Custom Name" ></constant>   

Custom type converters

When a property in an object does not have a Get set method, it is converted by a custom type converter by:

    1. Implement a converter class, inherit the Strutstypeconverter class, override the ConvertFromString and ConvertToString methods:
      public class Mypointconverter extends strutstypeconverter{@Overridepublic Object convertfromstring (Map context, String [] values, Class toclass) {point P = new Point (); String[] STRs = (string[]) values; string[] xy = Strs[0].split (",");p. x = Integer.parseint (xy[0]);p. y = Integer.parseint (xy[1]); return p;} @Overridepublic String converttostring (Map context, Object o) {//TODO auto-generated method Stubreturn o.tostring ();}}

    2. Establish the Properties file configuration information, if it is the action level, the file name is "class name-conversion", in the format "Property name = Converter class name". If the global level file is named "Xwork-conversion", the format is "need to convert class name = Converter Class name"

Struts Work Flow


    1. Client initiated HttpRequest request
    2. Send the request to Filterdispatcher (strutsprepareandexecutefilter) processing through the configuration in the application Web. xml file
    3. Filterdispatcher calls Actionmapper to find the action corresponding to the request and gives Actionproxy processing
    4. Actionproxy looks for the action class to prepare the call and creates a actioninvocation instance that invokes its Invoke method
    5. The Invoke method of Actioninvocation looks for the next interceptor (Intercepter) in the configuration file and calls its intercept method, which invokes the Execute method of the action class if there is no next interceptor
    6. The Invoke method of Actioninvocation is also called in the execution of each intercept method, and when all interceptors call the Invoke method, the Execute method of the action class is called and the result is obtained
    7. Reverse executes the second half of each interceptor intercept method, and finally sends the HttpResponse result











[Java Web] STRUTS2 Basic Summary (III.)

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.