Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

...

This page provides info relevant to the Roller struts2 migration effort which is starting as of Roller 4.0 and will end ??

...

Roller Struts2 Design

This section tries to breakdown the various ways in which Roller uses struts2 and should give you an idea of what tools and code are at your disposal for working with struts2 in Roller.

UIAction class

One of the goals of the struts2 migration is to make our actions simpler and easier to work on by having all the code and features which are common to all (or most) actions take place automatically and the UIAction class is how we do that. The UIAction class is a base class for actions to extend and provides support for all the things that we expect most of our actions will need to do, such as looking up the authenticated user, applying security checks on weblog permissions, setting action messages and errors, defining a page title, setting the tabbed menu preferences, etc, etc. Many of these things which were handled in custom or inconsistent ways in our struts1 forms are now handled in a simple and consolidated manner now and are provided to action developers for free.

Feel free to take a look at the UIAction class when you first start working with struts2 actions in Roller, but i'll talk about some of it's features in the coming sections. The main thing to know is that the UIAction class is meant to serve as a solid base for all Roller struts2 actions.

All struts2 actions should extend the UIAction class unless you have a very specific reason not too.

Action Freebies

One of the main features of the UIAction class is that it provides free access to some of the things that were handled in custom ways in the old struts1 actions. The best examples of this are how to access the UserData object representing the authenticated user, and the WebsiteData object representing the weblog being dealt with by the action. Pretty much all of Roller's actions require these 2 objects to work properly and so the UIAction has taken care of them for you by providing you getAuthenticatedUser() and getActionWeblog() methods.

...

Currently these are the only 2 objects which are extracted from the request and made available via UIAction, all other objects should be loaded by the action itself since they are more action specific. Hint: read about the myPrepare() method below.

Security Enforcement

One of the major things that the old struts1 code did poorly was action security enforcement. In specific, in all of the old struts1 code you would see a bunch of boilerplate code which basically did this ...

...

Now, obviously each action will have different security constraints and so we still need a way to specify those constraints, so you your job isn't completely taken caredone. All you need to do to ensure that the proper security rules are applied for your action is potentially override a couple of to override some security methods. The UIAction base class implements an interface called UISecurityEnforced which looks like this ...

...

These methods provide a very simple way for action writers to control the security enforcement on their action. By overriding any of these methods you can configure your action to not enforce any security rules at all or to enforce all very specific security options, it's up to you. To set a security preference just implement the method as you would expect, so . So to force enforce that users must have the ADMIN permission on the action weblog to use the your action you would do this ...

...

This means that by default, all actions require that a user and weblog is properly specified and that the user has the "editor" role. Now So now all you would really need to do for the example we gave about, all you would really have to do above is put this in your action to enforce the ADMIN permission ...

No Format
    public short requiredWeblogPermissions() { return PermissionsData.ADMIN; }

Pretty slick eh? Note, : the reason why there is no default required weblog permission is mostly to alleviate any confusion about required permissions for actions. Basically, we want the action classes to define the requiredWeblogPermissions() method as shown above so that anyone working on the action can see directly in that class what the enforced permissions are.

...

That's it. Now I know that my action has already looked up the list of themes that are used by all the action methods defined in my action and the code is in a nice and reusable place. So if you have any work that you want to do just before you your action method executes, then implement the myPrepare() method.

Tabbed Menu controls

One of the page elements that is required for pretty much all actions is the tabbed menu which displays near the top of the page and show the current location of the user. The old Roller code used to render that menu using a custom jsp taglib which was hard to understand and effectively used lots of custom request parsing code to figure out what page the request was on and what permissions to enforce, etc. All of that has been updated for the struts2 actions and design has changed to use a simple tiles jsp for rendering the tabbed menu and the logic for forming the menu has been pushed to the actions where it is most appropriate. Basically, with the new struts2 code it is an actions job to properly provide a menu to be rendered.

Now, as you would expect we have made the process for actions to provide a menu extremely simple. The UIAction class defines a few methods which deal with controlling and accessing the menu for that action via a getMenu() method. To define how to render a standard Roller tabbed menu you simply need to set 2 attributes on your action which are inherited from the UIAction class, the actionName and desiredMenu attributes. These 2 attributes tell the menuing code what action is currently selected and what menu should be rendered, the rest is handled for you by using a MenuHelper class to construct a Menu which is appropriate.

So, to specify these attributes you will simply want to define their values in the default constructor for your action like so ...

No Format

    public MyAction() {
        this.actionName = "myAction";
        this.desiredMenu = "editor"; // choose editor menu
    }

NOTE: the actionName value must specify the name of your action as defined in the struts.xml action mapping, not necessarily your action class name. the desiredMenu attribute is just the name of the menu, which currently only has options for "editor" and "admin".

Exception and Error Handling

...