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"%>ADS_TO_REPLACE_1 <%@taglib uri="/struts-tags" prefix="s"%> <html> <head><title>Example_OGNL_Struts2</title></head>ADS_TO_REPLACE_2 <body ><h1></h1><table><tr> <td align="center" valign="top" bgcolor="#CCCCFF"> <br>Select Student Name and Age....ADS_TO_REPLACE_3 <s:actionerror/> <s:form action="ognlAction"> <s:select list="{'Bharat','Gyan','Ankit','Vinay'}"ADS_TO_REPLACE_4 label="Name" headerKey="none" headerValue="None" name="studentname"/> <s:textfield label="Age" name="age"></s:textfield>ADS_TO_REPLACE_5 <s:submit></s:submit> </s:form> </td>ADS_TO_REPLACE_6 <td align="center" valign="top"> |Accessing Bean Properties | <table cellpadding="2" cellspacing="0" width="200">ADS_TO_REPLACE_7 <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>ADS_TO_REPLACE_8 </td> <td align="center" valign="top"> |Accessing Request |ADS_TO_REPLACE_9 <table cellpadding="2" cellspacing="0" width="200"> <tr bgcolor="#CC66FF"> <td>Student Name</td> <td>Age</td></tr><tr>ADS_TO_REPLACE_10 <td><s:property value="#request['bean'].studentname"/></td> <td><s:property value="#request['bean'].age"/> </td></tr></table></td>ADS_TO_REPLACE_11 </tr> </table> </body>ADS_TO_REPLACE_12 </html> |
3_ OGNLOnsession.java (Action class)
package roseindia.action; import javax.servlet.http.HttpServletRequest; import org.apache.struts2.interceptor.ServletRequestAware;ADS_TO_REPLACE_13 import roseindia.model.StudentDetails; import com.opensymphony.xwork2.ActionSupport; public class OGNLAction extends ActionSupport ADS_TO_REPLACE_14 implements ServletRequestAware { private HttpServletRequest request; private String studentname;ADS_TO_REPLACE_15 private int age; public String getStudentname() { return studentname;ADS_TO_REPLACE_16 } public void setStudentname(String studentname) { this.studentname = studentname;ADS_TO_REPLACE_17 } public int getAge() { return age;ADS_TO_REPLACE_18 } public void setAge(int age) { this.age = age;ADS_TO_REPLACE_19 } public void setServletRequest(HttpServletRequest request) { this.request = request;ADS_TO_REPLACE_20 } public HttpServletRequest getServletRequest() { return request;ADS_TO_REPLACE_21 } public String execute() throws Exception { if (studentname.equals("none")) {ADS_TO_REPLACE_22 this.addActionError("Please Select name..."); return ERROR; }ADS_TO_REPLACE_23 StudentDetails bean = new StudentDetails(); bean.setStudentname(studentname); bean.setAge(age);ADS_TO_REPLACE_24 request.setAttribute("bean", bean); return SUCCESS; } } |
4_ EmployeeBean.java (Bean class)
package roseindia.model;ADS_TO_REPLACE_25 public class StudentDetails { private String studentname; private int age;ADS_TO_REPLACE_26 public String getStudentname() { return studentname; }ADS_TO_REPLACE_27 public void setStudentname(String studentname) { this.studentname = studentname; }ADS_TO_REPLACE_28 public int getAge() { return age; }ADS_TO_REPLACE_29 public void setAge(int age) { this.age = age; } ADS_TO_REPLACE_30 } |
5_ struts.xml
<struts> <constant name="struts.devMode" value="false" /> <package name="roseindia" extends="struts-default">ADS_TO_REPLACE_31 <action name="ognlAction" class="roseindia.action.OGNLAction"> <result name="error">index.jsp</result> <result name="success">index.jsp</result>ADS_TO_REPLACE_32 <result name="input">index.jsp</result> </action> </package>ADS_TO_REPLACE_33 </struts> |
Output
ADS_TO_REPLACE_34
Download Source CodeADS_TO_REPLACE_35
Advertisements
Ads
Ads