You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@struts.apache.org by Russell Neufeld <ru...@hds.com> on 2009/04/15 22:00:34 UTC

Struts2 equivalent of

Hi all,

    We're porting an app from Struts 1 to Struts 2, and I'm having 
trouble figuring out how to conditionally include a portion of a JSP 
depending on the role of the user principal.  In Struts 1 we did this:

<logic:present role="SERVICE,SYSTEM">
    ... some html ...
</logic:present>

    What's the preferred way of doing this in Struts2?  Thanks,

       Russ

Re: Struts2 equivalent of

Posted by Russell Neufeld <ru...@hds.com>.
You're right - the <s:if> method may be a quicker change.  I'll give 
that a shot.  Thanks for the quick answer.

    Russ

Wes Wannemacher wrote:
> On Wednesday 15 April 2009 16:00:34 Russell Neufeld wrote:
>   
>> Hi all,
>>
>>     We're porting an app from Struts 1 to Struts 2, and I'm having
>> trouble figuring out how to conditionally include a portion of a JSP
>> depending on the role of the user principal.  In Struts 1 we did this:
>>
>> <logic:present role="SERVICE,SYSTEM">
>>     ... some html ...
>> </logic:present>
>>
>>     What's the preferred way of doing this in Struts2?  Thanks,
>>
>>        Russ
>>     
>
> There are a couple of ways to do it, personally, I would use Spring Security 
> and incorporate their tag library. It will eliminate quite a bit of work on 
> your part.
>
> Since you are probably pressed for time since this is an upgrade effort, the 
> better method would be to expose your user object in a way that you can get at 
> the list of roles (such as putting it in the session). Then, just use s:if and 
> some OGNL to check like this - 
>
> <s:if test="#session.user.roles.contains('REQUIRED_ROLE')">
> ...
> </s:if>
>
> -Wes
>
>   


RE: Struts2 equivalent of

Posted by Security Management <li...@secmgmt.com>.
I second the Spring Security stuff, it's great.



-----Original Message-----
From: Wes Wannemacher [mailto:wesw@wantii.com] 
Sent: Wednesday, April 15, 2009 4:09 PM
To: Struts Users Mailing List
Subject: Re: Struts2 equivalent of <logic:present role=""...>

On Wednesday 15 April 2009 16:00:34 Russell Neufeld wrote:
> Hi all,
>
>     We're porting an app from Struts 1 to Struts 2, and I'm having
> trouble figuring out how to conditionally include a portion of a JSP
> depending on the role of the user principal.  In Struts 1 we did this:
>
> <logic:present role="SERVICE,SYSTEM">
>     ... some html ...
> </logic:present>
>
>     What's the preferred way of doing this in Struts2?  Thanks,
>
>        Russ

There are a couple of ways to do it, personally, I would use Spring Security

and incorporate their tag library. It will eliminate quite a bit of work on 
your part.

Since you are probably pressed for time since this is an upgrade effort, the

better method would be to expose your user object in a way that you can get
at 
the list of roles (such as putting it in the session). Then, just use s:if
and 
some OGNL to check like this - 

<s:if test="#session.user.roles.contains('REQUIRED_ROLE')">
...
</s:if>

-Wes

-- 

Wes Wannemacher
Author - Struts 2 In Practice 
Includes coverage of Struts 2.1, Spring, JPA, JQuery, Sitemesh and more
http://www.manning.com/wannemacher


---------------------------------------------------------------------
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: Struts2 equivalent of

