freemarker cannot handle request for '/' on google app engine

允我心安 提交于 2019-12-06 06:55:47

问题


I'm trying to get freemarker working under Google App Engine. I've gotten simple ftl template files working when requested directly however I'd like to have index.ftl work if available otherwise index.html -- or vice versa.

  • If I request /index.html, it renders fine as HTML.
  • If I request /index.ftl, it renders fine as a FreeMarker template. Variables are expanded.
  • If, however, I request '/' it gives the following 404 message:

    Problem accessing /index.html/index.ftl.

I have the following in my web.xml file:

<?xml version="1.0" encoding="utf-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee"
     xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
        http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
        version="2.5">
    <welcome-file-list>
        <welcome-file>index.html</welcome-file>
        <welcome-file>index.ftl</welcome-file>
    </welcome-file-list>
    <servlet>
        <servlet-name>freemarker</servlet-name>
        <servlet-class>freemarker.ext.servlet.FreemarkerServlet</servlet-class>
        <init-param>
            <param-name>TemplatePath</param-name>
            <param-value>file://ftl</param-value>
        </init-param>
        <load-on-startup>1</load-on-startup>
    </servlet>
    <servlet-mapping>
        <servlet-name>freemarker</servlet-name>
        <url-pattern>*.ftl</url-pattern>
    </servlet-mapping>
</web-app>

I have the following in the war/ directory of my Eclipse GAE application:

  • /index.html
  • /ftl/index.ftl
  • /WEB-INF/...

If I switch the order of the index.ftl and index.html entries, a request for / gives the following 404 message:

Problem accessing /index.ftl/index.ftl.

Thanks for any help.


One additional bit of information is that if I have one <welcome-file> entry of index.html, it works fine. When I add the index.ftl, in any order, is when I get the errors.

Thanks for any help.


回答1:


I think the problem here is pretty similar the problem of using a struts action as a welcome page. Quoting Damien B's answer from that question

There isn't ( a better way other than using a jsp redirect). Servlet specifications (Java Servlet Specification 2.4, "SRV.9.10 Welcome Files" for instance) state:

The purpose of this mechanism is to allow the deployer to specify an ordered list of partial URIs for the container to use for appending to URIs when there is a request for a URI that corresponds to a directory entry in the WAR not mapped to a Web component.

Since it is mapped to directory entry and not a mapped web component, the "/" isn't forwarding to the freemarker servlet when index.ftl is the welcome file.

I suggest trying the same approach used to make actions a welcome page. Which is have a jsp fwd to your index.ftl.




回答2:


I'm still looking for the solution to this (although @Andy Pryor's answer may be ultimately right) but I thought that I'd note what I've done to work around this issue.

I ended up moving all of my html files into the FreeMarker view hierarchy so that all HTML and FreeMarker files are processed by the FreeMarker servlet. I don't have to support *.ftl files since I will never be rendering them directly anyway. So the only files I have in my static hierarchy are images and the like.

This seems to be working well although I had to subclass the FreemarkerServlet to block the getSession() methods made on the request since my app does not have sessions enabled. Here's my web.xml file:

<?xml version="1.0" encoding="utf-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns="http://java.sun.com/xml/ns/javaee"
    xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
        http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
    version="2.5">

    <welcome-file-list>
        <welcome-file>index.html</welcome-file>
    </welcome-file-list>
    <servlet>
        <servlet-name>freemarker</servlet-name>
        <servlet-class>freemarker.ext.servlet.FreemarkerServlet</servlet-class>
        <init-param>
            <param-name>TemplatePath</param-name>
            <param-value>file://views</param-value>
        </init-param>
        <load-on-startup>1</load-on-startup>
    </servlet>
    <servlet-mapping>
        <servlet-name>freemarker</servlet-name>
        <url-pattern>*.html</url-pattern>
    </servlet-mapping>
</web-app>


来源:https://stackoverflow.com/questions/4127610/freemarker-cannot-handle-request-for-on-google-app-engine

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!