The Value Stack:
----------------
The value stack is a set of several objects which keeps the following objects in the provided order:
Objects & Description
---------------------
1. Temporary Objects
-There are various temporary objects which are created during execution of a page. For example the current iteration value for a collection being looped over in a JSP tag.
2. The Model Object
If you are using model objects in your struts application, the current model object is placed before the action on the value stack
3. The Action Object
This will be the current action object which is being executed.
4. Named Objects
These objects include #application, #session, #request, #attr and #parameters and refer to the corresponding servlet scopes
You can get valueStack object inside your action as follows:
ActionContext.getContext().getValueStack()
Once you have a ValueStack object, you can use following methods to manipulate that object:
ValueStack Methods & Description
-------------------------------
1. Object findValue(String expr)
Find a value by evaluating the given expression against the stack in the default search order.
2. CompoundRoot getRoot()
Get the CompoundRoot which holds the objects pushed onto the stack.
3. Object peek()
Get the object on the top of the stack without changing the stack.
4. Object pop()
Get the object on the top of the stack and remove it from the stack.
5. void push(Object o)
Put this object onto the top of the stack.
6. void set(String key, Object o)
Sets an object on the stack with the given key so it is retrievable by findValue(key,...)
7. void setDefaultType(Class defaultType)
Sets the default type to convert to if no type is provided when getting a value.
8. void setValue(String expr, Object value)
Attempts to set a property on a bean in the stack with the given expression using the default search order.
9. int size()
Get the number of objects in the stack.
Ex:
Java Part
---------
ValueStack stack =
ActionContext.getContext().getValueStack();
Map<String, Object> context =
new HashMap<String, Object>();
context.put("key1", new String("This is key1"));
context.put("key2", new String("This is key2"));
stack.push(context);
JSP Part
-------
<body>
Value of key 1 : <s:property value="key1" /><br/>
Value of key 2 : <s:property value="key2" /> <br/>
</body>
------------------------------------------------------------------------------------------------------------------------
OGNL in struts
---------------
- Object Graph Navigation Language is a expression language.
- It is used for getting and setting the properties of java object.
- It is very useful binding language for manipulating and retrieving different properties of java object.
- It has own syntax, which is very simple.
- It make code more readable.
- It acts as a binding language between GUI elements and model objects.
- The Struts framework used a standard naming context for evaluating an OGNL expression.
- It sets OGNL context to be the ActionContext, and ValueStack to be the OGNL root object.
The ActionContext map consists of the following:
1. application - application scoped variables
2. session - session scoped variables
3. root / value stack - all your action variables are stored here
4. request - request scoped variables
5. parameters - request parameters
6. atributes - the attributes stored in page, request, session and application scope
Objects in the ActionContext are referred using the pound symbol, however, the objects in the value stack can be directly referenced,
Syntax of OGNL :
The chain contain following parts :
1. Property Names - Property names, such as name and text.
2. Methods calls - Method calls, such as the hashcode(), which returns hash code of current object.
3. Array Indices - For example, array[0], which returns first element of current object.
For example.
name.toCharArray()[0].numericValue.toString()
Description :
1. The name is the property of the initial or root object. This root object provided to the OGNL through the OGNL context.
2. Then it calls the toCharArray() of the resulting string , After calling toCharArray() method, this first character at 0 index is extracted from the resulting array.
3. It then gets the numericValue property from that character.
4. Finally the String is returned after calling the toString() on resulting Integer object.
-------------------------------------------------------------------------------------------------------------------
Access value of array using OGNL
JSP Part:
<body><h1>OGNL_Example</h1><hr>
Size of list : <s:property value="name.size"/><br/>
<p> access data of list.</p><br/>
name[3] : <s:property value="name[3]"/><br/>
name[2] : <s:property value="name[2]"/><br/>
name[1] : <s:property value="name[1]"/><br/>
name[0] : <s:property value="name[0]"/><br/>
Value array list : <s:property value="name"/>
</body>
Action Class
public class OGNLAction extends ActionSupport{
private List<String> name=
new ArrayList<String>();
{
name.add("User1");
name.add("User2");
name.add("User3");
name.add("User4");
}
public String execute() throws Exception {
return SUCCESS;
}
public List<String> getName() {
return name;
}
public void setName(List<String> name) {
this.name = name;
}
}
-----------------------------------------------------------------------------------------------------------
Access session value using OGNL
JSP Part
--------
<s:iterator id="beans" value="#session['beans']">
<tr>
<td><s:property value="empName"/></td>
<td><s:property value="lang"/></td>
</tr>
</s:iterator>
Java Bean Class
---------------
public class EmployeeBean {
private String lang;
private String empName;
public String getLang() {
return lang;
}
public void setLang(String lang) {
this.lang = lang;
}
public String getEmpName() {
return empName;
}
public void setEmpName(String empName) {
this.empName = empName;
}
}
Action Class
------------
public class OGNLOnsessionObject extends ActionSupport {
private List langName;
private String lang;
private String empName;
ActionContext context =
ActionContext.getContext();
Map session = context.getSession();
public String getLang() {
return lang;
}
public void setLang(String lang) {
this.lang = lang;
}
public String getEmpName() {
return empName;
}
public void setEmpName(String empName) {
this.empName = empName;
}
public List getLangName() {
return langName;
}
public void setLangName(List langName) {
this.langName = langName;
}
public OGNLOnsessionObject() {
langName = new ArrayList();
langName.add("C");
langName.add("c++");
langName.add("JAVA");
langName.add("PHP");
}
public String show() {
return INPUT;
}
public String execute() {
if (lang.equals("none")) {
this.addActionError("Please select any language.");
return ERROR;
}
ArrayList beans = null;
EmployeeBean bean = new EmployeeBean();
bean.setEmpName(empName);
bean.setLang(lang);
beans = (ArrayList) session.get("beans");
if (beans == null)
beans = new ArrayList();
beans.add(bean);
session.put("beans", beans);
return SUCCESS;
}
}
-----------------------------------------------------------------------------------------------------------
Access properties of bean from request object using OGNL
JSP Part
--------
|Accessing Request |
<table cellpadding="2" cellspacing="0" width="200">
<tr bgcolor="#CC66FF">
<td>Student Name</td> <td>Age</td>
</tr>
<tr>
<td>
<s:property
value="#request['bean'].studentname"/>
</td>
<td>
<s:property value="#request['bean'].age"/>
</td>
</tr>
</table>
Bean Part
---------
public class StudentDetails {
private String studentname;
private int age;
public String getStudentname() {
return studentname;
}
public void setStudentname(String studentname) {
this.studentname = studentname;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
}
Action Class
------------
public class OGNLAction extends ActionSupport implements ServletRequestAware {
private HttpServletRequest request;
private String studentname;
private int age;
public String getStudentname() {
return studentname;
}
public void setStudentname(String studentname) {
this.studentname = studentname;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public void setServletRequest(HttpServletRequest request) {
this.request = request;
}
public HttpServletRequest getServletRequest() {
return request;
}
public String execute() throws Exception {
if (studentname.equals("none")) {
this.addActionError("Please Select name...");
return ERROR;
}
StudentDetails bean = new StudentDetails();
bean.setStudentname(studentname);
bean.setAge(age);
request.setAttribute("bean", bean);
return SUCCESS;
}
}
-------------------------------------------------------------------------------------------
----------------
The value stack is a set of several objects which keeps the following objects in the provided order:
Objects & Description
---------------------
1. Temporary Objects
-There are various temporary objects which are created during execution of a page. For example the current iteration value for a collection being looped over in a JSP tag.
2. The Model Object
If you are using model objects in your struts application, the current model object is placed before the action on the value stack
3. The Action Object
This will be the current action object which is being executed.
4. Named Objects
These objects include #application, #session, #request, #attr and #parameters and refer to the corresponding servlet scopes
You can get valueStack object inside your action as follows:
ActionContext.getContext().getValueStack()
Once you have a ValueStack object, you can use following methods to manipulate that object:
ValueStack Methods & Description
-------------------------------
1. Object findValue(String expr)
Find a value by evaluating the given expression against the stack in the default search order.
2. CompoundRoot getRoot()
Get the CompoundRoot which holds the objects pushed onto the stack.
3. Object peek()
Get the object on the top of the stack without changing the stack.
4. Object pop()
Get the object on the top of the stack and remove it from the stack.
5. void push(Object o)
Put this object onto the top of the stack.
6. void set(String key, Object o)
Sets an object on the stack with the given key so it is retrievable by findValue(key,...)
7. void setDefaultType(Class defaultType)
Sets the default type to convert to if no type is provided when getting a value.
8. void setValue(String expr, Object value)
Attempts to set a property on a bean in the stack with the given expression using the default search order.
9. int size()
Get the number of objects in the stack.
Ex:
Java Part
---------
ValueStack stack =
ActionContext.getContext().getValueStack();
Map<String, Object> context =
new HashMap<String, Object>();
context.put("key1", new String("This is key1"));
context.put("key2", new String("This is key2"));
stack.push(context);
JSP Part
-------
<body>
Value of key 1 : <s:property value="key1" /><br/>
Value of key 2 : <s:property value="key2" /> <br/>
</body>
------------------------------------------------------------------------------------------------------------------------
OGNL in struts
---------------
- Object Graph Navigation Language is a expression language.
- It is used for getting and setting the properties of java object.
- It is very useful binding language for manipulating and retrieving different properties of java object.
- It has own syntax, which is very simple.
- It make code more readable.
- It acts as a binding language between GUI elements and model objects.
- The Struts framework used a standard naming context for evaluating an OGNL expression.
- It sets OGNL context to be the ActionContext, and ValueStack to be the OGNL root object.
The ActionContext map consists of the following:
1. application - application scoped variables
2. session - session scoped variables
3. root / value stack - all your action variables are stored here
4. request - request scoped variables
5. parameters - request parameters
6. atributes - the attributes stored in page, request, session and application scope
Objects in the ActionContext are referred using the pound symbol, however, the objects in the value stack can be directly referenced,
Syntax of OGNL :
The chain contain following parts :
1. Property Names - Property names, such as name and text.
2. Methods calls - Method calls, such as the hashcode(), which returns hash code of current object.
3. Array Indices - For example, array[0], which returns first element of current object.
For example.
name.toCharArray()[0].numericValue.toString()
Description :
1. The name is the property of the initial or root object. This root object provided to the OGNL through the OGNL context.
2. Then it calls the toCharArray() of the resulting string , After calling toCharArray() method, this first character at 0 index is extracted from the resulting array.
3. It then gets the numericValue property from that character.
4. Finally the String is returned after calling the toString() on resulting Integer object.
-------------------------------------------------------------------------------------------------------------------
Access value of array using OGNL
JSP Part:
<body><h1>OGNL_Example</h1><hr>
Size of list : <s:property value="name.size"/><br/>
<p> access data of list.</p><br/>
name[3] : <s:property value="name[3]"/><br/>
name[2] : <s:property value="name[2]"/><br/>
name[1] : <s:property value="name[1]"/><br/>
name[0] : <s:property value="name[0]"/><br/>
Value array list : <s:property value="name"/>
</body>
Action Class
public class OGNLAction extends ActionSupport{
private List<String> name=
new ArrayList<String>();
{
name.add("User1");
name.add("User2");
name.add("User3");
name.add("User4");
}
public String execute() throws Exception {
return SUCCESS;
}
public List<String> getName() {
return name;
}
public void setName(List<String> name) {
this.name = name;
}
}
-----------------------------------------------------------------------------------------------------------
Access session value using OGNL
JSP Part
--------
<s:iterator id="beans" value="#session['beans']">
<tr>
<td><s:property value="empName"/></td>
<td><s:property value="lang"/></td>
</tr>
</s:iterator>
Java Bean Class
---------------
public class EmployeeBean {
private String lang;
private String empName;
public String getLang() {
return lang;
}
public void setLang(String lang) {
this.lang = lang;
}
public String getEmpName() {
return empName;
}
public void setEmpName(String empName) {
this.empName = empName;
}
}
Action Class
------------
public class OGNLOnsessionObject extends ActionSupport {
private List langName;
private String lang;
private String empName;
ActionContext context =
ActionContext.getContext();
Map session = context.getSession();
public String getLang() {
return lang;
}
public void setLang(String lang) {
this.lang = lang;
}
public String getEmpName() {
return empName;
}
public void setEmpName(String empName) {
this.empName = empName;
}
public List getLangName() {
return langName;
}
public void setLangName(List langName) {
this.langName = langName;
}
public OGNLOnsessionObject() {
langName = new ArrayList();
langName.add("C");
langName.add("c++");
langName.add("JAVA");
langName.add("PHP");
}
public String show() {
return INPUT;
}
public String execute() {
if (lang.equals("none")) {
this.addActionError("Please select any language.");
return ERROR;
}
ArrayList beans = null;
EmployeeBean bean = new EmployeeBean();
bean.setEmpName(empName);
bean.setLang(lang);
beans = (ArrayList) session.get("beans");
if (beans == null)
beans = new ArrayList();
beans.add(bean);
session.put("beans", beans);
return SUCCESS;
}
}
-----------------------------------------------------------------------------------------------------------
Access properties of bean from request object using OGNL
JSP Part
--------
|Accessing Request |
<table cellpadding="2" cellspacing="0" width="200">
<tr bgcolor="#CC66FF">
<td>Student Name</td> <td>Age</td>
</tr>
<tr>
<td>
<s:property
value="#request['bean'].studentname"/>
</td>
<td>
<s:property value="#request['bean'].age"/>
</td>
</tr>
</table>
Bean Part
---------
public class StudentDetails {
private String studentname;
private int age;
public String getStudentname() {
return studentname;
}
public void setStudentname(String studentname) {
this.studentname = studentname;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
}
Action Class
------------
public class OGNLAction extends ActionSupport implements ServletRequestAware {
private HttpServletRequest request;
private String studentname;
private int age;
public String getStudentname() {
return studentname;
}
public void setStudentname(String studentname) {
this.studentname = studentname;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public void setServletRequest(HttpServletRequest request) {
this.request = request;
}
public HttpServletRequest getServletRequest() {
return request;
}
public String execute() throws Exception {
if (studentname.equals("none")) {
this.addActionError("Please Select name...");
return ERROR;
}
StudentDetails bean = new StudentDetails();
bean.setStudentname(studentname);
bean.setAge(age);
request.setAttribute("bean", bean);
return SUCCESS;
}
}
-------------------------------------------------------------------------------------------
No comments:
Post a Comment