struts-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From phillips1021 <bphill...@ku.edu>
Subject Using Expression Language (EL) To Access ActionSupport Class Properties
Date Mon, 04 May 2009 18:00:36 GMT

We are building a new web application that uses both JSecurity
(http://cwiki.apache.org/confluence/display/KI/Index, now known as Ki) and
Struts 2 (version 2.1.6).  

In many of the JSPs for this application, we need to provide a dynamic
String value to the JSecurity tag library's hasAnyRoles tag.  This tag's
format is:

<jsec:hasAnyRoles name="Staff,Admin">
...content to include if current user has one of the roles specified in the
name attribute...
</jsec:hasAnyRoles>

Of course instead of hard-coding the values for the name attribute we want
to have the the attribute's value rendered dynamically using Struts 2.  

First we tried using:

<jsec:hasAnyRoles name="<s:property value='roles' />" ></jsec:hasAnyRoles>

But that did not work even though we have a public method named getRoles in
the action support class that renders this jsp page.

So just for a test we tried using expression language (EL):

<jsec:hasAnyRoles name="${roles}" ></jsec:hasAnyRoles>

And the above worked just fine.  

I didn't know you could access properties of the ActionSupport class using
Expression Language. 

I researched the ability to use Expression Language (EL) to access
properties of the ActionSupport class in the Struts 2 documentation at
http://struts.apache.org and in this mailing list.  I didn't find much about
the capability to use EL to access properties of the ActionSupport class. 
The documentation clearly states NOT to use EL in attributes for Struts 2
tags, but it's not clear on using EL to access ActionSupport class
properties outside of the Struts 2 tags.

Since we are building a new web application that will be around for some
time and would like to be able to upgrade to future versions of Struts 2,
we'd like to know if the ability to access ActionSupport class properties
using EL is a built-in and a supported feature of Struts 2?  


Below is some test code to detail how this works:

ActionSupport class:

import com.opensymphony.xwork2.ActionSupport;

public class TestActionSupport extends ActionSupport {

	private static final long serialVersionUID = 1L;

	public String execute() throws Exception {

		return SUCCESS;

	}
	
	public String getRoles() {
		
		return "Staff,Admin";

	}

}

JSP View rendered when above class returns SUCCESS:

<%@ page language="java" import="java.util.*" pageEncoding="ISO-8859-1"%>
<%@ taglib prefix="s" uri="/struts-tags" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
   
    <title>Test of Struts 2 and EL</title>

  </head>
  
  <body>
    <h3>Test of Struts 2 and EL Results Page</h3>
    <p>Roles via expression language: ${roles}</p>
    <p>Roles via Struts 2 property tag: <s:property value='roles' /></p>
  </body>
</html>

Does Struts 2 turn the ActionSupport class itself turned into a bean object
that is placed in request scope?  Is that how the Expression Language
${roles} is able to execute correctly?

Thanks for any information provided that helps us understand the use of EL
to access the ActionSupport class properties.

Bruce

-- 
View this message in context: http://www.nabble.com/Using-Expression-Language-%28EL%29-To-Access-ActionSupport-Class-Properties-tp23373477p23373477.html
Sent from the Struts - User mailing list archive at Nabble.com.


---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
For additional commands, e-mail: user-help@struts.apache.org


Mime
View raw message