Introduction to structs2 projection
Part 1: Code
// Front end
Access attributes
// Web. xml
// Ognl. xml
/Ognl. jsp
Ognl
// Class package
// Ognl. java
Package com. struts2.ognl;
Import java. util. ArrayList;
Import java. util. HashMap;
Import java. util. HashSet;
Import java. util. List;
Import java. util. Map;
Import java. util. Set;
Import com. opensymphony. xwork2.ActionSupport;
Public class OgnlAction extends ActionSupport {
Private String password;
Private String username;
Private User user;
Private Cat;
Private List Users = new ArrayList ();
Private Set Dogs = new HashSet ();
Private Map DogMap = new HashMap ();
Public OgnlAction (){
Users. add (new User (1 ));
Users. add (new User (2 ));
Users. add (new User (3 ));
Dogs. add (new Dog (dog1 ));
Dogs. add (new Dog (dog2 ));
Dogs. add (new Dog (dog3 ));
DogMap. put (dog100, new Dog (dog100 ));
DogMap. put (dog101, new Dog (dog101 ));
DogMap. put (dog102, new Dog (dog102 ));
}
Public String execute (){
Return SUCCESS;
}
Public String getPassword (){
Return password;
}
Public String getUsername (){
Return username;
}
Public User getUser (){
Return user;
}
Public Cat getCat (){
Return cat;
}
Public List GetUsers (){
Return users;
}
Public Map GetDogMap (){
Return dogMap;
}
Public Set GetDogs (){
Return dogs;
}
Public String m (){
Return hello;
}
Public void setCat (Cat cat ){
This. cat = cat;
}
Public void setDogMap (Map DogMap ){
This. dogMap = dogMap;
}
Public void setDogs (Set Dogs ){
This. dogs = dogs;
}
Public void setPassword (String password ){
This. password = password;
}
Public void setUser (User user ){
This. user = user;
}
Public void setUsername (String username ){
This. username = username;
}
Public void setUsers (List Users ){
This. users = users;
}
}
// User. java
Package com. struts2.ognl;
Public class User {
Private int age = 8;
Public User (){
}
Public User (int age ){
Super ();
This. age = age;
}
Public int getAge (){
Return age;
}
Public void setAge (int age ){
This. age = age;
}
@ Override
Public String toString (){
Return user + age;
}
}
// Cat. java
Package com. struts2.ognl;
Public class Cat {
Private Dog friend;
Public Dog getFriend (){
Return friend;
}
Public void setFriend (Dog friend ){
This. friend = friend;
}
Public String miaomiao (){
Return miaomiao;
}
}
// Dog. java
Package com. struts2.ognl;
Public class Cat {
Private Dog friend;
Public Dog getFriend (){
Return friend;
}
Public void setFriend (Dog friend ){
This. friend = friend;
}
Public String miaomiao (){
Return miaomiao;
}
}
?
1
// Display
Common attribute of the action in the Access value Stack: username =
Common Properties of objects in the Access value stack (get set Method): ||| wrong: <% ---- %>
Common Properties of objects in the Access value stack (get set Method ):
Common methods for accessing objects in the value Stack:
Common methods for accessing objects in the value Stack:
Common methods for accessing actions in the value Stack:
Static access method:
Access static attributes:
Static methods for accessing the Math class:
Constructor for accessing common classes:
Access List:
Access an element in the List:
Access the set of an attribute of an element in the List:
Access the specific value in the set of an attribute of the element in the List: |
Access Set:
Access an element in the Set:
Access Map:
Access an element in Map: |
Access all keys in the Map:
Access all values in Map:
Access container size: |
Projection (filter ):
Projection: 1}. {age}/>
Projection: 1}. {age}/>
Projection: 1}. {age }== null/>
[]:
Part 2: Analysis
-> Click it and find the file inclusion according to the configuration in the configuration file.
-> Search for the ognl. xml file
/Ognl. jsp
Ognl
-> Construct class objects based on the classes associated with the actions in the xml configuration file;
-> Construct an ognl object based on ognl. java;
-> Values are displayed in the display phase. You must note the following methods for access:
-> Access attributes in the value stack, methods of objects in the value stack, static methods, static attributes, and container objects;
Common attribute of the action in the Access value Stack
Common attributes of objects in the value Stack
Common Methods of objects in the value Stack
Common action methods in the value Stack
Static Method access method @ Class Name @ method name
You need to set value = true> in web. xml.
Method To view default settings:/Struts2_1900_OGNL/WebRoot/WEB-INF/lib/struts2-core-2.1.6.jar/default. properties
Access set
-> Call Method
?
1
Http: // localhost: 8080/Struts2_1900_OGNL/ognl. action? Username = u & password = p & user. age = 19 & cat. friend. name = oudy
Part 3; Discussion
Object Graph Navigation Language
Object chart Navigation language
Part 4: Projection
Projection is the filtering function.
Filter statement:
?
1
Projection (filter ):
Projection: 1}. {age}/>
Projection: 1}. {age}/>
Projection: 1}. {age }== null/>
To? ^ $ Starts with a filter condition, which is different from a regular expression;
This indicates the objects in the loop process and helps you do some loops;
Projection (filter ):
The one whose age is 1 is retrieved from users. The result is a set;
Projection (filter ):
Indicates that the first entry of age = 1 is retrieved from users. The result is one;
Projection: 1}. {age}/>
The end of the set age> 1 is retrieved from users;