How to access session value using OGNL.
Posted on: January 24, 2011 at 12:00 AM
In this example, you will see how to access value of session using OGNL expression language in struts2.

How to access session value using OGNL.

In this example, you will see how to access value of session using OGNL expression language in struts2.

 1-index.jsp

<html>ADS_TO_REPLACE_1

<head>

<title>Access_Value_bean_from_session_OGNL</title>

</head>ADS_TO_REPLACE_2

<body>

<a href="sessionOGNL">Access_Value_bean_from_session_OGNL</a>

</body>ADS_TO_REPLACE_3

</html>

2_ sessionValue.jsp 

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

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

<html>ADS_TO_REPLACE_4

<head><title>Access_Value_of_bean_from_session_OGNL</title>

</head>

<body><h1>Access_Value_of_bean_from_session_OGNL</h1><hr/>ADS_TO_REPLACE_5

<table bgcolor="#CC99FF">

<tr><td>Please enter the Name and Profile....

<DIV style="color:pink;"><s:actionerror/></DIV>ADS_TO_REPLACE_6

<s:form action="setbeanInsession">

<s:select label="Language" headerKey="none" name="lang"

headerValue="Select Language" list="langName"></s:select>ADS_TO_REPLACE_7

<s:textfield name="empName" label="Employee Name"></s:textfield>

<s:submit></s:submit></s:form>

</td> </tr></table>ADS_TO_REPLACE_8

</body>

</html>

3_ OGNLOnsession.java (Action class)

package roseindia.action;

import com.opensymphony.xwork2.ActionContext;ADS_TO_REPLACE_9

import com.opensymphony.xwork2.ActionSupport;

import java.util.ArrayList;

import java.util.List;ADS_TO_REPLACE_10

import java.util.Map;

import roseindia.model.EmployeeBean;

public class OGNLOnsessionObject extends ActionSupport {ADS_TO_REPLACE_11

private static final long serialVersionUID = 1L;

private List langName;

private String lang;ADS_TO_REPLACE_12

private String empName;

ActionContext context = ActionContext.getContext();

Map session = context.getSession();ADS_TO_REPLACE_13

public String getLang() {

return lang;

}ADS_TO_REPLACE_14

public void setLang(String lang) {

this.lang = lang;

}ADS_TO_REPLACE_15

public String getEmpName() {

return empName;

}ADS_TO_REPLACE_16

public void setEmpName(String empName) {

this.empName = empName;

}ADS_TO_REPLACE_17

public List getLangName() {

return langName;

}ADS_TO_REPLACE_18

public void setLangName(List langName) {

this.langName = langName;

}ADS_TO_REPLACE_19

public OGNLOnsessionObject() {

langName = new ArrayList();

langName.add("C");ADS_TO_REPLACE_20

langName.add("c++");

langName.add("JAVA");

langName.add("PHP");ADS_TO_REPLACE_21

}

public String show() {

return INPUT;ADS_TO_REPLACE_22

}

public String execute() {

if (lang.equals("none")) {ADS_TO_REPLACE_23

this.addActionError("Please select any language.");

return ERROR;

}ADS_TO_REPLACE_24

ArrayList beans = null;

EmployeeBean bean = new EmployeeBean();

bean.setEmpName(empName);ADS_TO_REPLACE_25

bean.setLang(lang);

beans = (ArrayList) session.get("beans");

if (beans == null)ADS_TO_REPLACE_26

beans = new ArrayList();

beans.add(bean);

session.put("beans", beans);ADS_TO_REPLACE_27

return SUCCESS; }

}

4_ EmployeeBean.java (Bean class)

package roseindia.model;

public class EmployeeBean {ADS_TO_REPLACE_28

private String lang;

private String empName;

public String getLang() {ADS_TO_REPLACE_29

return lang;

}

public void setLang(String lang) {ADS_TO_REPLACE_30

this.lang = lang;

}

public String getEmpName() {ADS_TO_REPLACE_31

return empName;

}

public void setEmpName(String empName) {ADS_TO_REPLACE_32

this.empName = empName;

}

}

5_ struts.xml

<struts>ADS_TO_REPLACE_33

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

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

<action name="sessionOGNL" class=
"roseindia.action.OGNLOnsessionObject"
method="show">
ADS_TO_REPLACE_34

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

</action>

<action name="setbeanInsession" class=
"roseindia.action.OGNLOnsessionObject"
>
ADS_TO_REPLACE_35

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

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

</action>ADS_TO_REPLACE_36

</package>

</struts>

6_ DisplayBeanPropertyValue.jsp

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

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

<html>

<head>

<title>Access_Value_of_bean_from_session_OGNL</title>ADS_TO_REPLACE_38

</head>

<body><h1>Access_Value_of_bean_from_session_OGNL</h1><hr>

<b style="color: purple;">|Access session |</b>ADS_TO_REPLACE_39

<table cellpadding="10" cellspacing="0">

<tr bgcolor="#CC99FF">

<td >Employee</td>ADS_TO_REPLACE_40

<td>Language</td>

</tr>

<s:iterator id="beans" value="#session['beans']">ADS_TO_REPLACE_41

<tr>

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

<td><s:property value="lang"/></td>ADS_TO_REPLACE_42

</tr>

</s:iterator>

</table>ADS_TO_REPLACE_43

</body>

</html>

Output

ADS_TO_REPLACE_44

ADS_TO_REPLACE_45

ADS_TO_REPLACE_46

Download Source Code

Related Tags for How to access session value using OGNL.:

Advertisements

Ads

Ads

 
Advertisement null

Ads