"Web Development Learning note" STRUCTS2 OGNL study notes

Source: Internet
Author: User

First part: Code
Front 
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 Pack//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 cat;private list<user> users = new arraylist<user> ();p rivate Se T<dog> dogs = new hashset<dog> ();p rivate map<string, dog> dogmap = new hashmap<string, Dog> ();p UB Lic ognlaction () {Users.add (new User (1)), Users.add (new User (2)), Users.add (new User (3));d Ogs.add (New Dog ("Dog1")); Dogs.add ("dog2");d Ogs.add (New Dog ("Dog3"));d ogmap.put ("dog100", New Dog ("dog100"));d ogmap.put ("dog101", New Dog ("dog101"));d ogmap.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) {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.) {super (); t His.age = age;} public int getage () {return age;} public void Setage (int.) {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 II: Analysis<a href= "<%=contextpath%>/ognl.action?username=u&password=p" >ognl</a>
After clicking, locate the file containing <include file= "/com/struts2/ognl/ognl.xml" based on the configuration in the configuration file/>
, find 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>

Constructs the class object according to the class associated with the action in the XML configuration file;
The Ognl object is constructed according to Ognl.java;
Display stage to show the value, there are several ways to pay attention to:
Access value stack in the properties, the value stack of the object's methods, static methods, static properties, container objects;
Accessing the normal properties of an action in the value stack
Normal properties of objects in the value stack
Common methods for objects in the value stack
Common method of action in value stack

Static method access Method @ class name @ Method name

Need to set <constant name= "Struts.ognl.allowStaticMethodAccess" in Web. xml
Value= "true" ></constant>
Ways to view the default settings:/struts2_1900_ognl/webroot/web-inf/lib/struts2-core-2.1.6.jar/default.properties
Accessing collections
Call Method

Http://localhost:8080/Struts2_1900_OGNL/ognl.action?username=u&password=p&user.age = 19& Cat.friend.name =oudy
Part III: DiscussionObject Graph Navigation Language
Object Graph Navigation Language

Part IV: Discussion of projectionsThe projection is the filtering function.
Filter wording:

<li> projection (filtering): <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 filter conditions, and regular expressions are not the same;
This represents the object in the loop process and helps you to do some loops;

<li> projection (filtering): <s:property value= "users. {? #this. Age==1} "/></li> represents the one from the age=1 removed from the users, and the result is a collection;
<li> projection (filtering): <s:property value= "users. {? #this. Age==1} [0] "/></li> represents the first of the age=1 from the users, the result is one;<li> projection: <s:property value= "users. {$ #this. age>1}.{age} '/></li> represents the end of the collection of age>1 from the 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.