Ognl: an object map navigation language, which is a powerful expression language. It uses a simple and consistent syntax to not only access attributes of objects but also call object methods, it can traverse the structure of the entire object and convert object property types. I have written a blog: Workshop. First look at the JSP page ognl_info.jsp:
<% @ Page Language = "Java" Import = "Java. util. * "pageencoding =" UTF-8 "%> <% @ taglib uri ="/Struts-tags "prefix =" S "%> <! Doctype HTML public "-// W3C // dtd html 4.01 transitional // en"> <HTML>
User entity class user. Java:
Package COM. tjcyjd. web. action; import Java. util. date;/*** entity class ***/public class user {private integer ID; private string loginname; private Double score; private Boolean gender; private character cha; private date birthday; public user () {} public user (integer ID, string loginname, Double score, Boolean gender, character Cha, date birthday) {This. id = ID; this. loginname = loginname; this. score = score; this. gender = gender; this. CHA = CHA; this. birthday = birthday;} public integer GETID () {return ID;} public void setid (integer ID) {This. id = ID;} Public String getloginname () {return loginname;} public void setloginname (string loginname) {This. loginname = loginname;} public double getscore () {return score;} public void setscore (Double score) {This. score = score;} public Boolean getgender () {return gender;} public void setgender (Boolean gender) {This. gender = gender;} public character getcha () {return cha;} public void setcha (character CHA) {This. CHA = CHA;} public date getbirthday () {return birthday;} public void setbirthday (date birthday) {This. birthday = birthday;} Public String Info () {return "User [Birthday =" + birthday + ", CHA =" + CHA + ", Gender =" + gender + ", id = "+ ID +", loginname = "+ loginname +", score = "+ score +"] ";}}
Action class ognlaction:
Package COM. tjcyjd. web. action; import Java. util. arraylist; import Java. util. date; import Java. util. hashmap; import Java. util. linkedhashset; import Java. util. list; import Java. util. map; import Java. util. set; import Org. apache. struts2.servletactioncontext; import COM. opensymphony. xwork2.actioncontext; import COM. opensymphony. xwork2.actionsupport;/*** ognl action class **/public class ognlaction extends actionsupport {Private Static final long serialversionuid =-2554018432709689579l; private string loginname; private string PWD; private user; private set <string> courseset; private list <string> list; private Map <string, string> map; private list <user> userlist; Public String execute () throws exception {This. loginname = "Li Xiaohong"; this. user = new user (123, "Liu mengmiao", 88.9, true, 'B', new date (); this. courseset = new linkedhashset <string> (); this. courseset. add ("corejava"); this. courseset. add ("JSP/servlet"); this. courseset. add ("s2sh"); this. list = new arraylist <string> (this. courseset); this. map = new hashmap <string, string> (); this. map. put ("X", "XXX"); this. map. put ("Y", "yyy"); this. map. put ("Z", "Zzz"); actioncontext context = actioncontext. getcontext (); context. put ("uname", "yuntianhe"); context. put ("inte", integer. valueof (888888); context. put ("user2", new user (123, "Han Ling Sha", 99.1, false, 'B', new date (); this. userlist = new arraylist <user> (); this. userlist. add (new user (1, "Zs", 48.9, true, 'D', new date (); this. userlist. add (new user (2, "ls", 68.1, true, 'C', new date (); this. userlist. add (new user (3, "ww", 78.2, false, 'B', new date (); this. userlist. add (new user (4, "ZL", 88.3, true, 'A', new date (); // ------------------------------------------------------------------- // recommended method: the context will not be coupled with the servlet API. put ("reqatt", "Put attributes in actioncontext"); context. getsession (). put ("sesatt", "To actioncontext. put attribute in getsession () "); context. getapplication (). put ("appatt", "To actioncontext. put attribute in getapplication () "); servletactioncontext. getrequest (). setattribute ("reqatt2", "attributes in request scope"); servletactioncontext. getrequest (). getsession (). setattribute ("sesatt2", "attributes in session scope"); servletactioncontext. getservletcontext (). setattribute ("appatt2", "attributes in Application Scope"); Return success;} Public String getappname () {return "this is the sample code for using ognl";} Public String getloginname () {return loginname;} public void setloginname (string loginname) {This. loginname = loginname;} Public String getpwd () {return PWD;} public void setpwd (string PWD) {This. pwd = PWD;} public user getuser () {return user;} public void setuser (User user) {This. user = user;} public set <string> getcourseset () {return courseset;} public void setcourseset (set <string> courseset) {This. courseset = courseset;} public list <string> getlist () {return list;} public void setlist (list <string> List) {This. list = List;} public Map <string, string> getmap () {return map;} public void setmap (Map <string, string> map) {This. map = map;} public list <user> getuserlist () {return userlist;} public void setuserlist (list <user> userlist) {This. userlist = userlist ;}}