[Web Development Study Notes] Structs2 OGNL Study Notes

Source: Internet
Author: User

[Web Development Study Notes] Structs2 OGNL Study Notes
Part 1: Code

// Front-end 
//web.xml<struts>    <constant name="struts.enable.DynamicMethodInvocation" value="false" />    <constant name="struts.devMode" value="true" />    <constant name="struts.ognl.allowStaticMethodAccess" value="true"></constant><include file="/com/struts2/ognl/ognl.xml"/>   </struts>
//ognl.xml<struts>    <package name="ognl" extends="struts-default">        <action name="ognl" class="com.struts2.ognl.OgnlAction">            <result>/ognl.jsp</result>        </action>        <action name="test" class="com.struts2.ognl.TestAction">            <result type="chain">ognl</result>        </action>    </package></struts>
// Class package // ognl. javapackage 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 <User> users = new ArrayList <User> (); private Set <Dog> dogs = new HashSet <Dog> (); private Map <String, Dog> dogMap = new HashMap <String, Dog> (); 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 <User> getUsers () {return users;} public Map <String, Dog> getDogMap () {return dogMap;} public Set <Dog> getDogs () {return dogs;} public String m () {return "hello";} public void setCat (Cat cat) {this. cat = cat;} public void setDogMap (Map <String, Dog> dogMap) {this. dogMap = dogMap;} public void setDogs (Set <Dog> dogs) {this. dogs = dogs;} public void setPassword (String password) {this. password = password;} public void setUser (User user User) {this. user = user;} public void setUsername (String username) {this. username = username;} public void setUsers (List <User> users) {this. users = users;} // user. javapackage 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 ;}@ Overridepublic String toString () {return "user" + age ;}}
//cat.javapackage 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.javapackage 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";}}
// Display Part 2: Analysis of <a href = "<% = contextPath %>/ognl. action? Username = u & password = p "> ognl </a>

-> Click it and find the file <include file = "/com/struts2/ognl. xml"/> Based on the configuration in the configuration file.
-> Search for the olnl. xml file

->

<struts><package name="ognl" extends="struts-default"><action name="ognl" class="com.struts2.ognl.OgnlAction"><result>/ognl.jsp</result></action><action name="test" class="com.struts2.ognl.TestAction"><result type="chain">ognl</result></action></package>   </struts>

-> 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 must set <constant name = "struts. ognl. allowStaticMethodAccess" in web. xml"
Value = "true"> </constant>
Method To view default settings:/Struts2_1900_OGNL/WebRoot/WEB-INF/lib/struts2-core-2.1.6.jar/default. properties
Access set
-> Call Method
http://localhost:8080/Struts2_1900_OGNL/ognl.action?username=u&password=p&user.age = 19&cat.friend.name =oudy
Part 3: Discuss the Object Graph Navigation Language
Object chart Navigation language

Part 4: projection is the filtering function.
Filter statement:
<Li> projection (filter): <s: property value = "users .{? # This. age = 1} [0] "/> </li> <li> projection: <s: property value =" users. {^ # this. age> 1 }. {age} "/> </li> <li> projection: <s: property value =" users. {$ # this. age> 1 }. {age} "/> </li> <li> projection: <s: property value =" users. {$ # this. age> 1 }. {age }== null "/> </li>
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;
<Li> projection (filter): <s: property value = "users .{? # This. age = 1} "/> </li> indicates to retrieve the one whose age = 1 from users, and the result is a set;
<Li> projection (filter): <s: property value = "users .{? # This. age = 1} [0] "/> </li> indicates that the first entry of age = 1 is retrieved from users, and the result is one. <li> projection: <s: property value = "users. {$ # this. age> 1 }. {age} "/> </li> indicates the one at the end of age> 1 from users;

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.