Java Reference
In-Depth Information
Stripes Never Ceases to Amaze Me
So, I was building the User List page for this example and
wanted to generate checkboxes next to each user to show
which roles each user has. As I was iterating over the list of roles
provided by the action bean, I was trying to figure out how to
make the checkbox checked if the user's list of roles contains
the current role in the iteration.
I was hoping for a
contains
operator or something similar in the
JSP EL so that I could write
${user.roles contains role}
, but no such
luck. Then, I wrote a small JSP custom tag library with a function
so that I could use
${myfn:userHasRole(user,role)}
. That involved
writing a class with a static method, declaring the tag library
in a TLD file, and importing the tag library with a
taglib
directive
in
taglibs.jsp
. As I was doing this, I was dreading having to explain
it all. . .
. . . until I had a closer look at the documentation for the Stripes
<s:checkbox> tag. It turns out that Stripes is smart enough to
recognize this often-needed functionality of having a check-
box be checked if the current value is contained in a collection.
All I had to do was put the user's roles in the
checked=
attribute
and the current role in the
value=
attribute, like this:
<s:checkbox name="..." value="${role}" checked="${user.roles}"/>
The checkbox is checked if
${user.roles}
contains
${role}
. Awe-
some! No wonder I like Stripes so much. Happy with that simple
and elegant solution, I went off to get rid of all that unneeded
tag library code with a big smile on my face.
Besides that it is simple and flexible, what makes the Stripes-Security
plug-in attractive is that it supports using roles with standard Java
annotations. We'll get to that in a minute—let's start by setting up the
plug-in. First, add
org.stripesstuff.plugin.security
to the extension packages:
<init-param>
<param-name>
Extension.Packages
</param-name>
<param-value>
stripesbook.ext,
org.stripesstuff.stripersist,
org.stripesstuff.plugin.security
</param-value>
</init-param>
Search WWH ::
Custom Search