1

I am using grunt for my angular js app, and tomcat for my server side.

Whenever needed, in dev env, I run Grunt build, takes dist folder and put it in tomcat webapp folder.

However, I have a weird issue. URLs within the app work only if I click on them from an HTML page.

e.g. if I type http://localhost:8080/upload - I will get 404 page not found error, however if from index page - http://localhost:8080 , I click on a link in page that leads to upload it works fine.

What could be the problem?

If I run the app on node.js, the links work perfectly! just on Tomcat it doesn't work as expected.

EDITED

I define the urls like this in angular js

angular.module('angularjsApp')
  .config(function ($stateProvider) {
    $stateProvider
      .state('upload', {
        url: '/upload',
        templateUrl: 'app/upload/upload.html',
        controller: 'UploadCtrl'
      });
  });
Dejell
  • 13,947
  • 40
  • 146
  • 229

3 Answers3

1

In order to server by AngularJs then add /#/ after the domain and make sure html5 mode default enabled is false($locationProvider.html5mode).Below the example for your app to serve content from client site(angular-js)

http://localhost:8080/#/upload
Arpit Srivastava
  • 2,249
  • 1
  • 16
  • 28
  • how would the search engine will act? if it leads to /upload I will get 404. if I write rewrite rule it will be infinite.. – Dejell Jan 05 '15 at 08:32
1

The way you have angular setup its using history push state to navigate, changing the browser's URL without actually causing the browser to re-fetch its content from the back end.

So using push state the in the browser angular can navigate just fine, but when a request is actually sent to the server it responds 404 as you have nothing there to serve your HTML.

There are two solutions to this:

  1. Switch to use #! urls so that from the server you're always getting the same URL (see other answers).
  2. Update your server to serve the same static HTML for all paths (this answer).

You need the servlet that's sending back your initial HTML content to do so for all routes - so mapped to /* rather than just / so any path it's given always returns the same HTML.

So your web.xml has a servlet that looks like this that's returning:

<servlet>
    <servlet-name>StaticHtmlServlet</servlet-name>
    <servlet-class>com.example.StaticHtmlServlet</servlet-class>
</servlet>
<servlet-mapping>
    <servlet-name>StaticHtmlServlet</servlet-name>
    <url-pattern>/</url-pattern>
    <url-pattern>/*</url-pattern> <!-- so this is what's added -->
</servlet-mapping>
Tom
  • 43,583
  • 4
  • 41
  • 61
0

Are the links actual standard HTML links? If Tomcat doesn't answer for http://localhost:8080/upload, then maybe it's not a GET request.

Kayaman
  • 72,141
  • 5
  • 83
  • 121