Posted by Dave Newton <ne...@yahoo.com>.
Martin Gainty wrote:
> yes the Struts 1.x if tag has been deprecated
> 
> you can check for specific attributes as wes suggested 
> OR
> change your struts-spring autowire type to "constructor" in your interceptor stack e.g.
> \WEB-INF\src\java\struts.xml
> 
> <interceptors>
>   <interceptor name="autowire" class="com.opensymphony.xwork.spring.interceptor.ActionAutowiringInterceptor">
>     <param name="autowireStrategy">2</param>
>   </interceptor>
>   <interceptor-stack name="autowireDefault">
>     <interceptor-ref name="autowire"/>
>     <interceptor-ref name="defaultStack"/>
>   </interceptor-stack>
> </interceptors>
> and configure the springframework ContextLoaderListener in web.xml
> 
> <listener>
>   <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
> </listener>
> 
> where the individual bean has been configured as
> 
> <bean name="some-action" class="fully.qualified.class.name" singleton="false">
> </bean>
> take a brief look at the characteristics of your Bean class from Bean.java
> 
>     public boolean start(Writer writer) {
>         boolean result = super.start(writer);
>         ValueStack stack = getStack();
>         try {
>             String beanName = findString(name, "name", "Bean name is required. Example: com.acme.FooBean");
>             bean = objectFactory.buildBean(ClassLoaderUtil.loadClass(beanName, getClass()), stack.getContext());
>         } catch (Exception e) {
>             LOG.error("Could not instantiate bean", e);
>             return false;
>         }
> 
>         // push bean on stack
>         stack.push(bean);
> ...
> }
> that last statement stack.push(bean) means the bean is now on the stack to test with!
> 
> from jsp
> <s:property value="#beanname" />
> 
> we now know the bean as a constructed Bean object
> (via the decorated name of the bean)
> feel free to use the stack.push to push the necessary value to be accessed
> later by <s:property ... /> in your jsp..

Seems like it'd be way easier just to expose it as an action property.

Dave


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


RE: Struts2 equivalent of

Posted by Martin Gainty <mg...@hotmail.com>.
yes the Struts 1.x if tag has been deprecated

you can check for specific attributes as wes suggested 
OR
change your struts-spring autowire type to "constructor" in your interceptor stack e.g.
\WEB-INF\src\java\struts.xml

<interceptors>
  <interceptor name="autowire" class="com.opensymphony.xwork.spring.interceptor.ActionAutowiringInterceptor">
    <param name="autowireStrategy">2</param>
  </interceptor>
  <interceptor-stack name="autowireDefault">
    <interceptor-ref name="autowire"/>
    <interceptor-ref name="defaultStack"/>
  </interceptor-stack>
</interceptors>
and configure the springframework ContextLoaderListener in web.xml

<listener>
  <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>

where the individual bean has been configured as

<bean name="some-action" class="fully.qualified.class.name" singleton="false">
</bean>
take a brief look at the characteristics of your Bean class from Bean.java

    public boolean start(Writer writer) {
        boolean result = super.start(writer);
        ValueStack stack = getStack();
        try {
            String beanName = findString(name, "name", "Bean name is required. Example: com.acme.FooBean");
            bean = objectFactory.buildBean(ClassLoaderUtil.loadClass(beanName, getClass()), stack.getContext());
        } catch (Exception e) {
            LOG.error("Could not instantiate bean", e);
            return false;
        }

        // push bean on stack
        stack.push(bean);
...
}
that last statement stack.push(bean) means the bean is now on the stack to test with!

from jsp
<s:property value="#beanname" />

