2

Good afternoon in my timezone.

I am developing a web application using struts framework. In a simple way, when the user call the application the first Action to be call is the SecurityAction, then this action redirects to one of the two actions, this is how i make the redirect :

     if (user == "type_profile")
           forward = mapping.forward("action2Fwd");
     else
           forward = mapping.forward("action3Fwd");
     return forward;

In the struts-config.xml i have

<global-forwards>
    <forward name="action2Fwd" path="/action2.do"/>
    <forward name="action3Fwd" path="/action3.do"/>
 </global-forwards>
<action path="/action2"
            type="com.teste.dummy.action2"
            name="actionForm" 
            validate="true"
            input="/action2.jsp">
 </action>

My first question: Is this the best way to redirect from an action to another action ?

Second question : When i redirect to another action, the actionForm will execute, how can i know that this request come from another action ?

Inside the form if the request comes from another action i do not want to validate anything , so i have to know that this request comes from another action and not from the "browser", one solution is to put some kind of flag in the request or session scope indicating that this request comes from another action, but is this the best way ?

Dave Newton
  • 158,873
  • 26
  • 254
  • 302
tt0686
  • 1,771
  • 6
  • 31
  • 60

2 Answers2

1

If you need to enable/disable validation based on where the form is coming from the easiest solution would be to put a flag in the form.

The flag can be processed by a custom request processor to provide application-wide behavior. Less elegantly, an action base class could manually call validation or not based on its presence.

There's no other great way to forward between actions other than defining the forward and returning it--whether it's global or not is secondary, and depends on your application's needs.

Dave Newton
  • 158,873
  • 26
  • 254
  • 302
  • 1
    But is there any way to find who redirect this request ? Something like -> mapping.getWhoRedirect() – tt0686 Dec 14 '11 at 17:08
  • @tt0686 By putting that information into the form (or request, or whatever). The same flag can be used both to identify that it came from another action, and which action it came from. I'm not convinced this is a *good* architecture, but the mechanism is sound. You could *try* using the referrer, but meh. – Dave Newton Dec 14 '11 at 17:14
0

I have had to do that with one of my projects. This is how I solved my problem. I hope it works out for you.

I called this action SelectCopyFromProjectAction.do from my jsp using JavaScript. This action called another class, which did more work.

From jsp

function selectThisCopyProject(){
    document[0].action = "SelectCopyFromProjectAction.do";
    submitForm2();
}       

struts.config

<action path="/SelectCopyFromProjectAction" type="*****.SelectCopyFromProjectAction" scope="request">
    <forward name="success" path="LoadProjectionCopyLOBAction.do?actionType=loadProjects" />
    <forward name="failure" path="/WEB-INF/jsp/project_list.jsp"/>
</action>

Secondary Class Forwards a new action as seen in the struts. config <forward name="success" path="LoadProjectionCopyLOBAction.do" />

If the forward is "success" it leads to this action in the config:

<action path="/LoadProjectionCopyLOBAction" type="*******.LoadProjectionCopyLOBAction" name="ProjectCopyFormBean" validate="false" scope="request"  >
    <forward name="" .... />
</action>
staples89
  • 167
  • 3
  • 14