Announcement Announcement Module
Collapse
No announcement yet.
Changing to REST, preventing unwanted URL calls Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Changing to REST, preventing unwanted URL calls

    Hi,

    I'm switching over to REST style URLs.
    I use Spring 3.0, Sitemesh and Freemarker with Sitemesh decorators stored in WEB-INF/decorators.

    The problem I have is that when I change my web.xml to not just send *.html to the dispatcher but /*, I get handlerNotFound errors on decorators that it apparently tries to call directly.

    Orginally, my web.xml had stuff like so
    PHP Code:
        <filter-mapping>
            <
    filter-name>gzipFilter</filter-name>
            <
    url-pattern>*.html</url-pattern>
        </
    filter-mapping>

        <
    filter-mapping>
            <
    filter-name>gzipFilter</filter-name>
            <
    url-pattern>*.jsp</url-pattern>
        </
    filter-mapping>
        <
    filter-mapping>
            <
    filter-name>exportFilter</filter-name>
            <
    url-pattern>*.html</url-pattern>
        </
    filter-mapping>
        <
    filter-mapping>
            <
    filter-name>rewriteFilter</filter-name>
            <
    url-pattern>//*</url-pattern>
            
    <dispatcher>REQUEST</dispatcher>
            <
    dispatcher>FORWARD</dispatcher>
        </
    filter-mapping
    PHP Code:
        <filter-mapping>
            <
    filter-name>sitemesh</filter-name>
            <
    url-pattern>//*</url-pattern>
            
    <dispatcher>REQUEST</dispatcher>
            <
    dispatcher>FORWARD</dispatcher>
        </
    filter-mapping>
        <
    filter-mapping>
            <
    filter-name>staticFilter</filter-name>
            <
    url-pattern>*.html</url-pattern>
            <
    dispatcher>REQUEST</dispatcher>
            <
    dispatcher>FORWARD</dispatcher>
        </
    filter-mapping>

        <
    servlet>
            <
    servlet-name>sitemesh-freemarker</servlet-name>
            <
    servlet-class>
                
    nl.project.webapp.utils.SpringFreemarkerDecoratorServlet</servlet-class>
            <
    init-param>
                <
    param-name>TemplatePath</param-name>
                <
    param-value>/</param-value>
            </
    init-param>
            <
    init-param>
                <
    param-name>default_encoding</param-name>
                <
    param-value>ISO-8859-1</param-value>
            </
    init-param>
        </
    servlet>
        <
    servlet-mapping>
            <
    servlet-name>sitemesh-freemarker</servlet-name>
            <
    url-pattern>*.ftl</url-pattern>
        </
    servlet-mapping>
        <
    servlet-mapping>
            <
    servlet-name>dispatcher</servlet-name>
            <
    url-pattern>*.html</url-pattern>
        </
    servlet-mapping
    Never mind the double forward slashes. The forum software chokes on /*.

    When I change the *.html mappings to /* I get errors as it can find the non-existing mappings for
    /WEB-INF/decorators/somedecorator.ftl

    Any ideas on how to solve this?

    Kind regards,

    Marc

  • #2
    a small step

    Well, this helped me *a bit* further.

    Apparently, if you use
    PHP Code:
    <servlet-mapping>
            <
    servlet-name>dispatcher</servlet-name>
            <
    url-pattern>/</url-pattern>
        </
    servlet-mapping
    in stead of
    PHP Code:
    <servlet-mapping>
            <
    servlet-name>dispatcher</servlet-name>
            <
    url-pattern>/*</url-pattern>
        </servlet-mapping> 
    It will just include patterns from the url, not everything else tomcat throws your way (images,css,/WEB-INF/decorators/main.ftd, etc). This unfortunately also means, that none of the static resources such as images, stylesheets etc are displayed anymore which is especially pesky during debugging against the localhost.

    Against Apache you could resolve this by not letting those static resources pass through Tomcat in the first place.

    This is not really a solution. There must be an easy way to prevent the dispatcher from trying to resolve certain url patterns. Any suggestions? There must be some easy way?

    Cheers,

    Marc

    Comment


    • #3
      Solved

      Ok, two solutions:
      1. use a prefix for all your controller mappings and use urlRewrite to rewrite incoming urls to that controller. Not so cool if you have a large existing project. You will have to change all your annotated controllers. This is the best way if you're starting a new project
      2. use the default servlet in web.xml to catch all your static resources.
      The default is available "by default". You don't have to explicitly define it. Not so cool if you can't predict all the file extensions to be served but easy to implement

      So, 1 would be
      PHP Code:
          <filter>
              <
      filter-name>rewriteFilter</filter-name>
              <
      filter-class>org.tuckey.web.filters.urlrewrite.UrlRewriteFilter</filter-class>
              <
      init-param>
                  <
      param-name>logLevel</param-name>
                  <
      param-value>log4j</param-value>
              </
      init-param>
          </
      filter>
          <
      filter-mapping>
              <
      filter-name>rewriteFilter</filter-name>
              <
      url-pattern>//*</url-pattern>
              
      <dispatcher>REQUEST</dispatcher>
              <
      dispatcher>FORWARD</dispatcher>
          </
      filter-mapping>
          <
      servlet-mapping>
              <
      servlet-name>dispatcher</servlet-name>
              <
      url-pattern>/prefix//*</url-pattern>
          
      </servlet-mapping
      use WEB-INF/urlrewrite.xml to define your regular expression url pattern matcher

      PHP Code:
      2 would be 
          
      <servlet-mapping>
              <
      servlet-name>dispatcher</servlet-name>
              <
      url-pattern>/</url-pattern>
          </
      servlet-mapping>
       
          <
      servlet-mapping>
              <
      servlet-name>default</servlet-name>
              <
      url-pattern>*.css</url-pattern>
          </
      servlet-mapping>
          <
      servlet-mapping>
              <
      servlet-name>default</servlet-name>
              <
      url-pattern>*.png</url-pattern>
          </
      servlet-mapping>
          <
      servlet-mapping>
              <
      servlet-name>default</servlet-name>
              <
      url-pattern>*.js</url-pattern>
          </
      servlet-mapping
      Don't mind the double //*. It's the forum software

      HTH,

      Marc

      Comment

      Working...
      X