Hans,

Comments in the text below.

Niall

> -----Original Message-----
> From: Hans Bure [mailto:[EMAIL PROTECTED]]
> Sent: 14 June 2001 17:40
> To: [EMAIL PROTECTED]
> Subject: Multiple Forwards
>
>
> Hi all,
>
> I am a relatively new struts user, so if the question I'm asking has been
> answered before, I appologize ahead of time.
>
> What I'm trying to do, is create a framework integrated with struts that,
> among other things, allows users to set up multiple forwards dependent on
> the broswer type.  This could be Netscape, IE, WAP browser, or others.
>
> What I would like to do is something like the following in the
> struts-config.xml file:
>
> ...
> <action name="test">
>   ...
>   <forward name="success" browser="IE" path="/ie/test.jsp"/>
>   <forward name="success" browser="NS" path="/ns/test.jsp"/>
>   <forward name="success" browser="WAP" path="/wap/test.jsp"/>
>   ...
> </action>
> ...

You can't have multiple forwards with the same name because they are stored
in a HashMap which uses the name as the key - so they will just overwrite
each other.


>
> The reasoning behind this, would be to create an additional
> attribute that
> could be used by my framework internals that determines the browser and
> calls the correct JSP for the user automatically.
>
> Something like:
>
>   ...
>   return myMapping.findForward("success");
>   ...
>
> Which calls into a wrapper class that I create and determines the
> browser,
> then calling the appropriate JSP as defined in the config file.
> This makes
> it very simple for the user, as he only needs to define a series of
> 'success's, and the Action code is simple as defined above.
>
> Now, obviously, adding an additional attribute to the forward
> above is not trivial.

Creating your own custom ActionForward is straight forward, but I don't
think it gives you much benefit.

Extend ActionForward and add the addtional properties you require.....


/* CustomActionForward Class ****** START ******/
public Class CustomActionForward extends ActionForward {

  private String browser;

  public void setBrowser(String browser){
    this.browser = browser;
  }

  public String getBrowser(){
    return browser;
  }
}
/* CustomActionForward Class ****** END ******/

Then you need to tell Struts to use your CustomActionForward.....

In the <servlet> entry in the web.xml file add the following <init-param>

  <init-param>
    <param-name>forward</param-name>
    <param-value>myPackage.CustomActionForward</param-name>
  </init-param>

Then in your struts-config.xml file use <set-property> to initialise your
addtional properties:

  <action ................>
    <forward name="ie_success" path="/ie/test.jsp>
      <set-property property="browser" value="ie"/>
    </forward>
    <forward name="ns_success" path="/ns/test.jsp>
      <set-property property="browser" value="ns"/>
    </forward>
    <forward name="wap_success" path="/wap/test.jsp>
      <set-property property="browser" value="wap"/>
    </forward>
  </action>

Obviously this is not very elegant and I you probably won't want to do this.

> So my question, is has anyone seen anything like this, and is
> there a way I haven't thought of to do this within the current struts
> framework?
>
> One thought I've had would be to do the following:
>
>   <forward name="ie_success" path="/ie/test.jsp"/>
>   <forward name="ns_success" path="/ns/test.jsp"/>
>   <forward name="wap_success" path="/wap/test.jsp"/>
>
> This would work and not require many changes to the framework,
> but I would
> rather not do this, as it would make things very difficult for my
> users to create their own actions.

Personally this doesn't look any more difficult for you user than what you
are proposing below.

>
> Another possibility is the following:
>
>   <forward name="success" path="test.jsp" />
>   <browserforward name="success" browser="ie" path="/ie/test.jsp" />
>   <browserforward name="success" browser="ns" path="/ns/test.jsp" />
>   <browserforward name="success" broswer="wap" path="/wap/test.jsp" />
>
> In this case, I would need to create another XML entry that kept
> all of the
> 'success' forwards for each action in its own little HashTable, much like
> the normal forwards, except keyed by a combination of the name and the
> browser attributes.
>
> I honestly don't know how difficult this would be, but it seems like the
> best solution I've come up with so far.  One obvious drawback to
> this one is
> that the struts DTD would need to be altered to include the
> 'browserforward'
> element.  I am not sure of the implications of this.

It would be a pain every time you took a new release of Struts - I wouldn't
do this.

>
> Any thoughts?

If you don't mind the constraints it imposes how about this for a solution:


For forwards which are browser specific create your entries in
struts-config.xml as follows:

   <forward name="success" path="/$browser$/test.jsp"/>

Then in your action you need to do something along the following lines:

  // Determine the Browser being used
  String browser = ............?

  // Get the ActionForward
  ActionForward origForward = mapping.findForward("success");

  // replace /$browser$ in the path with the browser
  String origPath = origForward.getPath();
  if (origPath.substring(1, 10).equalsIgnoreCase("/$browser$")) {
    String newPath = "/"+browser+origPath.substring(10);
    return new ActionForward(newPath);
  } else {
    return origForward;
  }

This has its restrictions, but would be simple to set up in your
struts-config.xml as you wouldn't need browser dependant forwards.


>
> Thanks for your time.
>
> Hans Bure
> _________________________________________________________________
> Get your FREE download of MSN Explorer at http://explorer.msn.com
>

Reply via email to