Home Struts Struts Struts2.2.1 Tags How to access session value using OGNL.

Ask Questions?

View Latest Questions


 
 

How to access session value using OGNL.
Posted on: January 24, 2011 at 12:00 AM
Advertisement
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>

<head>

<title>Access_Value_bean_from_session_OGNL</title>

</head>

<body>

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

</body>

</html>

2_ sessionValue.jsp 

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

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

<html>

<head><title>Access_Value_of_bean_from_session_OGNL</title>

</head>

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

<table bgcolor="#CC99FF">

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

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

<s:form action="setbeanInsession">

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

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

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

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

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

</body>

</html>

3_ OGNLOnsession.java (Action class)

package roseindia.action;

import com.opensymphony.xwork2.ActionContext;

import com.opensymphony.xwork2.ActionSupport;

import java.util.ArrayList;

import java.util.List;

import java.util.Map;

import roseindia.model.EmployeeBean;

public class OGNLOnsessionObject extends ActionSupport {

private static final long serialVersionUID = 1L;

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

}

4_ EmployeeBean.java (Bean class)

package roseindia.model;

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;

}

}

5_ struts.xml

<struts>

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

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

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

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

</action>

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

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

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

</action>

</package>

</struts>

6_ DisplayBeanPropertyValue.jsp

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

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

<html>

<head>

<title>Access_Value_of_bean_from_session_OGNL</title>

</head>

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

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

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

<tr bgcolor="#CC99FF">

<td >Employee</td>

<td>Language</td>

</tr>

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

<tr>

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

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

</tr>

</s:iterator>

</table>

</body>

</html>

Output

Download Source Code

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


DMCA.com