You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@struts.apache.org by "Glencross, Christian" <Ch...@gs.com> on 2000/08/08 13:08:34 UTC

Prefix mapping

The Struts user guide talks about the ability to use either prefix mapping
('/execute/myaction') or extension mapping ('myaction.do').

I've got the extention mapping working fine, but not prefix mapping.

Using the example from the user guide, I've included the following mapping
in my web.xml:
    <servlet-mapping>
      <servlet-name>action</servlet-name>
      <url-pattern>/execute/*</url-pattern>
    </servlet-mapping>

When I try accessing the URL http://myserver/struts/execute/myaction, I get
an error "No mapping for path '/execute'". The ActionServlet should, as I
understand it, be looking for the mapping for the path "/myaction".

Is there a problem in what I'm doing, in my servlet engine (WebLogic) or in
the ActionServlet.processPath method?

As a workaround, I'm going to override processPath.

Thanks in advance for any advice.

Chris

Re: Prefix mapping

Posted by Pierre Delisle <pi...@sun.com>.
Christian,

Funny you should bring the issue. Just stumbled on that one last night
and was about to post a patch when I saw your mail.

Here is the scoop and the patch:

-----
I'd like to use prefix matching with struts (as described in 
section "4.5.2 - Configure the Action Servlet Mapping".

I've therefore defined the url-pattern for the struts ActionServlet
to be as follows:

      <url-pattern>/exec/*</url-pattern>

This works fine. All request URIs with '/exec/*' are mapped to the
ActionServlet.

However, I'd expect that for a url such as:
   
   'http://localhost:8080/myapp/exec/adduser'

ActionServlet would dispatch the request to the mapping defined for
action 'adduser'.

This does not work.

When the request comes to the ActionServlet, it is decomposed
as follows:

    servlet path: /exec
    path  info:   /adduser

Because the dispatching in ActionServlet is done on the servlet path,
the
'adduser' in the pathinfo is never considered.

It works with extension matching (e.g. when the url pattern is something
like '*.do'),
because with something like 'http://localhost:8080/myapp/adduser.do' we
have:
    servletpath: adduser.do
    pathinfo: null

and ActionProcess:processPath() strips the  servlet uses 'adduser' for
the action mapping.

The patch below adds support for prefix mapping.

-----
    protected String processPath(HttpServletRequest request) {
        String path = null;
        
        // prefix matching -- the action is in the path info
        path = request.getPathInfo();
        if (path != null) return (path);

        // extension matching -- action name is in the servlet path
        path = request.getServletPath();
        int slash = path.lastIndexOf("/");
        int period = path.lastIndexOf(".");
        if ((period >= 0) && (period > slash))	// Strip the extenson (if
any)
            path = path.substring(0, period);
	return (path);
    }



  -- Pierre


"Glencross, Christian" wrote:
> 
> The Struts user guide talks about the ability to use either prefix mapping
> ('/execute/myaction') or extension mapping ('myaction.do').
> 
> I've got the extention mapping working fine, but not prefix mapping.
> 
> Using the example from the user guide, I've included the following mapping
> in my web.xml:
>     <servlet-mapping>
>       <servlet-name>action</servlet-name>
>       <url-pattern>/execute/*</url-pattern>
>     </servlet-mapping>
> 
> When I try accessing the URL http://myserver/struts/execute/myaction, I get
> an error "No mapping for path '/execute'". The ActionServlet should, as I
> understand it, be looking for the mapping for the path "/myaction".
> 
> Is there a problem in what I'm doing, in my servlet engine (WebLogic) or in
> the ActionServlet.processPath method?
> 
> As a workaround, I'm going to override processPath.
> 
> Thanks in advance for any advice.
> 
> Chris