You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@struts.apache.org by pa...@firstenergycorp.com on 2006/04/18 19:01:15 UTC
Can't figure out how to do "if then else" with Struts logic tags
Hello,
Is it possible to do "if then else" type of processing using Struts Logic
tags? I don't see anything in the Struts Logic tags documentation at this
link http://struts.apache.org/struts-doc-1.2.7/userGuide/struts-logic.html
Here's my situation:
1) I want to put in some logic in all my jsps to check if a user has
authenticated.
2) If user has not then I want the page to display a simple message saying
that login is required and include a link back to login page.
3) If user HAS authenticated I want the page to display the normal "stuff"
(e.g. content, struts form, etc) that the page is intended for.
4) Upon valid login, a user object is placed in session and it will have a
boolean property called "authenticated"
I know I could do something like this
...
<logic:equal name="user" property="authorized" value="false">
<h3>You must be logged in to view this page. Please login <a href=
"login.jsp">here</a></h3>
</logic:equal>
<logic:equal name="user" property="authorized" value="true">
Thank you for loggin in... Normal "stuff" for this page
</logic:equal>
One of the problems is that I also need to check if the user session object
is null because a user could have tried to go directly to an "internal"
(after login page) page using a bookmark. How can I check for the presence
of an object in the session?
Any help would be greatly appreciated.
Thanks.
David.
-----------------------------------------
The information contained in this message is intended only for the
personal and confidential use of the recipient(s) named above. If
the reader of this message is not the intended recipient or an
agent responsible for delivering it to the intended recipient, you
are hereby notified that you have received this document in error
and that any review, dissemination, distribution, or copying of
this message is strictly prohibited. If you have received this
communication in error, please notify us immediately, and delete
the original message.
---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
For additional commands, e-mail: user-help@struts.apache.org
Re: Can't figure out how to do "if then else" with Struts logic tags
Posted by pa...@firstenergycorp.com.
OK, great. I just wanted to make sure I understood correctly.
Thanks a lot!
David
Dave Newton
<newton@pingsite.
com> To
Struts Users Mailing List
04/18/2006 04:07 <us...@struts.apache.org>
PM cc
Subject
Please respond to Re: Can't figure out how to do "if
"Struts Users then else" with Struts logic tags
Mailing List"
<user@struts.apac
he.org>
pantichd@firstenergycorp.com wrote:
> I create a "login.do" action that ends up bringing up the login.jsp. The
> action called from the login.jsp page will then forward to ... let's say
> main.jsp. Do I need to "wrap" that jsp in a "main.do" action?
>
That what I was saying when I mentioned that in general you'll never see
a "naked" JSP in a Struts app.
If the logic is in a filter it doesn't matter--filters can process of an
entire application's URL-space.
Dave
---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
For additional commands, e-mail: user-help@struts.apache.org
-----------------------------------------
The information contained in this message is intended only for the
personal and confidential use of the recipient(s) named above. If
the reader of this message is not the intended recipient or an
agent responsible for delivering it to the intended recipient, you
are hereby notified that you have received this document in error
and that any review, dissemination, distribution, or copying of
this message is strictly prohibited. If you have received this
communication in error, please notify us immediately, and delete
the original message.
---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
For additional commands, e-mail: user-help@struts.apache.org
Re: Can't figure out how to do "if then else" with Struts logic tags
Posted by Dave Newton <ne...@pingsite.com>.
pantichd@firstenergycorp.com wrote:
> I create a "login.do" action that ends up bringing up the login.jsp. The
> action called from the login.jsp page will then forward to ... let's say
> main.jsp. Do I need to "wrap" that jsp in a "main.do" action?
>
That what I was saying when I mentioned that in general you'll never see
a "naked" JSP in a Struts app.
If the logic is in a filter it doesn't matter--filters can process of an
entire application's URL-space.
Dave
---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
For additional commands, e-mail: user-help@struts.apache.org
Re: Can't figure out how to do "if then else" with Struts logic tags
Posted by pa...@firstenergycorp.com.
Matt,
Thanks for your reply. I've seen the suggestion about putting jsp files
under the WEB-INF directory before but have never had a need to do it.
One thing I"m not clear on... if I place the jsps under WEB-INF directory,
what happens if someone tries to access one of them directly using a
bookmark? Do they just get a "page not found" error?
Maybe I just need to try it but here's the scenario I'm envisioning.
I create a "login.do" action that ends up bringing up the login.jsp. The
action called from the login.jsp page will then forward to ... let's say
main.jsp. Do I need to "wrap" that jsp in a "main.do" action?
If I don't what's to stop the user from bookmarking main.jsp and then
getting an ugly (or custom) "page not found" message if the jsp is not
accessible directly?
Like I said, maybe I just need to try it.
Thanks again for all the replies. I don't know what I'd do without all the
help I get from this mailing list.
"Matthew J.
Vincent"
<mjv@informatics. To
jax.org> Struts Users Mailing List
<us...@struts.apache.org>
04/18/2006 02:00 cc
PM
Subject
Re: Can't figure out how to do "if
Please respond to then else" with Struts logic tags
"Struts Users
Mailing List"
<user@struts.apac
he.org>
Not sure if this is a solution for you or not, but you could also put
your JSPs under WEB-INF
directory so that they cannot be directly accessed by a user. They
would instead have to go
through an Action first.
Filters may be your best bet (and yes, that is the URL to look at).
As far as JST syntax, you could use something like:
<c:choose>
<c:when test="${numberOfResults==0}">
<!-- DO SOMETHING -->
</c:when>
<c:otherwise>
<!-- DO SOMETHING ELSE -->
</c:otherwise>
</c:choose>
Hope that helps!
Matt
-----------------------------------------
The information contained in this message is intended only for the
personal and confidential use of the recipient(s) named above. If
the reader of this message is not the intended recipient or an
agent responsible for delivering it to the intended recipient, you
are hereby notified that you have received this document in error
and that any review, dissemination, distribution, or copying of
this message is strictly prohibited. If you have received this
communication in error, please notify us immediately, and delete
the original message.
---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
For additional commands, e-mail: user-help@struts.apache.org
Re: Can't figure out how to do "if then else" with Struts logic tags
Posted by "Matthew J. Vincent" <mj...@informatics.jax.org>.
Not sure if this is a solution for you or not, but you could also put
your JSPs under WEB-INF
directory so that they cannot be directly accessed by a user. They
would instead have to go
through an Action first.
Filters may be your best bet (and yes, that is the URL to look at).
As far as JST syntax, you could use something like:
<c:choose>
<c:when test="${numberOfResults==0}">
<!-- DO SOMETHING -->
</c:when>
<c:otherwise>
<!-- DO SOMETHING ELSE -->
</c:otherwise>
</c:choose>
Hope that helps!
Matt
pantichd@firstenergycorp.com wrote:
> Dave,
>
> I totally agree with you about doing this kind of checking in an action
> class and I AM doing it there. However, the problem is that I still need a
> way to prevent a user from going to the jsp w/o first logging in.
>
> Let's say the user logged in during a previous session and bookmarked the
> jsp. They can get back to it w/o having to login. I know that once they
> submit the form the action class will catch that they're not logged in and
> send them back to login page. But I'd rather catch it before then. I hate
> to have the user fill in a bunch of information, submit form and THEN get
> redirected to login.
>
> I'll look into JSTL. Are you saying it has "if-then-else" type of
> processing? Could you point me in the right direction for that and for the
> "check if object exists in session" issue?
>
> Also, I haven't used filters before. We just upgraded from Struts 1.0.2 so
> I'm pretty ignorant of all the features of newest version of Struts. Is
> this some kind of Struts filter or what is described here?
> http://java.sun.com/products/servlet/Filters.html
>
> Thanks again for your reply.
>
> pantichd@firstenergycorp.com wrote:
>
>> Is it possible to do "if then else" type of processing using Struts Logic
>> tags? I don't see anything in the Struts Logic tags documentation at this
>> link http://struts.apache.org/struts-doc-1.2.7
>>
> /userGuide/struts-logic.html
>
>
> Use JSTL; you'll be better off in the long run anyway, and there's
> essentially zero learning curve.
>
>
>
>
>> 1) I want to put in some logic in all my jsps to check if a user has
>> authenticated.
>>
>>
>
> Don't do this in the JSP; do this in either a filter, base Action class,
> etc. Try to use JSP pages for presentation only to keep concerns separated.
>
>
>> One of the problems is that I also need to check if the user session
>>
> object
>
>> is null because a user could have tried to go directly to an "internal"
>> (after login page) page using a bookmark. How can I check for the
>>
> presence
>
>> of an object in the session?
>>
>>
>
> Again, if you're doing this outside of a JSP (highly recommended) it's
> trivial. Otherwise you could always use a JSTL tag to check for a value
> in the appropriate session scope attribute key.
>
> Als0-Dave
>
>
>
>
> David
>
>
> -----------------------------------------
> The information contained in this message is intended only for the
> personal and confidential use of the recipient(s) named above. If
> the reader of this message is not the intended recipient or an
> agent responsible for delivering it to the intended recipient, you
> are hereby notified that you have received this document in error
> and that any review, dissemination, distribution, or copying of
> this message is strictly prohibited. If you have received this
> communication in error, please notify us immediately, and delete
> the original message.
>
>
> ---------------------------------------------------------------------
> 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: Can't figure out how to do "if then else" with Struts logic tags
Posted by Dave Newton <ne...@pingsite.com>.
pantichd@firstenergycorp.com wrote:
> I totally agree with you about doing this kind of checking in an action
> class and I AM doing it there. However, the problem is that I still need a
> way to prevent a user from going to the jsp w/o first logging in.
>
Matthew already offered this but I'll reiterate; in general it's
generally accepted practice (nowadays, anyway) to put JSP pages under
WEB-INF (although I guess this doesn't work under JSF, as was pointed
out to me sometime back?) and access pages _only_ through Struts Actions.
> I'll look into JSTL. Are you saying it has "if-then-else" type of
> processing? Could you point me in the right direction for that and for the
> "check if object exists in session" issue?
>
<c:choose>, <c:when...> and <c:otherwise> tags.
<c:if test="${empty sessionUser}"> where "sessionUser" is replaced with
the session user attribute key.
> Also, I haven't used filters before. We just upgraded from Struts 1.0.2 so
> I'm pretty ignorant of all the features of newest version of Struts. Is
> this some kind of Struts filter or what is described here?
> http://java.sun.com/products/servlet/Filters.html
>
Yes; generic Servlet 2.3 filters or use the DependencyFilter from Frank
Zammetti's Java Web Parts or...
Dave
---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
For additional commands, e-mail: user-help@struts.apache.org
Re: Can't figure out how to do "if then else" with Struts logic tags
Posted by pa...@firstenergycorp.com.
Dave,
I totally agree with you about doing this kind of checking in an action
class and I AM doing it there. However, the problem is that I still need a
way to prevent a user from going to the jsp w/o first logging in.
Let's say the user logged in during a previous session and bookmarked the
jsp. They can get back to it w/o having to login. I know that once they
submit the form the action class will catch that they're not logged in and
send them back to login page. But I'd rather catch it before then. I hate
to have the user fill in a bunch of information, submit form and THEN get
redirected to login.
I'll look into JSTL. Are you saying it has "if-then-else" type of
processing? Could you point me in the right direction for that and for the
"check if object exists in session" issue?
Also, I haven't used filters before. We just upgraded from Struts 1.0.2 so
I'm pretty ignorant of all the features of newest version of Struts. Is
this some kind of Struts filter or what is described here?
http://java.sun.com/products/servlet/Filters.html
Thanks again for your reply.
pantichd@firstenergycorp.com wrote:
> Is it possible to do "if then else" type of processing using Struts Logic
> tags? I don't see anything in the Struts Logic tags documentation at this
> link http://struts.apache.org/struts-doc-1.2.7
/userGuide/struts-logic.html
>
Use JSTL; you'll be better off in the long run anyway, and there's
essentially zero learning curve.
> 1) I want to put in some logic in all my jsps to check if a user has
> authenticated.
>
Don't do this in the JSP; do this in either a filter, base Action class,
etc. Try to use JSP pages for presentation only to keep concerns separated.
> One of the problems is that I also need to check if the user session
object
> is null because a user could have tried to go directly to an "internal"
> (after login page) page using a bookmark. How can I check for the
presence
> of an object in the session?
>
Again, if you're doing this outside of a JSP (highly recommended) it's
trivial. Otherwise you could always use a JSTL tag to check for a value
in the appropriate session scope attribute key.
Als0-Dave
David
-----------------------------------------
The information contained in this message is intended only for the
personal and confidential use of the recipient(s) named above. If
the reader of this message is not the intended recipient or an
agent responsible for delivering it to the intended recipient, you
are hereby notified that you have received this document in error
and that any review, dissemination, distribution, or copying of
this message is strictly prohibited. If you have received this
communication in error, please notify us immediately, and delete
the original message.
---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
For additional commands, e-mail: user-help@struts.apache.org
Re: Can't figure out how to do "if then else" with Struts logic tags
Posted by Dave Newton <ne...@pingsite.com>.
pantichd@firstenergycorp.com wrote:
> Is it possible to do "if then else" type of processing using Struts Logic
> tags? I don't see anything in the Struts Logic tags documentation at this
> link http://struts.apache.org/struts-doc-1.2.7/userGuide/struts-logic.html
>
Use JSTL; you'll be better off in the long run anyway, and there's
essentially zero learning curve.
> 1) I want to put in some logic in all my jsps to check if a user has
> authenticated.
>
Don't do this in the JSP; do this in either a filter, base Action class,
etc. Try to use JSP pages for presentation only to keep concerns separated.
> One of the problems is that I also need to check if the user session object
> is null because a user could have tried to go directly to an "internal"
> (after login page) page using a bookmark. How can I check for the presence
> of an object in the session?
>
Again, if you're doing this outside of a JSP (highly recommended) it's
trivial. Otherwise you could always use a JSTL tag to check for a value
in the appropriate session scope attribute key.
Als0-Dave
---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
For additional commands, e-mail: user-help@struts.apache.org