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
- Action level, naming convention is "class name _ language name _ Domain name"
- Package level, naming convention is "Package_ language name _ field domain Name"
- 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:
- 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 ();}}
- 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
- Client initiated HttpRequest request
- Send the request to Filterdispatcher (strutsprepareandexecutefilter) processing through the configuration in the application Web. xml file
- Filterdispatcher calls Actionmapper to find the action corresponding to the request and gives Actionproxy processing
- Actionproxy looks for the action class to prepare the call and creates a actioninvocation instance that invokes its Invoke method
- 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
- 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
- Reverse executes the second half of each interceptor intercept method, and finally sends the HttpResponse result
[Java Web] STRUTS2 Basic Summary (III.)