【问题标题】:URL Rewriting for HTML5 History Server-Side SupportHTML5 历史服务器端支持的 URL 重写
【发布时间】:2013-12-24 09:46:17
【问题描述】:

假设我们有一个单页 (index.html) Web 应用程序,它使用 HTML5 History API 在客户端提供对以下 URL 的访问:

www.myapp.com/
www.myapp.com/screen1
www.myapp.com/screen2

我们将如何在服务器端为screen1screen2 提供支持?

【问题讨论】:

  • 您应该在片段部分(waht 跟随井号)中酸痛历史,即。 www.myapp.com/#screen1 www.myapp.com/#screen2 将使其在语义上相同的页面,但不同的部分。

标签: java guice html5-history


【解决方案1】:

使用Guice,我们需要配置我们的服务器为我们的HTML5 URLs调用RouteServlet

serve("/screen1", "/screen2").with(RouteServlet.class);


RouteServlet.java

@Singleton
public class RouteServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.getRequestDispatcher("index.html").include(req, resp);
    }

}

【讨论】:

    【解决方案2】:

    如果您不使用 MVC 框架,最好的解决方案之一是使用 .htaccess

    # html5 pushstate (history) support:
    <ifModule mod_rewrite.c>
        RewriteEngine On
        RewriteCond %{REQUEST_FILENAME} !-f
        RewriteCond %{REQUEST_FILENAME} !-d
        RewriteCond %{REQUEST_URI} !index
        RewriteRule (.*) index.html [L]
    </ifModule>
    

    More details here

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2015-06-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-07-04
      • 2014-01-18
      • 1970-01-01
      相关资源
      最近更新 更多