Home Struts Struts Struts2.2.1 Tags Access properties of bean from request object using OGNL.

Ask Questions?

View Latest Questions


 
 

Access properties of bean from request object using OGNL.
Posted on: January 27, 2011 at 12:00 AM
Advertisement
In this section, you will see how to access properties of bean from request object in struts.

Access properties of bean from request object using OGNL.

In this section, we will introduce you how to access beans properties from request object using OGNL expression language.

 1-index.jsp

<%@page language="java" session="true" pageEncoding="ISO-8859-1"%>

<%@taglib uri="/struts-tags" prefix="s"%>

<html>

<head><title>Example_OGNL_Struts2</title></head>

<body ><h1></h1><table><tr>

<td align="center" valign="top" bgcolor="#CCCCFF">

<br>Select Student Name and Age....

<s:actionerror/>

<s:form action="ognlAction">

<s:select list="{'Bharat','Gyan','Ankit','Vinay'}"

label="Name" headerKey="none" headerValue="None"

name="studentname"/>

<s:textfield label="Age" name="age"></s:textfield>

<s:submit></s:submit>

</s:form>

</td>

<td align="center" valign="top">

|Accessing Bean Properties |

<table cellpadding="2" cellspacing="0" width="200">

<tr bgcolor="#CC66FF"> <td>Student name</td> <td>Age</td></tr><tr>

<td><s:property value="studentname"/></td>

<td><s:property value="age"/></td></tr></table>

</td>

<td align="center" valign="top">

|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></td>

</tr>

</table>

</body>

</html>

3_ OGNLOnsession.java (Action class)

package roseindia.action;

import javax.servlet.http.HttpServletRequest;

import org.apache.struts2.interceptor.ServletRequestAware;

import roseindia.model.StudentDetails;

import com.opensymphony.xwork2.ActionSupport;

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;  }

}

4_ EmployeeBean.java (Bean class)

package roseindia.model;

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;

} 

}

5_ struts.xml

<struts>

<constant name="struts.devMode" value="false" />

<package name="roseindia" extends="struts-default">

<action name="ognlAction" class="roseindia.action.OGNLAction">

<result name="error">index.jsp</result>

<result name="success">index.jsp</result>

<result name="input">index.jsp</result>

</action>

</package>

</struts>

Output

Download Source Code

Related Tags for Access properties of bean from request object using OGNL.:


DMCA.com