Hi Zarar,

Being crazy takes its toll; please have exact change. ;) If you re-read my response you will see that I recognise that Jennie is using Struts 1.x. I suppose I mentioned Struts 2 because you can *almost* do the URL mapping that she requires with it. I say almost because the solution below redirects "recipes/4735/steak-with-barbecue-sauce.jsp" to "recipes/resultDetails.action?number=4735&name=steak-with-barbecue-sauce". It would possibly be nicer if we could use the chain result type but I think you could run into parameter difficulties.

E.g.

<struts>
   <constant name="struts.enable.SlashesInActionNames" value="true" />
<constant name="struts.action.extension" value="action,jsp" /> <package name="recipes" namespace="/recipes" extends="struts-default"> <action name="*/*">
           <result type="redirect-action">
               <param name="actionName">resultDetails</param>
               <param name="number">{1}</param>
               <param name="name">{2}</param>
           </result>
</action> <action name="resultDetails" class="com.pub.actions.RecipeDetailsAction">
           <result>/recipeDetailsSuccess.jsp</result>
</action> </package>
</struts>

I appreciate that Struts 1.2+ allow wildcards in action mappings, i.e.:

<http://struts.apache.org/1.3.8/userGuide/building_controller.html#action_mapping_wildcards>
<http://www.lunatech-research.com/archives/2006/01/23/struts-action-mappings>

but the problem here is how to pass the multiple parameters that we require. Hence my suggestion that it would be easier to use URL mapping (e.g. mod_rewrite or similar).

I hope this clears things up for you.

Mark


Zarar Siddiqi wrote:
Am I crazy or is Jennie using Struts 1.x and Mark just proposed a
solution for 2.x?

Jennie, your web.xml servler-mapping is overriding each other, you're
first saying you want /*/*.jsp sent to action but then you're saying
you want *.do's sent to action? I'm not too sure if that's valid.
Have you tried just using *.jsp like the following and see how far you
get?

<servlet-mapping>
       <servlet-name>action</servlet-name>
       <url-pattern>*.jsp</url-pattern>
   </servlet-mapping>

Zarar


On 9/28/07, Jennie Moeller <[EMAIL PROTECTED]> wrote:
Thank much Mark,

I talked with a colleague and we're going to give the urlrewrite a try.
Thanks!
-----Original Message-----
From: Mark McLaren [mailto:[EMAIL PROTECTED]
Sent: 28 September 2007 12:02
To: Struts Users Mailing List
Subject: Re: Mapping a jsp to an action

Hi Jennie,

I am fairly new to Struts 2 but you can certainly do action wildcard
mappings with it, such as:

<action name="*" >
  <result>/{1}.jsp</result>
</action>

where foo.action would map to foo.jsp.

However, my guess is that what you are asking for is a little bit more
advanced (and judging by the "*.do" you are not using Struts 2).  You
might be better to look at using mod_rewrite on apache if you can.
Alternatively you could create or use something like the URL rewriting
servlet filter below.

<http://tuckey.org/urlrewrite/>

A very simple URL mapping servlet would do the trick and look something
like the servlet below.  I'm sure that  other other here would be able
to improve on this with some clever one line regular expression but at
least it gives you an idea of how to do it:

public class UrlMappingServlet extends HttpServlet {

    public void doGet(HttpServletRequest req, HttpServletResponse
resp) throws ServletException, IOException {
        String path=req.getServletPath();
        String queryString = req.getQueryString();

        // cut off the extension and leading slash
        int end = path.lastIndexOf(".jsp");
        path = path.substring(1, end);

        // split the path into components
        String[] pathcomponents = path.split("/");

        // 0 -recipes
        // 1 - number
        String number = pathcomponents[1];
        // 2 - recipe name
        String name = pathcomponents[2];

        String destination = "/recipeDetails.jsp"?number="
               + number + "&name=" + name + "&" + queryString;
        RequestDispatcher rd;
        rd = req.getRequestDispatcher(destination);
        rd.forward(req, resp);
    }

}

Of course, best practice states you should never go directly to a JSP
url in a Struts app.  You should always go via an action.

HTH

Mark

On 9/28/07, Jennie Moeller <[EMAIL PROTECTED]> wrote:
Hi,

I'm trying to map a jsp page to an action. When any jsp gets loaded
with the recipe path on it I want it to go to an action.

for example:

This uri: recipes/4735/steak-with-barbecue-sauce.jsp

I want it to hit my

recipeDetails action

My struts action looks like this:

<action path="/recipes/**"
            type="com.pub.actions.RecipeDetailsAction"
            name="recipeDetailsForm"
            scope="request"
      validate="false">

            <forward redirect="false" name="success"
path="/WEB-INF/jsp/recipeDetails.jsp" />
        </action>



My web.xml looks like this:

<servlet-mapping>
  <servlet-name>action</servlet-name>
  <url-pattern>/recipes/*/*.jsp</url-pattern>
 </servlet-mapping>

    <servlet-mapping>
        <servlet-name>action</servlet-name>
        <url-pattern>*.do</url-pattern>
    </servlet-mapping>


any ideas?

thanks,
Jennie


---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to