1

How should i deploy my Angularjs code on tomcat 8.5

I have tried copying my project folder to webapps folder in tomcat

My project structure

webapps
   myprojectname
      controllers
      css
      views
      route.js
      index.html
      config.js

route.js file

angular.module("testroutes",["ngRoute"]).config(['$routeProvider',  function($routeProvider) {
$routeProvider.
when('/', {templateUrl: 'views/home.html'}).
when('/home', {templateUrl: 'views/home.html',controller: 'homecontroller'}). otherwise({redirectTo: '/'});
}]);

tomcat has started to run all my controllers, html are loading but routing is not working.

localhost:8080/myprojectname/home --> gives me 404

only localhost:8080/myprojectname/ --> this works. which is loading index.html

my routing is not working. could someone help

Caconde
  • 2,969
  • 7
  • 24
  • 27
priya
  • 11
  • 1

1 Answers1

0

I'm going to assume you mean hitting the routes directly isn't working, not clicking an internal link in the application and having the page fully refresh, which is another issue and means you aren't setting up your links properly. If you mean hitting the routes directly, then the simplest and least complex way to solve this is preface all your routes with a common root, like /ng/* or /ajs/*, so /ng/home for instance. Then you have 2 options, that are both very similar...

  1. Use a reverse proxy, like apache web server to handle the content, and rewrite /ng/* to index.html. This also gives you lots of other benefits that come with this setup, if you are building a non-trivial app.

  2. Create a servlet or controller or whatever you are using within your Java app to handle /ng/* paths, and return index.html. You can also do something similar to apache mod_rewrite with tomcat's rewrite valve, which might serve a similar purpose... https://tomcat.apache.org/tomcat-8.0-doc/rewrite.html.

The issue is that your request is looking for a file, in the root app case, it finds one of the default files, index.html. When you request /home, there is no /home file or /home/ folder with an index.html, so you naturally get a 404. You have to have a way to tell the server that certain types of requests (starting with /ng/ for instance) should always return index.html.

Adam Marr
  • 43
  • 8
  • Could you give me an example(link to jsfiddle of solving this issue by following option 1 – priya Sep 06 '19 at 19:57
  • Its not that simple if you aren't familiar with web and app server setup, and not representable in jsfiddle. You can google apache reverse proxy tomcat for setup options, and then mod_rewrite examples. If you are looking for a quicker solution, the tomcat rewrite valve link is probably your best bet. I'm assuming the issue is hitting the route directly, and not from an internal link correct? – Adam Marr Sep 06 '19 at 20:05
  • I'm assuming the issue is hitting the route directly, and not from an internal link correct? yes when i am hitting the route directly – priya Sep 06 '19 at 20:27
  • Then yes, I would look at the tomcat rewrite link above, as that is probably easier than setting up a web proxy if you haven't done so before. An example rule would be: `RewriteRule ^/ng/?(.*) /index.html` – Adam Marr Sep 09 '19 at 13:56