java - AngularJS HTML5 Mode - How do direct links work without server specific changes? -


note: question read:

how support bookmarking of hashbang-less client side mvc frameworks in java.

i transitioning angular app uses hashtags 1 html5mode. have set

$locationprovider.html5mode(true); 

and links landing page (index.html) work fine.

the problem is, if partial urls referenced directly, 404, naturally, since server endpoint definitions aren't coupled client side-defined routes.

so without html5 non-seo friendly hashbangs, cannot bookmark other landing page (the page bootstraps angular).

why works if requesting default landing page (index.html) first, ie htpp://mydomain.com/ :

  1. browser requests index.html server
  2. server returns index.html, , browser loads angular framework
  3. url changes sent client side router , proper partial/s loaded.

why doesn't work if (ie) http://mydomain.com/foo requested directly browser:

  1. browser requests mydomain/foo server.
  2. resource doesn't exist
  3. server returns 404

something missing story, don't know what. here 2 answers can see...

  • it's design. how supposed work? users must land on client mvc framework's bootstrap page (usually index.html), , navigate there. not ideal because state cannot saved , there no way bookmark... not mention crawling.
  • server solution. worked around server side trick? example, on requests, return index.html , call router additional context. if so, against objective angularjs client-side , seems hack.

the angularjs documentation in fact mention this

server side using mode requires url rewriting on server side, have rewrite links entry point of application (e.g. index.html)

in case, 1 java-based solution tell server "map urls index.html." can done in http server or container. implemented using java/servet since want application http server agnostic (ie apache versus nginx, or tomcat/jboss only).

in web.xml:

  <welcome-file-list>         <welcome-file>index.jsp</welcome-file>   </welcome-file-list>    <servlet>       <servlet-name>staticservlet</servlet-name>       <jsp-file>/index.jsp</jsp-file>   </servlet>    <servlet-mapping>       <servlet-name>staticservlet</servlet-name>       <url-pattern>/app</url-pattern>   </servlet-mapping> 

and index.jsp looks like:

<%@ include file="index.html" %> 

and add following tag within index.html:

<base href="/app" /> 

Comments