we now know the bean as a constructed Bean object
(via the decorated name of the bean)
feel free to use the stack.push to push the necessary value to be accessed
later by <s:property ... /> in your jsp..
Martin 
______________________________________________ 
Disclaimer and Confidentiality/Verzicht und Vertraulichkeitanmerkung / Note de déni et de confidentialité 
This message is confidential. If you should not be the intended receiver, then we ask politely to report. Each unauthorized forwarding or manufacturing of a copy is inadmissible. This message serves only for the exchange of information and has no legal binding effect. Due to the easy manipulation of emails we cannot take responsibility over the the contents.
Diese Nachricht ist vertraulich. Sollten Sie nicht der vorgesehene Empfaenger sein, so bitten wir hoeflich um eine Mitteilung. Jede unbefugte Weiterleitung oder Fertigung einer Kopie ist unzulaessig. Diese Nachricht dient lediglich dem Austausch von Informationen und entfaltet keine rechtliche Bindungswirkung. Aufgrund der leichten Manipulierbarkeit von E-Mails koennen wir keine Haftung fuer den Inhalt uebernehmen.
Ce message est confidentiel et peut être privilégié. Si vous n'êtes pas le destinataire prévu, nous te demandons avec bonté que pour satisfaire informez l'expéditeur. N'importe quelle diffusion non autorisée ou la copie de ceci est interdite. Ce message sert à l'information seulement et n'aura pas n'importe quel effet légalement obligatoire. Étant donné que les email peuvent facilement être sujets à la manipulation, nous ne pouvons accepter aucune responsabilité pour le contenu fourni.






> From: wesw@wantii.com
> To: user@struts.apache.org
> Subject: Re: Struts2 equivalent of <logic:present role=""...>
> Date: Wed, 15 Apr 2009 16:08:59 -0400
> 
> On Wednesday 15 April 2009 16:00:34 Russell Neufeld wrote:
> > Hi all,
> >
> >     We're porting an app from Struts 1 to Struts 2, and I'm having
> > trouble figuring out how to conditionally include a portion of a JSP
> > depending on the role of the user principal.  In Struts 1 we did this:
> >
> > <logic:present role="SERVICE,SYSTEM">
> >     ... some html ...
> > </logic:present>
> >
> >     What's the preferred way of doing this in Struts2?  Thanks,
> >
> >        Russ
> 
> There are a couple of ways to do it, personally, I would use Spring Security 
> and incorporate their tag library. It will eliminate quite a bit of work on 
> your part.
> 
> Since you are probably pressed for time since this is an upgrade effort, the 
> better method would be to expose your user object in a way that you can get at 
> the list of roles (such as putting it in the session). Then, just use s:if and 
> some OGNL to check like this - 
> 
> <s:if test="#session.user.roles.contains('REQUIRED_ROLE')">
> ...
> </s:if>
> 
> -Wes
> 
> -- 
> 
> Wes Wannemacher
> Author - Struts 2 In Practice 
> Includes coverage of Struts 2.1, Spring, JPA, JQuery, Sitemesh and more
> http://www.manning.com/wannemacher
> 
> 
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
> For additional commands, e-mail: user-help@struts.apache.org
> 

_________________________________________________________________
Rediscover Hotmail®: Get e-mail storage that grows with you. 
http://windowslive.com/RediscoverHotmail?ocid=TXT_TAGLM_WL_HM_Rediscover_Storage1_042009

Re: Struts2 equivalent of

Posted by Wes Wannemacher <we...@wantii.com>.
On Wednesday 15 April 2009 16:00:34 Russell Neufeld wrote:
> Hi all,
>
>     We're porting an app from Struts 1 to Struts 2, and I'm having
> trouble figuring out how to conditionally include a portion of a JSP
> depending on the role of the user principal.  In Struts 1 we did this:
>
> <logic:present role="SERVICE,SYSTEM">
>     ... some html ...
> </logic:present>
>
>     What's the preferred way of doing this in Struts2?  Thanks,
>
>        Russ

There are a couple of ways to do it, personally, I would use Spring Security 
and incorporate their tag library. It will eliminate quite a bit of work on 
your part.

Since you are probably pressed for time since this is an upgrade effort, the 
better method would be to expose your user object in a way that you can get at 
the list of roles (such as putting it in the session). Then, just use s:if and 
some OGNL to check like this - 

<s:if test="#session.user.roles.contains('REQUIRED_ROLE')">
...
</s:if>

-Wes

-- 

Wes Wannemacher
Author - Struts 2 In Practice 
Includes coverage of Struts 2.1, Spring, JPA, JQuery, Sitemesh and more
http://www.manning.com/wannemacher


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