开发者

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 contr开发者_运维知识库oller looks as follows (if it helps solve the problem at all..):

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


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

/channel1/{id}/{random_text}

This is to be covered by url-pattern of /channel1/*. Note the trailing /*. The url-pattern of /channel1 would not accept anything more in the pathinfo behind, it would only accept the exact URL and the optional query parameters.


The best practice of doing things for rest format is to keep all your URL's which need to be processed by the DispatcherServlet in a separate namespace like web, so that the static resources will just be served directly and all your controller urls will be passed through Dispatcher, here is an example..

<servlet>
    <servlet-name>web</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <init-param>
        <param-name>namespace</param-name>
        <param-value>web</param-value>
    </init-param>
    <init-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>/WEB-INF/web-servlet.xml</param-value>
    </init-param>

    <load-on-startup>1</load-on-startup>
</servlet>

<servlet-mapping>
    <servlet-name>web</servlet-name>
    <url-pattern>/web/*</url-pattern>
</servlet-mapping>


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}" })
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