How to Redirect to Another Action Class Without Using on Struts.Xml

Is there a way to redirect to another Action class without using on struts.xml

Looks like you want to check in the constructor of the base action class, but you are mistaken. The constructor is used by the object factory to instantiate your action instance. In this stage a few things is available to you. In your case it's wrong. Another approach is if you move the logic into the method say execute() and call super.execute() before any method call would work, but if you forget to put the super call in the action then you may end up the action code running not authenticated. To prevent it you should run the code before any action is executed and be able to access the action instance or action context to be more Struts2. I guess you've never read the book Struts 2 in Action so I will give you some my own thoughts. It's about creating AuthenticationInterceptor and the action that implements UserAware that injects the user logged in into the action that implement this interface. The interceptor is looks like

public class AuthenticationInterceptor implements Interceptor {

public void destroy() {
}

public void init() {
}

public String intercept(ActionInvocation actionInvocation) throws Exception {
Map session = actionInvocation.getInvocationContext().getSession();
User user = (User) session.get(Struts2MyConstants.USER);

if (user == null) {
return Action.LOGIN; //login required result
}
else {
Action action = (Action)actionInvocation.getAction();

if (action instanceof UserAware) {
User freshUser = myService.getUser(user.getId());
((UserAware)action).setUser(freshUser);
}

System.out.println("Logged in: interceptor");
return actionInvocation.invoke();
}
}

The UserAware is looks like

public interface UserAware {

public void setUser( User user );

}

and make a secure default stack that will reference any action

<interceptors>
<interceptor name="authenticationInterceptor" class="org.yourapp.struts.interceptor.AuthenticationInterceptor"/>
<interceptor-stack name="secureStack">
<interceptor-ref name="authenticationInterceptor"/>
<interceptor-ref name="defaultStack"/>
</interceptor-stack>
</interceptors>
<default-interceptor-ref name="secureStack"/>

If you make your base action to implement UserAware then the user object that is logged in will be available not only from session but in action too if you define getter for the user or make it protected. You have to make the User object immutable so that not compromise the security feature.

Redirecting to another action with unknown amount of parameters in Struts 2

You can save action name, namespace, and parameters from the ActionMapping.

ActionMapping mapping = ServletActionContext.getActionMapping();

You can also save query string instead of parameter map.

String params = request.getQueryString();

To add parameters dynamically to redirectAction result you should use OGNL in a dynamic parameter.

<param name="actionName">${previousAction.name +'?'+ parameters}</param>

Supposed you have a getter for parameters and initialized it from session where you saved previous query string, action name, and namespace.

Why can't i redirect an action to another action in Struts2?

Current Configuration:

<action name="add_user" class="org.apache.struts.gestion_edt.controller.adm_proyectos.BLTipoEntregable" method="addUser">
<result name="success" type="redirectAction">search_users</result>
</action>

As per the documentation correct format is:

<action name="add_user" class="org.apache.struts.gestion_edt.controller.adm_proyectos.BLTipoEntregable" method="addUser">
<result type="redirectAction">
<param name="actionName">search_users</param>
<!--<param name="namespace">/secure</param> This is optional if your action where you are redirecting is in the same namespace you can leave this, if your action is in some other name space then provide the namespace-->
</result>
</action>

Action redirect in struts.xml

Yes. You can redirect and you can chain. Redirect starts from scratch, it is like you called the other action for the first time while chain keeps the values on the value stack and adds the variables of the new action.

To forward:

<action name="newRedirect" >
<result type="redirect">/formsearch.action</result>
</action>

To chain:

<action name="newRedirect" >
<result type="chain">formsearch</result>
</action>

As a convenience the redirect result type can be changed to a "redirectAction" result type... which lets us write:

 <action name="newRedirect" >
<result type="redirectAction">formsearch</result>
</action>

This last one is probably what you want.

Now a warning, chaining/action redirection is up there with the "goto" statement. Not evil but easy to abuse, you should probably look to moving the deciding logic (the logic that determines what action to call of several to an interceptor) or if the logic is mostly setup related then some type of utility class that is invoked by the actions prepare method (or into the prepare method outright)... If the action needs parameters before prepare is called then use the paramsPrepareParamsStack.



Related Topics



Leave a reply



Submit