How to configure Weblogic application Sever for Angular application

Viewed 3516

I have an Angular (6.1) application that is packed in a WAR (and EAR) file and should be deploy to a Weblogic (12c).
Based on this link all requests to the application (context root) should be routed to the index.html application file.

There some configuration examples in the documentation but non of them for an application server like WebLogic. As it should be coupled with the application it shall go within the WAR and I thought of using the servlet mapping in the web.xml. I played around with it but don't get it working. (internal server error, Not Found for other views than default ... even if I got it working with plain WAR in a tomcat, WebLogic refuses to do the same, ...)

Before putting there to much time in it - is this the right way?
If so, how would the correct mapping/pattern look like?
If not, what is the other way to have it configured within the WAR?

3 Answers

If it should be more comfortable than creating your own filter you could use: org.tuckey.urlrewritefilter http://tuckey.org/urlrewrite/

Simple 3 steps:

  1. Add the dependency to your pom.xml
  2. Add the filter UrlRewriteFilter from tuckey to the web.xml
  3. Configure the filter in a urlrewrite.xml

From my experience it is very handy (especially compared to the supported patterns in web.xml).
An example rule could be:

<rule>
    <from>^/webapp/*</from>
    <to>/webapp/index.html</to>
</rule>

Use a servlet filter. If the request is a GET, and should be forwarded to index.html, then forward it to index.html. Otherwise, pass the request down the chain. Here's an example of such a filter. Of course, depending on the architecture of your application, the conditions could be different:

@WebFilter(value = "/*")
public class IndexFilter implements Filter {
    @Override
    public void doFilter(ServletRequest req,
                         ServletResponse response,
                         FilterChain chain) throws IOException, ServletException {
        HttpServletRequest request = (HttpServletRequest) req;
        if (mustForward(request)) {
            request.getRequestDispatcher("/index.html").forward(request, response);
            return;
        }

        chain.doFilter(request, response);
    }

    private boolean mustForward(HttpServletRequest request) {
        if (!request.getMethod().equals("GET")) {
            return false;
        }

        String uri = request.getRequestURI();

        return !(uri.startsWith("/api")
            || uri.endsWith(".js")
            || uri.endsWith(".css")
            || uri.startsWith("/index.html")
            || uri.endsWith(".ico")
            || uri.endsWith(".png")
            || uri.endsWith(".jpg")
            || uri.endsWith(".gif")
            || uri.endsWith(".eot")
            || uri.endsWith(".svg")
            || uri.endsWith(".woff2")
            || uri.endsWith(".ttf")
            || uri.endsWith(".woff");
    }

    @Override
    public void init(FilterConfig filterConfig) throws ServletException {
        // nothing to do
    }

    @Override
    public void destroy() {
        // nothing to do
    }
}

Add the below code to web.xml, without adding index.html to your url, http://yourwebsite/ will load your site successfully. For some reason, adding this entry to existing welcome-file-list didn't work. I am new to java so I am not sure why. Adding a new section of welcome-file-list with index.html as shown below fixed the issue.

 <welcome-file-list>
   <welcome-file>index.html</welcome-file>
 </welcome-file-list> 
Related