Struts2 Is there a way to add metadata to the action definition in the struts.xml file?

I am using Struts2 framework to build a webapp. I have an interceptor that needs to have different behavior depending on what action is called. For example, a login interceptor, which should always allow some actions to be performed, but it should block other actions if the user is not already logged in.

Now I solve this manually by checking the name (and / or namespace) of the action in the interceptor and defining my behavior based on that. The downside to this hardcoded logic is that it is difficult to maintain if I edit the struts.xml file, and it is also not obvious what is going on for other developers.

I would like to know if this is a way to add INSIDE metadata to the struts.xml file (or another file?) To "mark" certain actions as certain "types". For example, something like this:

Struts.xml

<action name="loginPage" types="login, user, viewpage" class="login.controller.LoginPage">
    <result name="success">/login/jsp/Login.jsp</result>
</action>

      

And then in my interceptor class:

@Override
public String intercept(ActionInvocation invocation)
throws Exception
{
    Set<String> actionTypes = invocation.getInvocationContext().getTypes();

    if(actionTypes.contains("login")
    {
        doSomething();
    }
    else
    {
        doSomethingElse();
    }        

}

      

Is this possible, or is it hardcoded parsing the name (space) the only way?

0


source to share


1 answer


Obviously you cannot do this, because the DTD won't let you.

Another good example of the XY problem :

Problem XY asks about your attempt at a solution, not your real problem.

That is, you are trying to solve problem X, and you think that solution Y will work, but instead of asking about X, when you run into difficulties, you are asking about Y.

You really need to define a free action group and other groups running under login control.

To do this, you can manually include the Interceptor in each individual action (useful when using the Convention plugin, a waste of time otherwise) or logically configure the actions in struts.xml.



<package>

- your friend here: define two (or more) packages, one of which works with default settings, and the other runs your custom Interceptor for each package action:

<package name="unsecure-package" namespace="/unsecure" extends="struts-default">
    <action name="login" class="org.foo.bar.actions.LoginAction">
        ...
    </action>
    <action name="askHelp" class="org.foo.bar.actions.AskHelpAction">
        ...
    </action>    
</package>

<package name="secure-package" namespace="/secure" extends="struts-default">
    <interceptors>
        <interceptor name="authInterceptor" 
                    class="org.foo.bar.interceptor.AuthInterceptor"/>

        <interceptor-stack name="securedStack">
            <interceptor-ref name="authInterceptor"/>
            <interceptor-ref name="defaultStack"/>
        </interceptor-stack>
    </interceptors>

    <default-interceptor-ref name="securedStack"/>

    <action name="write" class="org.foo.bar.actions.WriteAction">
        ...
    </action>
    <action name="delete" class="org.foo.bar.actions.DeleteAction">
        ...
    </action>        
</package>

      

This way, every time you or someone else add an action to struts.xml, you just need to drop it in the package you want and it will work automatically.

It is better to always support the action of the interceptor agent - agnostic;)

+1


source







All Articles