Search code examples
javaspringservletsurl-mapping

Spring servlet mapping - no css or jsp!


I read over this post, which is similar to my issue, but had no luck solving the problem:

Basically I used to have the following servlet-mapping in my web.xml:

<servlet-mapping>
  <servlet-name>myServlet</servlet-name>
  <url-pattern>/index.html</url-pattern>
  <url-pattern>/channel1</url-pattern>
  <url-pattern>/channel2</url-pattern>
</servlet-mapping>

This worked perfectly until I needed to map the following url:

/channel1/{id}/{random_text}

Where {id} is a numeric ID value of my objects and {random_text} is just there just for "freindly urls". I managed to get this to work using the @RequestMapping in my controller as well as the @PathVariable to pull the variables from the URL.

However, the only way I managed to get the new URL to map successfully is be adding

<url-pattern>/</url-pattern>

to my web.xml at the bottom of my servlet-mappings. BUT, when I do this, all my other pages (/channel1, /channel2) display without access to the static content (css, jsp etc); I get a No mapping found for HTTP request with URI for the static files. I tried various combinations of mappings as suggested in the link I posted, but nothing worked. Any help would be great!!!

Update: my RequestMapping in the controller looks as follows (if it helps solve the problem at all..):

@RequestMapping(value = { "/channel1/{id}", "/channel1/{id}/{text}" })

Solution

  • I realized what the issue is (probably my fault for not elaborating on my @RequestMapping setup in the Controller earlier):

    In my web.xml I had a url-pattern:

    <url-pattern>/channel/*</url-pattern>
    

    Also, in my controller I used the following mapping:

    @RequestMapping(value = { "/channel1/{id}", "/channel1/{id}/{text}" })
    

    The problem was that I was duplicating the /channel1 portion. I randomly (luckily) came across this post explaining this issue.

    Long story short, when I changed my mapping in the controller to the following it works perfectly:

    @RequestMapping(value = { "/{id}", "/{id}/{text}" })