You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@struts.apache.org by Janne Mattila <ja...@hotmail.com> on 2004/08/27 09:09:09 UTC
Tiles - proper way for choosing a tile dynamically
I use Tiles to create pages that have their tiles selected dynamically based
on some information that is created in an action. One simple example could
be a menu tile, which is selected based on an attribute that is stored in
session. I created simple definitions like:
<definition name=".layout.basic-cd"
path="/pages/cd/layout/basicTemplate.jsp">
<put name="topleft" value="/pages/cd/content/common/topleft.jsp" />
<put name="header" value="/pages/cd/content/common/logged-in-header.jsp"
/>
<put name="menu" value="/pages/cd/content/common/menu.jsp" />
<put name="bottomleft" value="/pages/cd/content/common/bottomleft.jsp" />
<put name="footer" value="/pages/cd/content/common/footer.jsp" />
</definition>
<definition name=".page.main" extends=".layout.basic-cd">
<put name="content" value="/pages/cd/content/main.jsp"/>
</definition>
...
then, in menu.jsp page I include either "admin" menu or "user" menu based on
the session parameter:
<unstandard:bind var="AdminRole" type="cd.business.Role" field="ADMIN"/>
<unstandard:bind var="UserRole" type="cd.business.Role" field="USER"/>
<c:choose>
<c:when test="${sessionScope.role == AdminRole}">
<tiles:insert page="menu-admin.jsp" />
</c:when>
<c:when test="${sessionScope.role == UserRole}">
<tiles:insert page="menu-user.jsp" />
</c:when>
</c:choose>
Now the question: is this the "proper" way to do this, or is there a more
elegant method?
PS. if those <unstandard:bind> tags confuse, they use jakarta unstandard
taglib to enable JSTL to compare the object in session to typesafe
enumeration values. In scriptlets this would be written simply as
<% if (cd.business.Role.ADMIN.equals(session.getAttribute("role")) { %>
<tiles:insert page="menu-admin.jsp" />
...
_________________________________________________________________
The new MSN 8: smart spam protection and 2 months FREE*
http://join.msn.com/?page=features/junkmail
---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
For additional commands, e-mail: user-help@struts.apache.org
Re: Tiles - proper way for choosing a tile dynamically
Posted by Jason Lea <ja...@kumachan.net.nz>.
If you are using container managed security or the security filter you could use request.isUserInRole("admin") checking in the action. But Tiles also supports request.isUserInRole checking in the definition and tags eg
<put name="menu" value="/pages/cd/content/common/menu-admin.jsp" role="admin" />
You could probably try using this in your definition
<put name="menu" value="/pages/cd/content/common/menu-user.jsp" role="user" />
<put name="menu" value="/pages/cd/content/common/menu-admin.jsp" role="admin" />
then on the page you would just use
<tiles:insert name="menu" />
and see if the second menu overrides the first as they have the same name (I assume it does).
It also works on the tiles:insert tag too, so you could use this on the page in question
<tiles:insert page="menu-admin.jsp" role="admin" />
<tiles:insert page="menu-user.jsp" role="user" />
David Friedman wrote:
>Janne,
>
>You could set everything dynamically in the tile using something like this
>(don't quote me if you like it, just look it up!):
>
>public class TestAction extends TilesAction {
> public ActionForward execute(ComponentContext context,
> ActionMapping mapping, ActionForm form, HttpServletRequest request,
> HttpServletResponse response) throws Exception {
>
> if (request.isUserInRole("admin")) {
> context.putAttribute("menu", new String("menu-admin.jsp"));
> } else {
> context.putAttribute("menu", new String("menu-user.jsp"));
> }
> return super.execute(mapping, form, request, response);
> }
>}
>
>Then, you don't need the unstandard:bind or EL tags, just the tiles:insert
>tags.
>
>For more information, I recommend you read the Tiles Advanced Features by
>Cedric Dumoulin. Link (from the bottom of the "Developer Guide" subsection
>"Tiles" page http://struts.apache.org/userGuide/dev_tiles.html) is:
>http://www.lifl.fr/~dumoulin/tiles/tilesAdvancedFeatures.pdf
>
>Regards,
>David
>
>-----Original Message-----
>From: Janne Mattila [mailto:janne4444_63@hotmail.com]
>Sent: Friday, August 27, 2004 3:09 AM
>To: user@struts.apache.org
>Subject: Tiles - proper way for choosing a tile dynamically
>
>
>I use Tiles to create pages that have their tiles selected dynamically based
>on some information that is created in an action. One simple example could
>be a menu tile, which is selected based on an attribute that is stored in
>session. I created simple definitions like:
>
> <definition name=".layout.basic-cd"
>path="/pages/cd/layout/basicTemplate.jsp">
> <put name="topleft" value="/pages/cd/content/common/topleft.jsp" />
> <put name="header" value="/pages/cd/content/common/logged-in-header.jsp"
>/>
> <put name="menu" value="/pages/cd/content/common/menu.jsp" />
> <put name="bottomleft" value="/pages/cd/content/common/bottomleft.jsp" />
> <put name="footer" value="/pages/cd/content/common/footer.jsp" />
> </definition>
>
> <definition name=".page.main" extends=".layout.basic-cd">
> <put name="content" value="/pages/cd/content/main.jsp"/>
> </definition>
>
> ...
>
>then, in menu.jsp page I include either "admin" menu or "user" menu based on
>the session parameter:
>
><unstandard:bind var="AdminRole" type="cd.business.Role" field="ADMIN"/>
><unstandard:bind var="UserRole" type="cd.business.Role" field="USER"/>
><c:choose>
><c:when test="${sessionScope.role == AdminRole}">
> <tiles:insert page="menu-admin.jsp" />
></c:when>
><c:when test="${sessionScope.role == UserRole}">
> <tiles:insert page="menu-user.jsp" />
></c:when>
></c:choose>
>
>Now the question: is this the "proper" way to do this, or is there a more
>elegant method?
>
>
>
>PS. if those <unstandard:bind> tags confuse, they use jakarta unstandard
>taglib to enable JSTL to compare the object in session to typesafe
>enumeration values. In scriptlets this would be written simply as
>
><% if (cd.business.Role.ADMIN.equals(session.getAttribute("role")) { %>
> <tiles:insert page="menu-admin.jsp" />
>...
>
>_________________________________________________________________
>The new MSN 8: smart spam protection and 2 months FREE*
>http://join.msn.com/?page=features/junkmail
>
>
>---------------------------------------------------------------------
>To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
>For additional commands, e-mail: user-help@struts.apache.org
>
>
>---------------------------------------------------------------------
>To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
>For additional commands, e-mail: user-help@struts.apache.org
>
>
>
>
--
Jason Lea
---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
For additional commands, e-mail: user-help@struts.apache.org
Re: Tiles - proper way for choosing a tile dynamically
Posted by Michael McGrady <mi...@michaelmcgrady.com>.
+1
David Friedman wrote:
>Janne,
>
>You could set everything dynamically in the tile using something like this
>(don't quote me if you like it, just look it up!):
>
>public class TestAction extends TilesAction {
> public ActionForward execute(ComponentContext context,
> ActionMapping mapping, ActionForm form, HttpServletRequest request,
> HttpServletResponse response) throws Exception {
>
> if (request.isUserInRole("admin")) {
> context.putAttribute("menu", new String("menu-admin.jsp"));
> } else {
> context.putAttribute("menu", new String("menu-user.jsp"));
> }
> return super.execute(mapping, form, request, response);
> }
>}
>
>Then, you don't need the unstandard:bind or EL tags, just the tiles:insert
>tags.
>
>For more information, I recommend you read the Tiles Advanced Features by
>Cedric Dumoulin. Link (from the bottom of the "Developer Guide" subsection
>"Tiles" page http://struts.apache.org/userGuide/dev_tiles.html) is:
>http://www.lifl.fr/~dumoulin/tiles/tilesAdvancedFeatures.pdf
>
>Regards,
>David
>
>-----Original Message-----
>From: Janne Mattila [mailto:janne4444_63@hotmail.com]
>Sent: Friday, August 27, 2004 3:09 AM
>To: user@struts.apache.org
>Subject: Tiles - proper way for choosing a tile dynamically
>
>
>I use Tiles to create pages that have their tiles selected dynamically based
>on some information that is created in an action. One simple example could
>be a menu tile, which is selected based on an attribute that is stored in
>session. I created simple definitions like:
>
> <definition name=".layout.basic-cd"
>path="/pages/cd/layout/basicTemplate.jsp">
> <put name="topleft" value="/pages/cd/content/common/topleft.jsp" />
> <put name="header" value="/pages/cd/content/common/logged-in-header.jsp"
>/>
> <put name="menu" value="/pages/cd/content/common/menu.jsp" />
> <put name="bottomleft" value="/pages/cd/content/common/bottomleft.jsp" />
> <put name="footer" value="/pages/cd/content/common/footer.jsp" />
> </definition>
>
> <definition name=".page.main" extends=".layout.basic-cd">
> <put name="content" value="/pages/cd/content/main.jsp"/>
> </definition>
>
> ...
>
>then, in menu.jsp page I include either "admin" menu or "user" menu based on
>the session parameter:
>
><unstandard:bind var="AdminRole" type="cd.business.Role" field="ADMIN"/>
><unstandard:bind var="UserRole" type="cd.business.Role" field="USER"/>
><c:choose>
><c:when test="${sessionScope.role == AdminRole}">
> <tiles:insert page="menu-admin.jsp" />
></c:when>
><c:when test="${sessionScope.role == UserRole}">
> <tiles:insert page="menu-user.jsp" />
></c:when>
></c:choose>
>
>Now the question: is this the "proper" way to do this, or is there a more
>elegant method?
>
>
>
>PS. if those <unstandard:bind> tags confuse, they use jakarta unstandard
>taglib to enable JSTL to compare the object in session to typesafe
>enumeration values. In scriptlets this would be written simply as
>
><% if (cd.business.Role.ADMIN.equals(session.getAttribute("role")) { %>
> <tiles:insert page="menu-admin.jsp" />
>...
>
>_________________________________________________________________
>The new MSN 8: smart spam protection and 2 months FREE*
>http://join.msn.com/?page=features/junkmail
>
>
>---------------------------------------------------------------------
>To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
>For additional commands, e-mail: user-help@struts.apache.org
>
>
>---------------------------------------------------------------------
>To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
>For additional commands, e-mail: user-help@struts.apache.org
>
>
>
>
>
>
---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
For additional commands, e-mail: user-help@struts.apache.org
RE: Tiles - proper way for choosing a tile dynamically
Posted by David Friedman <hu...@ix.netcom.com>.
Janne,
You could set everything dynamically in the tile using something like this
(don't quote me if you like it, just look it up!):
public class TestAction extends TilesAction {
public ActionForward execute(ComponentContext context,
ActionMapping mapping, ActionForm form, HttpServletRequest request,
HttpServletResponse response) throws Exception {
if (request.isUserInRole("admin")) {
context.putAttribute("menu", new String("menu-admin.jsp"));
} else {
context.putAttribute("menu", new String("menu-user.jsp"));
}
return super.execute(mapping, form, request, response);
}
}
Then, you don't need the unstandard:bind or EL tags, just the tiles:insert
tags.
For more information, I recommend you read the Tiles Advanced Features by
Cedric Dumoulin. Link (from the bottom of the "Developer Guide" subsection
"Tiles" page http://struts.apache.org/userGuide/dev_tiles.html) is:
http://www.lifl.fr/~dumoulin/tiles/tilesAdvancedFeatures.pdf
Regards,
David
-----Original Message-----
From: Janne Mattila [mailto:janne4444_63@hotmail.com]
Sent: Friday, August 27, 2004 3:09 AM
To: user@struts.apache.org
Subject: Tiles - proper way for choosing a tile dynamically
I use Tiles to create pages that have their tiles selected dynamically based
on some information that is created in an action. One simple example could
be a menu tile, which is selected based on an attribute that is stored in
session. I created simple definitions like:
<definition name=".layout.basic-cd"
path="/pages/cd/layout/basicTemplate.jsp">
<put name="topleft" value="/pages/cd/content/common/topleft.jsp" />
<put name="header" value="/pages/cd/content/common/logged-in-header.jsp"
/>
<put name="menu" value="/pages/cd/content/common/menu.jsp" />
<put name="bottomleft" value="/pages/cd/content/common/bottomleft.jsp" />
<put name="footer" value="/pages/cd/content/common/footer.jsp" />
</definition>
<definition name=".page.main" extends=".layout.basic-cd">
<put name="content" value="/pages/cd/content/main.jsp"/>
</definition>
...
then, in menu.jsp page I include either "admin" menu or "user" menu based on
the session parameter:
<unstandard:bind var="AdminRole" type="cd.business.Role" field="ADMIN"/>
<unstandard:bind var="UserRole" type="cd.business.Role" field="USER"/>
<c:choose>
<c:when test="${sessionScope.role == AdminRole}">
<tiles:insert page="menu-admin.jsp" />
</c:when>
<c:when test="${sessionScope.role == UserRole}">
<tiles:insert page="menu-user.jsp" />
</c:when>
</c:choose>
Now the question: is this the "proper" way to do this, or is there a more
elegant method?
PS. if those <unstandard:bind> tags confuse, they use jakarta unstandard
taglib to enable JSTL to compare the object in session to typesafe
enumeration values. In scriptlets this would be written simply as
<% if (cd.business.Role.ADMIN.equals(session.getAttribute("role")) { %>
<tiles:insert page="menu-admin.jsp" />
...
_________________________________________________________________
The new MSN 8: smart spam protection and 2 months FREE*
http://join.msn.com/?page=features/junkmail
---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
For additional commands, e-mail: user-help@struts.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
For additional commands, e-mail: user-help@struts.apache.org